FreeTstar

loving Freedom + + loving Linux

  • 首页
  • Archive
  • 关于
  • 杂七杂八
  • 留言板
Twitter Sina RSS

小说Python中的while/for循环后的else

发布于 2011年10月27日 由 freetstar
15 条评论发表评论

首先来看c/c++语言,在c/c++中我们在处理一个循环时,通常是如下形式

for(i=0;i<n;i++)
      {your code}//可能有必要的break或者continue语句

或者

while(True)
      {your code}//可能有必要的break或者continue语句

 

基本上是for/while关键字加上条件,然后循环体内写代码,代码内可能包含了必要的break或者continue语句来跳出循环或者跳过当此循环。而循环之后再则是其他语句,与前边的循环无直接关系
我们再来看python语言,在Python中的while或者for循环之后还可以有else子句,形如下:

for x in range(1,5):
      if x == 6 :
           print "found the number",x
           break;
else:
      print "not found!"

保存上边的代码,运行得到“not found”,WHY?我们先来看看python官方文档中的解释,原文在这儿:

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

翻译:循环语句后可以有一个else从句,这个else从句在因为for循环中list全部遍历完或者因为while循环中条件不满足而执行,而如果循环中有break语句执行了则else从句就不执行了。。

简单理解来:for循环中if条件一直不满足,则最后就执行else语句
我们这里来简单想象下如果用c语言式的写法,即添加found flag

found = false
for x in range(1,5):
     if x == 6:
          found = True
          print "found the number",x
          break;
 if !found:
     print "nout found!"

每个人有可能对这样的实现有自己的理解,可能大家更喜欢或者习惯使用found flag:)

更多的讨论,在这里

分类:python
linux下限制局域网内部使用p2p软件的主机
理解版权和许可协议

15 则回应给 “小说Python中的while/for循环后的else”

  1. maplebeats CHINA Mozilla Firefox Linux 说道:
    2011年11月5日 11:26 上午

    发表评论的名字下面的日期有点蛋疼哦。。。重复了?

    [回复]

  2. maplebeats CHINA Mozilla Firefox Linux 说道:
    2011年10月29日 7:40 下午

    C语言学习中,python放在后面的 :shock:

    [回复]

    freetstar CHINA Mozilla Firefox Linux 回复:
    十一月 2nd, 2011 at 6:12 下午

    python比c好学,哈哈

    [回复]

  3. 婉秋 AUSTRALIA Mozilla Firefox Windows 说道:
    2011年10月28日 5:16 下午

    看懂了什么意思,嗯,不过我还没用过python就是了~ :mrgreen:

    [回复]

    freetstar CHINA Google Chrome Windows 回复:
    十月 29th, 2011 at 7:43 下午

    @婉秋, 还是你懂~

    [回复]

  4. yish CHINA Google Chrome Ubuntu Linux 说道:
    2011年10月28日 9:31 上午

    用来说明文章核心意思的例子中的else缩进有问题

    [回复]

    freetstar CHINA Mozilla Firefox Linux 回复:
    十月 28th, 2011 at 10:05 上午

    @yish, 恩恩,已修正,thx :???:

    [回复]

  5. Mucid CHINA Mozilla Firefox Linux 说道:
    2011年10月27日 11:04 下午

    其实也不是很必要的说

    [回复]

    freetstar CHINA Mozilla Firefox Linux 回复:
    十月 28th, 2011 at 10:03 上午

    @Mucid, 其实也是为了易读性,还有在try/except后还可以跟上else子句

    [回复]

  6. hitsmaxft Mozilla Firefox Linux 说道:
    2011年10月27日 10:47 下午

    else应该跟for对齐,
    按文中的缩进格式, 应该输出4个not found

    [回复]

    freetstar CHINA Mozilla Firefox Linux 回复:
    十月 28th, 2011 at 10:04 上午

    @hitsmaxft, 感谢,已修正

    [回复]

  7. Tao Zhu CHINA Mozilla Firefox Linux 说道:
    2011年10月27日 6:14 下午

    last 和 next 伤不起啊!

    [回复]

    freetstar CHINA Mozilla Firefox Linux 回复:
    十月 27th, 2011 at 6:23 下午

    @Tao Zhu, 啥意思。。

    [回复]

    Tao Zhu CHINA Mozilla Firefox Linux 回复:
    十月 27th, 2011 at 6:24 下午

    @freetstar, 去翻翻 Perl 的书就 OK

    [回复]

    freetstar CHINA Mozilla Firefox Linux 回复:
    十月 27th, 2011 at 6:25 下午

    @Tao Zhu, 呃。。明白了

    [回复]

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用 * 标注

*

*


疑问 开心 悲伤 邪恶 惊叹 微笑 脸红 笑 惊讶 惊奇 迷惑 酷 憨笑 生气 阴险 转眼球 眨眼 主意 箭头 中立 哭 大笑

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image

点击取消回复
第 1 页,共 1 页1
  • 欢迎订阅

  • Github

  • 近期评论

    • freetstar CHINA Mozilla Firefox Linux 发表在《留言板》
    • _Fly CHINA Google Chrome Linux 发表在《使用e4rat加速你的linux启动速度(ext4文件系统专用)》
    • walfred Mozilla Firefox Windows 发表在《留言板》
    • freetstar CHINA Mozilla Firefox Windows 发表在《用landslide创建html5 slide》
  • Spread Firefox Affiliate Button
  • 我的豆瓣

  • 链接表

    • 51开源社区
    • Adam's
    • delectate's blog
    • Don's
    • fl
    • ghostunix
    • Jasey Wang
    • Jerry's Blog
    • Moolight@Ericyue
    • OSMSG
    • panda
    • Pity is the Bomp
    • PT ubuntu
    • Tao Zhu
    • ubuntuhome
    • WOW!ubuntu
    • zhangzhfang
    • 丕字的blog
    • 傻东の学习笔记
    • 奶牛的blog
    • 孤风颠影
    • 弯柚博客
    • 熊猫党军委主席
    • 靖 轩
    • 马力哥
    • 黑孩儿
  • 我的支付宝

  • 功能

    • 登录
    • 文章 RSS
    • 评论 RSS
    • WordPress.org
© FreeTstar. WordPress 强力驱动 | Nest Theme by YChong