條件語句
if 條件 then
...
else if 條件 then
...
else
...
end if
舉例:
set num to 14
if num is 13 then -- 不能使用== ,只能使用is表示相等
set num to 13 * 2
else if num is 14 then
set num to 14 * 2
else
set num to num * 5
end if
循環(huán)語句
語法1:
repeat 循環(huán)次數(shù) times
end repeat
舉例:
set num to 2
repeat 3 times
set num to num * 2
end repeat
語法2:
repeat with 計(jì)數(shù)變量 from 計(jì)數(shù)變量初始值 to 計(jì)數(shù)變量目標(biāo)值 by 每次增量
end repeat
注:by省略時(shí),默認(rèn)值為1
舉例:
set num to 2
repeat with i from 2 to 4 by 1 //i初值為2,每次增長1,直到4
set num to num * i
end repeat
語法3:
repeat while 條件
end repeat
舉例:
repeat while num < 100
set num to num * 2
end repeat
語法4:
repeat until 條件
end repeat
舉例
set num to 2
repeat until num > 100
set num to num * 2
end repeat
語法5:
repeat with 單個(gè)元素名 in 循環(huán)對(duì)象
end repeat
舉例
set arr to {"張三", "李四", "王五", "謝六"}
set appendStr to ""
repeat with nameStr in arr
set appendStr to appendStr & nameStr
end repeat