Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8473
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-27 22:31
个人简介

风过竹林~那种感觉很神奇

文章分类

全部博文(7)

文章存档

2015年(3)

2014年(4)

我的朋友
最近访客

分类: C/C++

2014-12-30 20:41:22


点击(此处)折叠或打开

  1. --Example 21
  2. -- repeat until statement
  3. a = 0
  4. repeat
  5.     a = a+1
  6.     print(a)
  7. until a = 5

  8. -----------------------Output---------------------
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5


  14. --Example 22
  15. --for statement
  16. --Numberic iteration form

  17. -- Count from 1 to 4 by 1
  18. for a = 1, 4 do io.write(a) end -- 1 ~ 4 default step=1

  19. print()

  20. -- Count from 1 to 6 by 3
  21. for a =1,6,3 do io.write(a) end -- 1 ~ 6 step=3

  22. ------------------------Output----------------------
  23. 1234
  24. 14


  25. --Example 23
  26. --More for statement
  27. --Swquential iteration form

  28. for key, value in pairs({1, 2, 3, 4}) do print(key, value) end


  29. -----------------------Output-------------------------

  30. 1 1
  31. 2 2
  32. 3 3
  33. 4 4


  34. --Example 24
  35. --Printing tables
  36. --Simple Way to print tables

  37. a = {1, 2, 3, 4, "five", "elephant", "mouse"}

  38. for i, v in pairs(a) do print(i, v) end


  39. -----------------------Output--------------------------
  40. 1        1
  41. 2        2
  42. 3        3
  43. 4        4
  44. 5        five
  45. 6        elephant
  46. 7        mouse


  47. --Example 25
  48. --break statement

  49. a=0
  50. while true do
  51.     a = a+1
  52.     if a==10 then
  53.         break
  54.     end
  55. end
  56. print(a)

  57. -------------------------Output---------------------------

  58. 10

  59. --Example 26
  60. --functions
  61. --Define a function without parameters or return value

  62. function myFirstLuaFunction()
  63.     print("My First Lua function was called")
  64. end

  65. --Call myFirstLuaFunction

  66. myFirstLuaFunction()

  67. --------------------------Output--------------------------
  68. My First Lua function was called


  69. --Example 27
  70. --More function
  71. --Define a function with a return value

  72. function mySecondLuaFunction() --why??????
  73.     return "My Second Lua function was called"
  74. end

  75. --Call function returning a value
  76. a=mySecondLuaFunction("string") ----why?????
  77. print(a)


  78. -----------------------------Output-------------------------
  79. My Second Lua function was called


  80. --Example 28
  81. --More functions
  82. --define function with multiple parameters and multiple return values
  83. function myFirstLuaFunctionWithMultipleReturnValue(a, b, c)
  84.     return a, b, c,"My First Lua Function with multiple return values", 1, true

  85. a,b,c,d,e,f = myFirstLuaFunctionWithMultipleReturnValue(1, 2, "three")
  86. print (a,b,c,d,e,f )

  87. ------------------------------Output--------------------------
  88. 1        2        three    My First Lua Function with multiple return values
  89. 1        true


  90. --Example 29
  91. --Variable scoping and functions
  92. --All variables are global in scope by default

  93. b="global"

  94. --To make local variable you must put the keyword 'local' in front
  95. function myFunc()
  96.     local b ="local variable"
  97.     a = "global variable"
  98.     print(a,b)
  99. end

  100. myfunc()
  101. print(a,b)

  102. --------------------------------Output--------------------------
  103. global variable local variable
  104. global variable         global


  105. --Example 30                -----Why?????
  106. --Formatted Printing
  107. --An implementation of printf

  108. function printf(fmt, ..)
  109.     io.write(string.format(fmt, ...))
  110. end

  111. printf("Hello %s from %s on %s\n",
  112.         os.getenv"USER" or "there", _VERSION, os.date())


  113. -------------------------------Output----------------------------
  114. Hello there from Lua 5.1 on 12/30/14 20:21:35

阅读(380) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~