There is another Python statement that can also be used to build an iteration. It is called the The following figure shows
the flow of control. We can use the Here is a new version of the
summation program that uses a while statement. You can almost read the The same program in codelens will allow you to observe the flow of execution. Activity: CodeLens 8.3.3 (ch07_while2) More formally, here is the flow of execution for a
The body consists of all of the statements below the header with the same indentation. This type of flow is called a loop because the third step loops back around to the top. Notice that if the condition is Warning Though
Python’s The body of the loop should change the value of one or more variables so
that eventually the condition becomes In the case shown above, we can prove that the loop terminates because we know that the value of Note Introduction of the while statement causes us to think about the types of iteration we have seen. The What you will notice here is that the So why
have two kinds of loop if Note This workspace is provided for your convenience. You can use this activecode window to try out anything you like. Check your understanding
True or False: You can rewrite any for-loop as a while-loop. The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate? n = 10 answer = 1 while n > 0: answer = answer + n n = n + 1 print(answer) What is printed by this code? n = 1 x = 2 while n < 5: n = n + 1 x = x + 1 n = n + 2 x = x + n print(n, x) You have attempted of activities on this page What type of loop uses a Boolean expression to control the number of times that it repeats a statement or set of statements?A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
What type of loop uses a Boolean expression?The while-loop uses a boolean test expression to control the run of the body lines.
Which type of loop causes a statement or set of statements to repeat as long as a condition is true?A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. A controlled loop contains some repetition condition which will eventually force the looping construct to terminate.
Which loop statement automatically increments the counter at the end of each iteration?The 'for' loop is more compact than the 'while' and 'do' loops and automatically updates the loop counter at the end of each iteration.
|