首页 > 其他分享 >Lua学习--4

Lua学习--4

时间:2023-03-04 22:57:03浏览次数:47  
标签:break repeat return -- 学习 Lua exp end block

结构控制

 

2.4.4 – Control Structures

The control structures ifwhile, and repeat have the usual meaning and familiar syntax:

	stat ::= while exp do block end
	stat ::= repeat block until exp
	stat ::= if exp then block {elseif exp then block} [else block] end

Lua also has a for statement, in two flavors (see §2.4.5).

The condition expression of a control structure can return any value. Both false and nil are considered false. All values different from nil and false are considered true (in particular, the number 0 and the empty string are also true).

In the repeatuntil loop, the inner block does not end at the until keyword, but only after the condition. So, the condition can refer to local variables declared inside the loop block.

The return statement is used to return values from a function or a chunk (which is just a function). Functions and chunks can return more than one value, and so the syntax for the return statement is

	stat ::= return [explist]

The break statement is used to terminate the execution of a whilerepeat, or for loop, skipping to the next statement after the loop:

	stat ::= break

break ends the innermost enclosing loop.

The return and break statements can only be written as the last statement of a block. If it is really necessary to return or break in the middle of a block, then an explicit inner block can be used, as in the idioms do return end and do break end, because now return and break are the last statements in their (inner) blocks.

在结构控制中有循环 
repeat block until exp
do block while exp
if exp then block {elseif exp then block} {else block} end
for exp do block end
exp 表达式只有 false 和 nil 才会是false,其它值都是true(包括数值的0和字符串的空)
在循环block 中 可以通过 break 结束当前block最内层循环 或goto 跳转 到 label,格式如下:

:: Label ::

 

c1,d1=3
--print(c1,d1)
a,b=1,2
if( a>b ) then
    print('a='..a)
elseif(a ~= b ) then
    print('b='..b)
else
    print(' a equal b')
end

 

i=5
repeat 
    print(i) 
    i=i-1 
until i<=0

 

标签:break,repeat,return,--,学习,Lua,exp,end,block
From: https://www.cnblogs.com/hztech/p/17179426.html

相关文章

  • 第六章--财政收入影响因素分析及预测
    1.数据分析 importmatplotlib.pyplotaspltimportnumpyasnpimportpandasaspdinputfile='./data.csv'data=pd.read_csv(inputfile)describe=data.d......
  • 洛谷P1213 [USACO1.4][IOI1994]时钟 The Clocks
    这是一个暴力枚举题有两种解决方法,第一种用九重for循环(有点麻烦,尽量别用),第二种简化版(虽然行数少了,但难理解),先来看看 题目!!!题目描述考虑将如此安排在一个 3*3 行......
  • 图片
    ......
  • go的timeDuration
    packagemainimport( "fmt" "time")//https://blog.csdn.net/qq_35655945/article/details/82706022typeDurationint64const( NanosecondDuration=1 Mi......
  • 判断本机ip地址是否是公网ip
    打开网页  https://www.ip138.com/如果ip地址是10或者100开头,肯定不是公网ip如果后面的地址城市显示的不是所在的真实城市,那99%不是公网ip如果最后运营商不是联通或者......
  • TCP通信聊天服务端和客户端(C/C++语言开发)附完整源码
    距离上次学Python写的Python实现简单聊天室已经过去好久了,现在学c++又写了一遍,其实过程差不多,无非是语法的变化,目前仅实现最简单的一对一的通信,然后改就是了,接下来应该是......
  • go的sync,未完成timeDuration
    packagemainimport( "fmt" "sync" "time")varproducer=func(wg*sync.WaitGroup,lsync.Locker){ deferwg.Done() fori:=5;i>0;i--{ l.Lock()......
  • display/float/clear
    displayinline:将元素变为行内元素。不会独占一行,排列自左向右。可以通过display:inline将一个块元素变为行内元素。block:将元素变为块元素。独占一行,排列......
  • 信息显示程序
    第一个程序(C语言)显示信息Hello,world!通过printf语句实现信息显示在数据段给出这个字符串形式的信息:;数据段msgbyte'Hello,Assembly!',13,10,0;定义要显示的字符串......
  • Acwing 93周赛 C
    异或值(字典树)思路唉,人太笨了,知道用字典树,但想不出过程,知其然而不知其所以然。代码voidinsert(intx){ intp=0; for(inti=30;i>=0;i--) { intu=(x>>i)&......