Chinaunix首页 | 论坛 | 博客
  • 博客访问: 739048
  • 博文数量: 235
  • 博客积分: 4309
  • 博客等级: 中校
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-17 11:25
个人简介

If you don\\\\\\\\\\\\\\\'t wanna do it, you find an EXCUSE; if you do, you\\\\\\\\\\\\\\\'ll find a WAY :-)

文章分类

全部博文(235)

文章存档

2014年(3)

2013年(2)

2012年(31)

2011年(199)

分类: Python/Ruby

2011-01-17 17:03:15

break 命令可以带一个参数.一个不带参数的break 循环只能退出最内层的循环,而break N
可以退出N 层循环.
continue 命令也可以带一个参数.一个不带参数的continue 命令只去掉本次循环的剩余代码
.而continue N 将会把N 层循环剩余的代码都去掉,但是循环的次数不变.

下面是continue带参数的例子
#!/bin/bash
# The "continue N" command, continuing at the Nth level loop.
for outer in I II III IV V           # outer loop
do
  echo; echo -n "Group $outer: "
  # --------------------------------------------------------------------
  for inner in 1 2 3 4 5 6 7 8 9 10  # inner loop
  do
    if [ "$inner" -eq 7 ]
    then
      continue 2  # Continue at loop on 2nd level, that is "outer loop".
                  # Replace above line with a simple "continue"
                  # to see normal loop behavior.
    fi 
    echo -n "$inner "  # 7 8 9 10 will never echo.
  done  
  # --------------------------------------------------------------------
done
echo; echo
# Exercise:
# Come up with a meaningful use for "continue N" in a script.
exit 0
 
for x in 1 2 3
do
echo before $x
continue 1
echo after $x
done

The output for the preceding will be

before 1
before 2
before 3
阅读(368) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~