跳转到内容

TI-Lua/流程控制:修订间差异

计算器百科,非营利的计算器专业知识百科。
Dousha99留言 | 贡献
先开坑,缺的内容之后放寒假再补
 
Dousha99留言 | 贡献
填坑与导航栏
 
第4行: 第4行:


=== 条件判断 ===
=== 条件判断 ===
==== 形式 ====


<code>if-else</code> 结构是基本的条件判断流程。其结构如下:
<code>if-else</code> 结构是基本的条件判断流程。其结构如下:
第16行: 第18行:


其中,<code>cond</code> 是一个[[TI-Lua/基本数据结构|布尔值]]。下同。
其中,<code>cond</code> 是一个[[TI-Lua/基本数据结构|布尔值]]。下同。
==== 例子 ====
输入:
<syntaxhighlight lang="lua">
if 1 == 2 then
  print("Our math is broken!")
else
  print("Everything is fine.")
end
</syntaxhighlight>
输出:
<code>Everything is fine.</code>
''注: TI-Lua 已经在 3.1 之后的版本中去除了 print 函数(这个函数在机上可向串口输出数据,但是其实际功能是向标准输出中输出数据)。这里仅作示意用途。''


=== 循环 ===
=== 循环 ===
==== 形式 ====


循环体分为 <code>for</code> 循环、 <code>while</code> 循环和 <code>repeat-until</code> 循环。我们首先来讨论 <code>while</code> 循环。
循环体分为 <code>for</code> 循环、 <code>while</code> 循环和 <code>repeat-until</code> 循环。我们首先来讨论 <code>while</code> 循环。


==== <code>while</code> 循环 ====
===== <code>while</code> 循环 =====


<code>while</code> 循环结构如下:
<code>while</code> 循环结构如下:
第31行: 第53行:
</syntaxhighlight>
</syntaxhighlight>


==== <code>repeat-until</code> 循环 ====
该循环会不断执行,直到 <code>cond</code> 为假。
 
===== <code>repeat-until</code> 循环 =====


<code>repeat-until</code> 循环结构如下:
<code>repeat-until</code> 循环结构如下:
第41行: 第65行:
</syntaxhighlight>
</syntaxhighlight>


==== <code>for</code> 循环 ====
该循环会不断执行,直到 <code>cond</code> 为真。
 
===== <code>for</code> 循环 =====


<code>for</code> 循环具有两种形式。第一种是”步进型“,第二种是”遍历型“(即 <code>foreach</code>)。
<code>for</code> 循环具有两种形式。第一种是”步进型“,第二种是”遍历型“(即 <code>foreach</code>)。
第48行: 第74行:


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Form 1
-- 步进型
for v = v_begin, v_end, v_step do
for v = v_begin, v_end, v_step do
   -- something
   -- something
end
end
</syntaxhighlight>
在此代码中, <code>v</code> 是一个自变量, <code>v_begin, v_end, v_step</code> 分别代表 <code>v</code> 的初始值、终止值与步长。
这种循环会不断执行,在执行过程中,<code>v</code> 的值会从 <code>v_begin</code> 以 <code>v_step</code> 的步长增长到 <code>v_end</code>, 然后结束。如果按照步长,<code>v</code> 在最后一步时会超过 <code>v_end</code>, <code>v</code> 的值仍会保留而不会再将 <code>v_end</code> 的值赋给 <code>v</code>.


-- Form 2
<syntaxhighlight lang="lua">
-- 遍历型
for v1, v2, ..., vn in a_list do
for v1, v2, ..., vn in a_list do
   -- something
   -- something
end
end
</syntaxhighlight>
</syntaxhighlight>
在此代码中, <code>v1, v2, ..., vn</code> 是有穷多个自变量。<code>a_list</code> 是一个列表或能够产生列表的表达式。在循环过程中,(产生的)列表中的项中的元素会按顺序赋给 <code>v1, v2, ..., vn</code>,直到每一个项按顺序访问过一遍。
需要注意到一个项中可以包含多个元素。这一点我们会在示例中说明。
==== 例子 ====
输入:
<syntaxhighlight lang="lua">
local i, j, arr
i = 0
j = 0
arr = {"Calculator", "Wiki"}
-- while example
print("While example:")
while i < 5 do
  print(i)
  i = i+1
