当前位置:网站首页>Lua conditional statement

Lua conditional statement

2022-06-12 22:46:00 o_ Guatian Lixia_ o


lua Conditional statements

      

           

                                  

Conditional statements

       

Grammar format

# if  sentence 
if( Boolean expression )
then
   --[  The Boolean expression is  true  The statement executed when  --]
end

# if-else  sentence 
if( Boolean expression )
then
   --[  The Boolean expression is  true  When the statement block is executed  --]
else
   --[  The Boolean expression is  false  When the statement block is executed  --]
end

# if  Nested statement 
if(  Boolean expression  1)
then
   --[  Boolean expression  1  by  true  When the statement block is executed  --]
   if( Boolean expression  2)
   then
      --[  Boolean expression  2  by  true  When the statement block is executed  --]
   end
end

explain :lua in nil、false For false , The rest is true

        

           

                                  

if sentence

       

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 2>1:true
> if(2>1) then print("2>1") end
2>1

-- 0:true
> if(0) then print("0 by true") end
0 by true

-- not nil:true
> if(not nil) then print("nil For false ") end
nil For false 

      

          

                                  

if-else sentence

       

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 2<1:false
> if(2<1) then print("2<1") else print("2>=1") end
2>=1

-- nil:false
> if(nil) then print("nil It's true ") else print("nil For false ") end
nil For false 

       

          

                                  

if Nested statement

       

[email protected] ~ % cat test.lua
if(type(2)=='number') then
   if(2>2) then
      print("2>2")
    else
      if(2==2) then
         print("2==2")
      else
         print("2<2")
      end
    end
end
[email protected] ~ % lua test.lua
2==2

        

                 

原网站

版权声明
本文为[o_ Guatian Lixia_ o]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122241504186.html