zoukankan      html  css  js  c++  java
  • The while statement

    Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly.

    Iteration is so common, Python provides several language features to make it easier. One is the for statement and another is the while statement.

    More formally, here is the flow of execution for while statement:

    1. Evaluate the condition, yielding True or False.
    2. If the condition is false, exit the while statement and continue execution at the next statement.
    3. If the condition is true, execute the body and then go back to step 1.

    This type of flow is called a loop because the third step loops back around to the top. The body of loop should change the value of one or more variables so that eventually the condition becomes false and the loop terminates. Otherwise the loop will repeat forever, which is called an infinite loop. An endless source of amusement for computer scientists is the observation that the directions on shampoo, “Lather, rinse, repeat” are an infinite loop.      

    In the case of countdown, we can prove that the loop terminates because we know the value of n is finite, and we can see the value of n gets smaller each time through the loop, so eventually we have to get to 0. In other cases, it is not so easy to tell:

    def sequence(n):
        while n != 1:
            print n,
            if n%2 == 0:         # n is even
                n = n/2
            else:                     # n is odd
                n = n*3+1

    The condition for this loop is n!=1, so the loop will continue until n is 1, which makes the condition false. Each time through the loop, the program outputs the value of n and then checks whether it is even or odd. When it is even, n is divided by 2. If it is odd, the value of n is replaced with n*3+1. For example, if the argument passed to sequence is 3, the resulting sequence is 3, 10, 5, 16, 8, 4, 2, 1.

    Since n sometimes increases and sometimes decreases, there is no obvious proof that n will ever reach 1, or the program terminates. For some particular values of n, we can prove termination. For example, if the starting value is a power of two, then the value of n will be even each time through the loop until it reaches 1. The previous example ends with such a sequence, starting with 16.

    The hard question is whether we can prove that this program terminates for all positive values of n. So far, no one has been able to prove it or disprove it.

    from think in Python

  • 相关阅读:
    图像融合之拉普拉斯融合(laplacian blending)
    真实场景的虚拟视点合成(View Synthsis)详解
    真实场景的双目立体匹配(Stereo Matching)获取深度图详解
    OpenCV亚像素角点cornerSubPixel()源代码分析
    OpenCV角点检测goodFeaturesToTrack()源代码分析
    OpenCV角点检测源代码分析(Harris和ShiTomasi角点)
    引导图滤波(Guided Image Filtering)原理以及OpenCV实现
    OpenCV3.4两种立体匹配算法效果对比
    文件操作常用函数
    two Pass方法连通域检测
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3779963.html
Copyright © 2011-2022 走看看