资源描述:
《Lua基础语法举例》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、—Example1——FirstProgram.——Classichelloprogram.printOutputhelloPress'EnterJkeyfornextexample——Example2——Comments.——SinglelinecommentsinLuastartwithdoublehyphen.——[[Multiplelinecommentsstartv/ithdoublehyphenandtwosquarebrackets.andendwitht讨osquarebrackets.]]——
2、Andofcoursethisexampleproducesno——outputssinceit'sallcomments?OutputPress1EnteNkeyfornextexample——Example3—Uariables.一一Uariablesholdualuesvihichhauetypes,uariablesdonJthauetypes.a=lb=MabcMc=<>d=printprint>print>print>print>Outputnumber
3、stringtablefunctionPress9EnterJkeyfornextexample——Example4——Uariablenanes.一一Uariablenanesconsistofletters..digitsandunderscores.——Theycannotstartwithadigit.one_two_3=123——isvaliduarablename——l_two_3isnotavaliduariablename.OutputPressJEnter,keyfornextexample^—Example5
4、——MoveUariablenames.—Theunderscoreistypicallyusedtostartspecialvalues--like_UERSIONinLua.print<_UERSION>—SodonJtusevariablesthatstartwith—butasingleunderscore_isoftenusedasa——dummyuariable.OutputLua5.1PressJEnte*keyfornextexample——Example6——CaseSensitiue.一一Luaiscases
5、ensitiuesoalluariablenames&keywords——nustbeincorrectcase.ab=lAb=2AB=3printOutput123PressJEnkeyfornextexample^―Example7——Keywords.—Luareservedwordsare:and,break,do,else,elseif,—end,,false,for,functioni£.in,localsnil,not,orj.——repeatsreturn,,then,true,,until
6、,while.—Keywordscannotbeusedforuariablenanesj.—'and'isakeywordshutANDisnotsoitisalegaluariablename.AND=3print"ND〉Output3Press9Enter1keyfornextexamplea=usingle9quoted^stringanddoubleMquotedustringinside0b=JsingleJquotedJstringanddouble"quoted"stringinsideJc=[[nult
7、ipielineExample8——Strings.withJsingleJandMdoubleMquotedstringsinside.]]printdprintpFintOutputsingle'quoted'stringanddouble"quoted'stringinsidesingle9quoted^stringanddouble"quotedMstringinsidemultiplelinewith'singleJandMdoubleMquotedstringsinside.PressfEnkeyforn
8、extexample—Example9—Assignments.—Multipleassignmentsarevalid.—uarlj.uai'2=uar3,uar4a^bj-cj.dj.6=1.2,^three0,"Fmiu",5printCa^bj-cj.d