Ch. 4 Loops (Python3)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Loop body

1. The loop expression is evaluated when the program reaches the while loop statement. If the loop expression is True then the indented code block, known as the loop body, is executed. 2. At the end of the loop body, execution goes back to the while loop statement again. 3. The loop expression is evaluated again, and if True, the loop body is executed again. But, if the expression evaluates to False, then execution instead proceeds to below the loop body.

Break statement

A break statement in a loop causes an immediate exit of the loop. A break statement can sometimes yield a loop that is easier to understand. (see image)

For Loops

A common programming task is to access all of the elements in a container; for example, printing every item in a list. A for loop statement loops over each element in a container one at a time, assigning the next element to a variable that can then be used in the loop body.

Continue statement

A continue statement in a loop causes an immediate jump to the while or for loop header statement. A continue statement can improve the readability of a loop.

While Loop

A while loop executes a block of code as long as the loop's expression is True.

Infinite Loops

An infinite loop is a loop that will always execute because the loop's expression is always True. A common error is to accidentally create an infinite loop by assuming equality will be reached. Good practice is to include greater-than or less-than along with equality in a loop expression to help avoid infinite loops.

Loop Variable

Commonly, a loop should iterate a specific number of times, such as 10 times. The programmer can use a variable to count the number of iterations, called a loop variable. To iterate N times using an integer loop variable i, a loop with the following form is used: (see image)

Iteration/Looping

Each execution of the loop body is called an iteration, and looping is also called iterating.

Loop

Executes the same code over and over again as long as some condition is true.

Developing programs incrementally

Experienced programmers develop programs incrementally, starting with a simple version of the program, and then growing the program little-by-little into a complete version.

enumerate() function

The enumerate() function retrieves both the index and corresponding element value at the same time, providing a cleaner and more readable solution: The enumerate() function yields a new tuple each iteration of the loop, with the tuple containing the current index and corresponding element value. In the example above, the for loop unpacks the tuple yielded by each iteration of enumerate(origins) into two new variables: "index" and "value". (see image)

Loop else

The loop else construct provides a programmer with the ability to perform some action if the loop completes normally. In the following example, a special message "All names printed" is displayed if the entire list of names is completely iterated through.

Unpacking

Unpacking is a process that performs multiple assignments at once, binding comma-separated names on the left to the elements of a sequence on the right. For example, num1, num2 = [350, 400] is equivalent to the statements num1 = 350 and num2 = 400.

Counting using Range() function

While loops are commonly used for counting a specific number of iterations, and for loops are commonly used to iterate over all elements of a container. The range() function allows counting in for loops as well. range() generates a sequence of numbers, starting at zero and ending before a value given inside the parentheses. For example, for i in range(3) sets i to 0 during the first iteration of the for loop, i to 1 during the second iteration, and finally i to 2 on the third iteration. The value within the parentheses is not included in the generated sequence.


Ensembles d'études connexes

Business Law Chapter 44 "Consumer Law"

View Set

Triangle Congruence: SSS and HL (Assignment)

View Set

MKTG 409 Developing Products Ch.12

View Set

ELA test 3/8/18, Key Terms for Argumentative Essay

View Set

Job 25 - Flashcard MC Questions - Ted Hildebrandt

View Set