break、continue区别
continue命令continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。对上面的例子进行修改:1. #!/bin/bash2. while :3. do4. echo -n "Input a number between 1 to 5: "5. read aNum6. case $aNum in7. 1|2|3|4|5) echo "Your number is $aNum!"8. ;;9. *) echo "You do not select a number between 1 to 5!"10. continue11. echo "Game is over!"12. ;;13. esac14. done运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句1. echo "Game is over!"永远不会被执行。同样,continue 后面也可以跟一个数字,表示跳出第几层循环。再看一个 continue 的例子:1. #!/bin/bash2. NUMS="1 2 3 4 5 6 7"3. for NUM in $NUMS4. do5. Q=`expr $NUM % 2`6. if [ $Q -eq 0 ]7. then8. echo "Number is an even number!!"9. continue10. fi11. echo "Found odd number"12. done运行结果:Found odd numberNumber is an even number!!Found odd numberNumber is an even number!!Found odd numberNumber is an even number!!Found odd number (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |