zoukankan      html  css  js  c++  java
  • Python中的break和continue的使用方法


    一、continue的使用方法(结束当前的循序,进行下一个数的循环)

    # *****************************************************************************
    # This is a program to illustrate the useage of continue in Python.
    # If you want to stop executing the current iteration of the loop and skip ahead to the next 
    # continue statement is what you need. 
    # ****************************************************************************
    for i in range (1,6):
        print
        print 'i=',i,
        print 'Hello,how',
        if i==3:
            continue
        print 'are you today?'
    

    执行结果例如以下:

    >>> ================================ RESTART ======================
    >>>

    i= 1 Hello,how are you today?

    i= 2 Hello,how are you today?

    i= 3 Hello,how
    i= 4 Hello,how are you today?



    i= 5 Hello,how are you today?
    >>> 


    二、break的使用方法(结束总的循环)

    # *****************************************************************************
    # This is a program to illustrate the useage of break in Python.
    # What if we want to jump out of the loop completely—never finish counting, or give up
    # waiting for the end condition? break statement does that.
    # ****************************************************************************
    for i in range (1,6):
        print
        print 'i=',i,
        print 'Hello,what is ',
        if i==3:
            break
        print 'the weather today?'
    
    执行结果例如以下:

    >>> ================================ RESTART ========================
    >>>

    i= 1 Hello,what is  the weather today?

    i= 2 Hello,what is  the weather today?

    i= 3 Hello,what is
    >>>




  • 相关阅读:
    软工_个人项目反(shai)思(zhao)
    软工_结对项目总结博客
    软工_个人博客作业3
    软工_个人博客作业2
    软工_个人博客作业1
    软工_个人项目总结博客
    [转]动态规划
    左式堆 优先级队列类模板 归并排序
    1038 约瑟夫环 循环单链表模拟
    链接表 List
  • 原文地址:https://www.cnblogs.com/llguanli/p/6729367.html
Copyright © 2011-2022 走看看