美文网首页
lua控制结构

lua控制结构

作者: 李白太白 | 来源:发表于2017-04-08 10:27 被阅读5次

    if exp then

    end

    if exp then

    else

    end

    if exp then

    elseif exp

    else

    end

    while  exp do

    end

    while exp do

    if exp then break end

    end

    repeat

    until exp

    for有2种

    一种是数字

    另一种是范型迭代器

    数字就是for i=0,100,1 do  end

    范型是for i,j in ipairs(a) do  end 这里i是索引 j是值。a是迭代数组,返回index和value。

    类似python中的

    for i,j  in enumerate(a):    a都是要可迭代的。

    lua中还有一个pairs,迭代table元素,返回key和value

    本质上说,table这种数据类型虽然可以同时表示数组和hash,把数组和字典统一起来了,胶水语言,但是!因为底层实现的不一样,遍历数组开销很低,但是遍历hash开销就大了哦...因为hash本来就不是为了遍历而设计的。

    break刚才讲了,简单说一下return,

    php中可以用exit退出,python中return之后也可以写代码,lua中不行

    要实现类似功能需要包裹起来用 do return end

    相关文章

      网友评论

          本文标题:lua控制结构

          本文链接:https://www.haomeiwen.com/subject/ikklattx.html