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

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

文章分类

全部博文(7)

文章存档

2015年(3)

2014年(4)

我的朋友
最近访客

分类: C/C++

2014-12-29 22:30:52


点击(此处)折叠或打开

  1. --Example 1
  2. -- First program
  3. -- classic hello program

  4. print("hello)


  5. ----------OutPut-----
  6. hello


  7. --Example 2
  8. --comments
  9. --Single comments in lua start with double hypen

  10. --[[ Multiple line comments start
  11. with double hypen and two square brackets.
  12. and end with two square brackets. ]]

  13. --Single line comments

  14. --And of course this example produce no
  15. --output because of the comments

  16. -----Output------------



点击(此处)折叠或打开

  1. -Example 3
  2. --Variables
  3. --Variables hold value which have types,but in lua Variables donot have types
  4. a = 1
  5. b = "abc"
  6. c = {}
  7. d = print

  8. print(type(a))
  9. print(type(b))
  10. print(type(c))
  11. print(type(d))

  12. ---------Output-------
  13. number
  14. string
  15. table
  16. function


  17. --Example 4
  18. --Variables names consist of letters, digits and underscores
  19. --They connot start with a digit

  20. one_two_3 = 123 -- is a valid varable name

  21. -- 1_one_two is not valid varable name

  22. ------Output------


点击(此处)折叠或打开

  1. --Example 5
  2. --More Variable names
  3. --The underscore is typically used to start special values
  4. --like _VERSION in lua

  5. print(_VERSION)

  6. --So donot use variable that start with _
  7. --but a single underscore _ is often used as a
  8. --dummy variable

  9. -------------Output------

  10. Lua 5.1

  11. --Example 6
  12. --Case Sensitive
  13. --Lua is case Sensitive so all variable name & keywords
  14. --must be correct case

  15. ab = 1
  16. Ab = 2
  17. AB = 3
  18. print(ab, Ab, AB)

  19. ------------Output---------
  20. 1 2 3

点击(此处)折叠或打开

  1. -Example 7
  2. --keywords
  3. --Lua reserved words are:
  4. -- and, break, do, else, elseif,
  5. -- end, false, for, function, if,
  6. -- in, local, nil, not, or, repeat,
  7. -- return, then, true, until, while.

  8. --keywords connot be used for variable names
  9. -- 'and' is a keyword, but And is not, so it can be a varable name

  10. And = 3
  11. print(And)

  12. ------------Output----------

  13. 3


  14. --Example 8
  15. --Strings

  16. a = "single 'quoted' string and double \"quoted\" string inside"
  17. b = 'single \'quoted\' string and double "quoted" string inside'
  18. c = [[ multiple line
  19. with 'single'
  20. and "double" quoted strings inside.]]

  21. print(a)
  22. print(b)
  23. print(c)

  24. -----------Output-------------

  25. single 'quoted' string and double "quoted" string inside
  26. single 'quoted' string and double "quoted" string inside
  27. multiple line
  28. with 'single'
  29. and "double" quoted strings inside.

点击(此处)折叠或打开

  1. --Example 9
  2. --Assigments
  3. --Multiple Assigments are valid
  4. -- var1, var2 = var3, var4

  5. a, b, c, d, e =1, 2, "three", "four", 5

  6. print(a, b, c, d, e)

  7. -----------Output--------------

  8. 1 2 three four 5


  9. --Example 10
  10. --More Assigments allows one line to swap two variables.
  11. a = 1
  12. b = 2
  13. print(a, b)
  14. a, b = b, a
  15. print(a, b)

  16. ------------Output---------------

  17. 1 2
  18. 2 1


点击(此处)折叠或打开

  1. --Example 11
  2. --numbers
  3. --Multiple Assigments showing different number formats.
  4. --Two dots<..> are used to concatenate string or a string and a number

  5. a, b, c, d, e = 1, 1.123, 1E9, -123, .000
  6. print("a="..a, "b="..b, "c="..c, "d="..d, "e="..e)


  7. ---------------Output-------------

  8. a=1 b=1.123 c=10000000 d=-123 e=0.000


  9. --Example 12
  10. --More Output
  11. --More writing output

  12. print "hello for Lua!"
  13. print ("hello for Lua!")


  14. ----------------Output--------------
  15. hello for
  16. hello for





阅读(273) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:lua自带的Example(二)

给主人留下些什么吧!~~