Lua的自我学习之路-语法学习4

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/asdfghj253/article/details/80382282

要点一:Loop
 1.while循环
--while 循环 while (condition) do statements end --condition意思是 条件
  --if语句   if(condition) then   statements   end
 2.for循环
  --1.数值for循环
  for var=start,end,step do
     循环体
  end
      --这里var会从start变化到end,每次变化以step进行(默认step是1,就是每次加1)

  
--2.泛型for循环 tab1={key1="value1",key2="value2"}; for k,v in pairs(tab1) do  --遍历,类似foreach     print(k,v); end


 3.repeat unitl  相当于(do while)
 --[[
    repeat until(do while)
    --注意:!!!!!
 --repeat until中until和do while中的while不一样
 --until是满足条件不再执行:比如until(b>10),那么b>10时就不循环了
 repeat
    循环体
 until(condition)
 --]]
 --repea until循环
 b=1
 repeat
    print(b);
 b=b+1;
 until(b>10)

嵌套


 --嵌套!!!!!!!!!

 for i=1,10 do
	for j=1,i do
	   print(i);
	end
 end

 for i=1,10 do
    j=1;
    while(j<=i) do
	   print(i);
	   j=j+1;
	end
 end

要点二:If语句
  1. if(布尔表达式) then
       为true时要执行的代码
   else
       为false时要执行的代码
   end
if(b) then
    print("b不为空");
else
    print("b为空")
end

2.   if(布尔表达式1) then
       1为true时要执行的代码
   elseif(布尔表达式2) then --注意 !!!!!  elseif要连在一起写
       2为true时要执行的代码
   else
       1,2都为false时要执行的代码
   end

if(a<=50) then
	 print("a<=50")
elseif(a<=100) then  --注意 !!!!!  elseif要连在一起写
     print("a<=100")
else
     print("上面两种条件都不满足");
end

猜你喜欢

转载自blog.csdn.net/asdfghj253/article/details/80382282