码迷,mamicode.com
首页 > 系统相关 > 详细

Linux Gvim shell break和continue命令

时间:2016-06-09 10:57:44      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

#break和continue命令:循环过程中,有时需要在未达到循环结束条件时强制跳出循环
#break:允许跳出所有循环(终止执行后面的所有循环)

 1 while :        #死循环写法#
 2 do
 3     echo -n " Input a number between 1 to 5 : "
 4     read aNum
 5     case ${aNum} in
 6     1|2|3|4|5) 
 7         echo " Your number is ${aNum}"
 8         ;;
 9     *) 
10         echo " You do not select a number betwwen 1 to 5 , game over ! " 
11         break
12         ;;
13     esac
14 done

#循环嵌套,break可以跟一个参数,表示跳出第几层循环
#例:如果var1=2并且var2=0,跳出循环

for var1 in 1 2 3
do
    for var2 in 0 5 
    do
        if [ ${var1} -eq 2 -a ${var2} -eq 0 ]
        then 
            break 2
        else
            echo " ${var1} ${var2} "
        fi
    done
done

#continue命令:不会跳出所有循环,仅仅跳出当前循环
#运行代码发现,输入的不是1-5的数字,循环也不会被终止。echo " game over " 永远不被执行

 1 while :
 2 do
 3     echo " Input a number between 1 to 5 "
 4     read aNum
 5     case ${aNum} in
 6     1|2|3|4|5)
 7         echo " Your number is ${aNum} "
 8         ;;
 9     *)
10         echo " you do not select a number between 1 to 5 ! "
11         continue
12         echo " game over "
13         ;;
14     esac
15 done

#寻找偶数

 1 numbers="1 2 3 4 5 6"
 2 for NUM in ${numbers}
 3 do
 4     Q=` expr ${NUM} % 2 `
 5     if [ ${Q} -eq 0 ]
 6     then
 7         echo " Number is an even number "
 8         continue
 9     fi
10     echo " Found odd number "
11 done

 

Linux Gvim shell break和continue命令

标签:

原文地址:http://www.cnblogs.com/blog4matto/p/5572048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!