官术网_书友最值得收藏!

Time for action – printing values using blocks

Let's give it a shot and see how powerful a language Lua is. We're starting to get an idea of how variables work and what happens when you assign a value to it. What if you have a variable that has multiple values attached to it? How does Lua differentiate them? We'll be using the Corona Terminal so we can see the values outputted in the terminal box. Along the way, you'll pick up other programming techniques as you progress through this section. We will also be referring to chunks in this exercise. The unit of execution of Lua is called a chunk. A chunk is a sequence of statements, which are executed sequentially.

If you remember from the previous chapter, we learned how to create our own project folder and main.lua file for the Hello World application.

  1. Create a new project folder on your desktop and name it Variables.
  2. Open up your preferred text editor and save it as main.lua in your Variables project folder.
  3. Create the following variables:
    local x = 10 –- Local to the chunk
    local i = 1  -- Local to the chunk
  4. In the while loop, add the following code:
    while i<=x do    
      local x = i    -- Local to the "do" body
      print(x)       -- Will print out numbers 1 through 10 
      i = i + 1
    end
  5. Create an if statement that will represent another local body:
    if i < 20 then
      local x          -- Local to the "then" body
      x = 20
      print(x + 5)  -- 25
    else
      print(x)         -- This line will never execute since the above "then" body is already true
    end
    print(x)  -- 10
  6. Save your script.
  7. Launch the Corona Terminal. Make sure you see the CoronaSDK screen and a terminal window pop up.
  8. Navigate to your Variables project folder and open your main.lua file in the simulator. You will notice that the device in the simulator is blank, but if you look at your terminal window, there are some results from the code printed out as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    25
    10
    

What just happened?

The first two variables that were created are local outside of each block of code. Notice in the beginning of the while loop, i <= x refers to the variables in lines 1 and 2. The statement local x = i inside the while loop is only local to the do body and is not the same as local x = 10. The while loop runs ten times and prints out a value that is incremented by 1 each time.

The if statement compares i < 20, where i equals 1 and uses another local x that is local to the then body. Since the statement is true, x equals 20 and prints out the value of x + 5 which is 25.

The very last line, print(x) is not attached to any of the blocks of code in the while loop or the if statement. Therefore, it refers to local x = 10 and prints out the value of 10 in the terminal window. This may seem confusing, but it's important to understand how local and global variables work in Lua.

主站蜘蛛池模板: 墨脱县| 苍溪县| 洛宁县| 汝州市| 建阳市| 桂阳县| 临邑县| 象山县| 岳阳县| 浪卡子县| 洞头县| 焦作市| 东明县| 那曲县| 伊吾县| 墨竹工卡县| 贞丰县| 田林县| 万宁市| 正定县| 同德县| 长白| 故城县| 苏州市| 南宁市| 丽水市| 淮北市| 华坪县| 衡阳县| 上高县| 孟州市| 彰化市| 奉新县| 普安县| 衢州市| 资溪县| 聂荣县| 霍邱县| 山东| 玉林市| 克拉玛依市|