end
-- repeat example
print("Repeat example:")
repeat
  print(j)
  j = j+1
until j >= 5
-- numeric for example
print("Numeric for example:")
for i, 0, 10, 3 do
  print(i)
end
-- generic for example
print("Generic for example:")
for i, j in ipairs(arr) do
  print(i, j)
end
</syntaxhighlight>
输出:
<pre>
While example:
0
1
2
3
4
Repeat example:
0
1
2
3
4
Numeric for example:
0
3
6
9
Generic for example
1      Calculator
2      Wiki
</pre>


=== <del>跳转</del> ===
=== <del>跳转</del> ===
第65行: 第162行:
| text = 由于 TI-Lua 是 Lua 5.1 的实现,而 <code>goto</code> 指令于 Lua 5.2 才添加,因此该小节可能不具有实际意义。
| text = 由于 TI-Lua 是 Lua 5.1 的实现,而 <code>goto</code> 指令于 Lua 5.2 才添加,因此该小节可能不具有实际意义。
}}
}}
{{TI-Lua}}

2017年2月11日 (六) 14:02的最新版本

流程控制

本节将讨论 TI-Lua 中的流程控制,其中包括条件判断、循环与无条件跳转

条件判断

形式

if-else 结构是基本的条件判断流程。其结构如下:

if cond then
  -- executes when cond is true
else
  -- executes when cond is false
end

其中,cond 是一个布尔值。下同。

例子

输入:

if 1 == 2 then
  print("Our math is broken!")
else
  print("Everything is fine.")
end

输出:

Everything is fine.

注: TI-Lua 已经在 3.1 之后的版本中去除了 print 函数(这个函数在机上可向串口输出数据,但是其实际功能是向标准输出中输出数据)。这里仅作示意用途。

循环

形式

循环体分为 for 循环、 while 循环和 repeat-until 循环。我们首先来讨论 while 循环。

while 循环

while 循环结构如下:

while cond do
  -- something
end

该循环会不断执行,直到 cond 为假。

repeat-until 循环

repeat-until 循环结构如下:

repeat
  -- something
until cond

该循环会不断执行,直到 cond 为真。

for 循环

for 循环具有两种形式。第一种是”步进型“,第二种是”遍历型“(即 foreach)。

for 循环结构如下:

-- 步进型
for v = v_begin, v_end, v_step do
  -- something
end

在此代码中, v 是一个自变量, v_begin, v_end, v_step 分别代表 v 的初始值、终止值与步长。 这种循环会不断执行,在执行过程中,v 的值会从 v_beginv_step 的步长增长到 v_end, 然后结束。如果按照步长,v 在最后一步时会超过 v_end, v 的值仍会保留而不会再将 v_end 的值赋给 v.

-- 遍历型
for v1, v2, ..., vn in a_list do
  -- something
end

在此代码中, v1, v2, ..., vn 是有穷多个自变量。a_list 是一个列表或能够产生列表的表达式。在循环过程中,(产生的)列表中的项中的元素会按顺序赋给 v1, v2, ..., vn,直到每一个项按顺序访问过一遍。

需要注意到一个项中可以包含多个元素。这一点我们会在示例中说明。

例子

输入:

local i, j, arr
i = 0
j = 0
arr = {"Calculator", "Wiki"}

-- while example
print("While example:")
while i < 5 do
  print(i)
  i = i+1
end

-- repeat example
print("Repeat example:")
repeat
  print(j)
  j = j+1
until j >= 5

-- numeric for example
print("Numeric for example:")
for i, 0, 10, 3 do
  print(i)
end

-- generic for example
print("Generic for example:")
for i, j in ipairs(arr) do
  print(i, j)
end

输出:

While example:
0
1
2
3
4
Repeat example:
0
1
2
3
4
Numeric for example:
0
3
6
9
Generic for example
1       Calculator
2       Wiki

跳转

由于 TI-Lua 是 Lua 5.1 的实现,而 goto 指令于 Lua 5.2 才添加,因此该小节可能不具有实际意义。