日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

语言for循环联程_Lua循环

發布時間:2024/4/11 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 语言for循环联程_Lua循环 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-- Lua循環--[[很多情況下我們需要做一些有規律性的重復操作,因此在程序中就需要重復執行某些語句。一組被重復執行的語句稱之為循環體,能否繼續重復,決定循環的終止條件。循環結構是在一定條件下反復執行某段程序的流程結構,被反復執行的程序被稱為循環體。循環語句是由循環體及循環的終止條件兩部分組成的。start --------> 條件 ---------> finish (條件為false) ---------> 執行條件代碼(條件為true) ------> 條件(直到條件為false)Lua 語言提供了以下幾種循環處理方式:循環類型 描述while循環 在條件為 true 時,讓程序重復地執行某些語句。執行語句前會先檢查條件是否為 true。for循環 重復執行指定語句,重復次數可在 for 語句中控制。repeat...until 重復執行循環,直到 指定的條件為真時為止循環嵌套 可以在循環內嵌套一個或多個循環語句(while do ... end;for ... do ... end;repeat ... until;)--]]print("\n\nwhile循環\n")-- Lua 編程語言中 while 循環語句在判斷條件為 true 時會重復執行循環體語句。--[[語法Lua 編程語言中 while 循環語法:while(condition)do statementsendstatements(循環體語句) 可以是一條或多條語句,condition(條件) 可以是任意表達式,在 condition(條件) 為 true 時執行循環體語句。start ------> condition --------> code block(condition is true) -------> condition(true/false) --------> finish(condition is false)在以上流程圖中我們可以看出在condition(條件)為 false 時會跳過當前循環并開始腳本執行緊接著的語句。]]-- 來看例子a = 10while(a < 20) do print("a的值為:",a) a = a + 1end--[[ 輸出結果為:a的值為: 10a的值為: 11a的值為: 12a的值為: 13a的值為: 14a的值為: 15a的值為: 16a的值為: 17a的值為: 18a的值為: 19 ]]times = 10while(times < 15) do print("你好呀!世界") times = times + 1endprint("\n\nLua for 循環\n")function f(num) return num * 2end--[[Lua 編程語言中 for 循環語句可以重復執行指定語句,重復次數可在 for 語句中控制。Lua 編程語言中 for語句有兩大類::數值for循環泛型for循環數值for循環Lua 編程語言中數值 for 循環語法格式:for var=exp1,exp2,exp3 do end var 從 exp1 變化到 exp2,每次變化以 exp3 為步長遞增 var,并執行一次 "執行體"。exp3 是可選的,如果不指定,默認為1。]]-- 來,看看例子for i=1, f(2) do print(i)endfor i = 5, 1, -1 do print(i)end-- for的三個表達式在循環開始前一次性求值,以后不再進行求值。比如上面的f(x)只會在循環開始前執行一次,其結果用在后面的循環中。function f_test(n) print("function f_test") return n + 1endfor i=1, f_test(3) do print(i)-- function f_test-- 1-- 2-- 3-- 4endprint("\n泛型for 循環\n")--[[泛型for循環通過一個迭代器函數來遍歷所有值,類似java中的foreach語句Lua編程語言中泛型for循環語法格式直接上例子]]a = {"hello","world","I","Like","You"}for i,v in ipairs(a) do print(i,v)-- 1 hello-- 2 world-- 3 I-- 4 Like-- 5 Youendprint("\n")--[[額外補充, ipairs 和 pairs區別1:ipairs遍歷時,遇到nil會停止,而pairs會輸出nil值然后繼續下去區別2:ipairs并不會輸出table中存儲的鍵值對,會跳過鍵值對,然后順序輸出table中的值,遇到nil則會停止而pairs會輸出table中國呢的鍵和鍵值對,先順序輸出值,再亂序(鍵的哈希值)輸出鍵值對原因:table在存儲值的時候是按照順序的,但是在存儲鍵值對的時候,是按照鍵的哈希值存儲的,并不會按照字母順序或是數字順序存儲]]local taba = {"Hello","world",aaa = 1,bbb =2,ccc=3,ddd = 100,"Good",nil,"Love","You"}for i ,v in ipairs(taba) do print(i,v)-- 1 Hello-- 2 world-- 3 Goodendprint("\nnext\n")for k,v in pairs(taba) do print(k,v)-- 1 Hello-- 2 world-- 3 Good-- 5 Love-- 6 You-- ddd 100-- bbb 2-- aaa 1-- ccc 3end--[[ 對于taba 來說,如果執行print(taba[3]),輸出的結果也會是Good,也就是說table并不會給鍵值對一個索引值 也就是說,ipairs只是按照索引順序,打印出了table中有索引的數據,沒有索引的值不管 而pairs是先按照索引值打印,打印完成后再按照鍵值對的鍵的哈希值打印他的值]]print(taba[3]) -- Good-- 所以嘞,要怎么順序輸出一個table里面的鍵值對?local tab = { key1 = 1, key2 = 2, key3 = 3, key4 = 4, key5 = 5, key6 = 6, key7 = 7 , key8 = 8,}function getLen(tab) local len = 0 for _,v in pairs(tab) do len = len + 1 end return lenendlocal keyfor i=1,getLen(tab) do key = "key" .. i print(key,tab[key])-- key1 1-- key2 2-- key3 3-- key4 4-- key5 5-- key6 6-- key7 7-- key8 8endprint("\n\n")-- i 是數組索引值,v是對應索引的數組元素值,ipairs是Lua提供的一個迭代器函數,用來迭代數組-- 循環數組 daysdays = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}for i,v in ipairs(days) do print(v)-- 星期一-- 星期二-- 星期三-- 星期四-- 星期五-- 星期六-- 星期日endprint("\n\nLua repeat ... until 循環\n")--[[ Lua編程語言中repeat ... until 循環語句不同于 for 和 while 循環,for 和 while 循環的條件語句在當前循環 執行開始時判斷,而 repeat ... until 循環的條件語句在當前循環結束后判斷 語法格式: repeat statements until(condition) 我們注意到循環條件判斷語句(condition)在循環體末尾部分,所以在條件進行判斷前循環體都會執行一次。 如果條件判斷語句(condition)為 false,循環會重新開始執行,直到條件判斷語句(condition)為 true 才會停止執行。 Lua repeat...until 循環流程圖如下: start --------> code block ----------> condition ----------> code block(codition is false) ----------> finish(condition is true)]]vara = 10repeat print("a的值為:",vara) vara = vara + 3-- a的值為: 10-- a的值為: 13-- a的值為: 16-- a的值為: 19until(vara > 20)print("\n\n")print("Lua 循環嵌套\n")-- Lua編程語言中允許循環中嵌入循環。--[[ Lua for 循環嵌套語句語法格式 for init,max/min value, increment do for init,max/min value, increment do statements end statements end]]for i =1, 4 do for j=1, i, 2 do print("i=",i,"j=",j) endend-- i= 1 j= 1-- i= 2 j= 1-- i= 3 j= 1-- i= 3 j= 3-- i= 4 j= 1-- i= 4 j= 3--[[ Lua while循環欠條語法格式 while(condition) do while(condition) do statements end statements end]]wi =1while(wi <= 3)do iwj = 1 while(iwj <= wi) do print("wi=",wi,"iwj=",iwj) iwj = iwj + 1 end wi = wi + 1end-- wi= 1 iwj= 1-- wi= 2 iwj= 1-- wi= 2 iwj= 2-- wi= 3 iwj= 1-- wi= 3 iwj= 2-- wi= 3 iwj= 3--[[ Lua 中 repeat ... until 循環嵌套語法格式 repeat statements repeat statements until(condition) until(condition) ]]rui = 1repeat iruj = 1 repeat print("rui=",rui,"iruj=",iruj) iruj = iruj + 1 until(iruj > rui) rui = rui + 1until(rui > 3)-- rui= 1 iruj= 1-- rui= 2 iruj= 1-- rui= 2 iruj= 2-- rui= 3 iruj= 1-- rui= 3 iruj= 2-- rui= 3 iruj= 3-- 除了以上同類型的循環嵌套外,還可以使用不同循環類型來嵌套 如for循環中嵌套while循環mix = 3for i=1,mix do tmp = 1 while(tmp <= i) do deep_in = 1 repeat print("deep_in=",deep_in,"tmp=",tmp,"i=",i) deep_in = deep_in + 1 until(deep_in > tmp) tmp = tmp + 1 endend-- deep_in= 1 tmp= 1 i= 1-- deep_in= 1 tmp= 1 i= 2-- deep_in= 1 tmp= 2 i= 2-- deep_in= 2 tmp= 2 i= 2-- deep_in= 1 tmp= 1 i= 3-- deep_in= 1 tmp= 2 i= 3-- deep_in= 2 tmp= 2 i= 3-- deep_in= 1 tmp= 3 i= 3-- deep_in= 2 tmp= 3 i= 3-- deep_in= 3 tmp= 3 i= 3-- 使用for循環打印乘法表raw = 9col = 9output = ""for i=1,raw do for j=1,i do output = output .. j .. "*" .. i .. "=" .. j*i .. "\t" end print(output) output = ""end-- 1*1=1 -- 1*2=2 2*2=4 -- 1*3=3 2*3=6 3*3=9 -- 1*4=4 2*4=8 3*4=12 4*4=16 -- 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 -- 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 -- 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 -- 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 -- 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 print("\n\n\n")--[[ 循環控制語句 循環控制語句用于控制程序的流程,以實現程序的各種結構方式 Lua支持一下循環控制語句 控制語句 描述 break語句 退出當前循環或語句,并開始腳本執行緊接著的語句。 goto語句 將程序的控制點轉移到一個標簽處。--]]--[[ break語句Lua 編程語言 break 語句插入在循環體中,用于退出當前循環或語句,并開始腳本執行緊接著的語句。如果你使用循環嵌套,break語句將停止最內層循環的執行,并開始執行的外層的循環語句。語法Lua 編程語言中 break 語句語法格式:start -------> conditional code ------> condition ---------> finish (condition is failse) ---------> coditional code (if condition is true) ------>(break) finish以下實例執行while循環,在變量a小于20時輸出a的值,并且在a大于15時終止執行操作]]a = 12while (a < 20) do print("a的值為:",a) a = a + 1 if (a > 15) then print("要break了") break endend-- a的值為: 12-- a的值為: 13-- a的值為: 14-- a的值為: 15-- 要break了print("\n\n")-- Lua語言的 goto 語句--[[ Lua 語言中的 goto 語句允許將控制流程無條件地轉到被標記的語句處。 語法 語法格式如下所示: goto Label Label 的格式為: :: Label ::]]-- 在判斷語句中使用gotolocal a = 1::label:: print("=----- gogo label ----=")a = a + 1if a < 3 then goto labelend-- =----- gogo label ----=-- =----- gogo label ----=-- 在label中設置多個語句i = 0::s1:: do print("in ::s1:: i=",i) i = i + 1endif i > 3 then print("the follow run is os.exit") -- os.exit() goto do_breakendgoto s1::do_break::-- in ::s1:: i= 0-- in ::s1:: i= 1-- in ::s1:: i= 2-- in ::s1:: i= 3-- the follow run is os.exit-- 有了goto可以實現contiue功能for i=1,5 do if i <=2 then print(i,"yes continue") goto continue end print(i,"no continue") ::continue:: print([[I'm end]])end-- 1 yes continue-- I'm end-- 2 yes continue-- I'm end-- 3 no continue-- I'm end-- 4 no continue-- I'm end-- 5 no continue-- I'm end--[[ 無限循環 在循環體中如果條件永遠為true,循環語句就會永遠執行下去, 以while循環為例子--]]-- while(true)-- do-- print("這里是死循環")--?end

掃碼關注一下唄~

總結

以上是生活随笔為你收集整理的语言for循环联程_Lua循环的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。