几类脚本的流程控制语句(shell,python,lua)

来源:互联网 发布:雅思网络课程百度云 编辑:程序博客网 时间:2024/05/17 23:04

python脚本的流程控制语句

while循环语句:

while 条件表达式:
条件表达式为真时执行的语句
else:
while循环结束后总是会执行

代码例子:

#!/usr/bin/pythoni = 1while i < 5:    print i    i += 1else:    print "while is over and arrive here"

for循环语句

for 变量 in 序列:
语句块1
else(可选):
语句块2

代码例子:

#!/usr/bin/pythonfor i in range(5):    print ielse:    print i    print "for is over and arrive here"

if条件语句

if 条件表达式1:
    当条件1为真时你要执行的代码
elif 条件表达式2:
    当条件2为真时你要执行的代码
else:
    上述两条件都不满足时执行的代码

代码例子:

#!/usr/bin/pythonimport sysi = sys.argv[1]print iif i =="1":    print 1elif i=="2":    print 2else:    print 3

lua脚本的流程控制语句

repeat循环语句:

repeat
执行语句
until 条件表达式

当条件表达式为真时,repeat结束,类似于C语言的do... while语句

例子:

#!/usr/bin/luai = 5repeat    print (i)    i = i-1until i ==1


while循环语句:

while 条件表达式 do
条件表达式为真时执行的语句
then

例子:

#!/usr/bin/luai = 1while i<4 do    print(i)    i =1+iend

for循环语句

1.数值for循环:
for Val=exp1,exp2,exp3 do
条件表达式为真时执行的语句
then
for 将用exp3 作为step 从exp1(初始值)到exp2(终止值),执行loop-part。其中
exp3 可以省略,默认step=1

例子:

for i=1,10,1 do    print iend
2.范型for循环:
for i in list do 
执行语句
end
i依次取list列表中的每一个值

if条件语句

if 条件表达式1 then
    当条件1为真时你要执行的代码
elseif 条件表达式2 then
    当条件2为真时你要执行的代码
else
    上述两条件都不满足时执行的代码

end

例子:

#!/usr/bin/luai = tonumber(arg[1])if i==1 then    print(1)elseif i==2 then    print(2)else    print(3)end
shell脚本的流程控制语句

while循环语句:
while 条件
 do 
执行语句
done

例子:

i=10while [[ $i -gt 5 ]]do    echo $i    ((i--))done

for循环语句

1.for...in语句
for 变量 in seq字符串
do
执行语句
done

例子1:

#!/bin/shfor i in $(seq 10); do    echo $i;done;
例子2:
#for i in {1..10}for i in 1 2 3 4 5do    echo $idone

2.for((负值;条件;运算语句))
 do
执行语句
done

例子:

#!/bin/shfor((i=1;i<=10;i++));do    echo $idone

if条件语句:


if 条件语句 then
    执行语句
elif条件语句 then
     执行语句
else
     执行语句
fi
关于then的位置,如果和条件语句在同一行需要使用 分号;隔开,如果换行则不需要分号。

例子:

#!/bin/shi=40;if [[ $i -gt 1 ]]; then    echo "1";elif [[ $i -gt 2 ]]; then    echo "2";elif [[ $i -gt 3 ]]; then    echo "3";else    echo "0";fi;





0 0