语法区别举例:
item | python | shell | c++ |
变量定义 | a = 10 | a=10 | int a = 10 |
数组定义 | arr=[1, 'add', 3] | arr=(1 'a' 'hello') declare -A arr=(['a']=1 ['b']=2) | int arr[] = {1, 2, 3} |
if条件判断 | if xxx: xxx else: xxx | if [ expressions ];then | if(xxx){ xxx; } else if(xxx){ xxx; } else{xxx;} |
while循环 | while xxx: xxx | while [ expressions ];do xxx done | while(xxx){ xxx; } |
do..while语句 | 无 | 无 | do{ xxx; }while(xxx); |
for循环 | for i in range(10): xxx | for i in {1..10} do xxx done 或者是 for((i=0;i<10;i++)) | for(int i=0;i<10;i++){ xxx; } |
switch..case语句 | 类似match...case语句: match status: case 404: xxx case 500: xxx case _: xxx | 类似case…esac语句: case $1 in -n) xxxx;; *) xxxx; | int score=20; switch(score){ case 10: xxx;break; case 20: xxx;break; default: xxx;break; } |
until语句 | 无 | until [ expressions ];do xxx done | 无 |
break | 仅退出当前循环 | 默认退出当前循环,但可通过break x 退出x层循环 | 仅退出当前循环 |