What structure contains a block of statements that is executed repeatedly?

The flow of the programs written in any programming language is sequential by default. The first statement in a function is executed first, followed by the second, and so on. There may be a situation when the programmer needs to execute a block of code several times. For this purpose, The programming languages provide various kinds of loops that are able to repeat some particular code numerous numbers of times. Here, we are going to talk about looping statements in Python.

In a programming language, a looping statement contains instructions that continually repeat until a certain condition is reached. Read on to find out more about them.

Table of Content

  • Looping statements in Python
  • For Loop
  • While Loop
  • Nested Loop

Looping statements in Python

Looping simplifies complicated problems into smooth ones. It allows programmers to modify the flow of the program so that rather than writing the same code, again and again, programmers are able to repeat the code a finite number of times.

In Python, there are three different types of loops: for loop, while loop, and nested loop.
Here, we will read about these different types of loops and how to use them.

For Loop

The for loop is used in the case where a programmer needs to execute a part of the code until the given condition is satisfied. The for loop is also called a pre-tested loop. It is best to use for loop if the number of iterations is known in advance.

In Python, there is no C style for loop, i.e., for (i=0; i

Syntax:

for variable in sequence:
    statements(s)

Input:

a = 5
for i in range(0, a):
    print(i)

Output:

0
1
2
3
4

The for loop runs till the value of i is less than a. As the value of i is 5, the loop ends.


While Loop

The while loop is to be used in situations where the number of iterations is unknown at first. The block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop.

In Python, the while loop executes the statement or group of statements repeatedly while the given condition is True. And when the condition becomes false, the loop ends and moves to the next statement after the loop.

Syntax:

While condition:
       statement(s)

Input:

count = 0
while (count < 5):   
    count = count + 1
    print("Flexiple")

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple

The loop prints ‘Flexiple’ till the value of count becomes 5 and the condition is False.


Nested Looping statements in Python

The Python programming language allows programmers to use one looping statement inside another looping statement.

Syntax:

#for loop statement
for variable in sequence:
    for variable in sequence:
       statement(s)
       statement(s)

#while loop statement
while condition:
    while condition:
       statement(s)
       statement(s)

Input:

for i in range(1, 7):
    for j in range(i):
         print(i, end=' ')
    print()

Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

Closing Thoughts

In this tutorial, we read about different looping statements in Python and their uses. The looping statements are used to repeat a specific block of code various number of times. One can read about other Python concepts here.

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.

Discussion

Introduction to Test Before Loops

There are two commonly used test before loops in the iteration (or repetition) category of control structures. They are: while and for. This module covers the: while.

Understanding Iteration in General – while

The concept of iteration is connected to possibly wanting to repeat an action. Like all control structures we ask a question to control the execution of the loop. The term loop comes from the circular looping motion that occurs when using flowcharting. The basic form of the while loop is as follows:

initialization of the flag 
while the answer to the question is true then do
    some statements or action
    some statements or action
    some statements or action
    update the flag

In most programming languages the question (called a test expression) is a Boolean expression. The Boolean data type has two values – true and false. Let’s rewrite the structure to consider this:

initialization of the flag 
while the expression is true then do
    some statements or action
    some statements or action
    some statements or action
    update the flag

Within the while control structure there are four attributes to a properly working loop. They are:

  • Initializing the flag
  • Test expression
  • Action or actions
  • Update of the flag

The initialization of the flag is not technically part of the control structure, but a necessary item to occur before the loop is started. The English phrasing is, “While the expression is true, do the following actions”. This is looping on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test before loop the action might not happen. It is called a test before loop because the test comes before the action. It is also sometimes called a pre-test loop, meaning the test is pre (or Latin for before) the action and update.

Human Example of the while Loop

Consider the following one-way conversation from a mother to her child.

Child: The child says nothing, but mother knows the child had Cheerios for breakfast and history tells us that the child most likely spilled some Cheerios on the floor.

Mother says: “While it is true that you see (As long as you can see) a Cheerio on the floor, pick it up and put it in the garbage.”

Note: All of the elements are present to determine the action (or flow) that the child will be doing (in this case repeating). Because the question (can you see a Cheerios) has only two possible answers (true or false) the action will continue while there are Cheerios on the floor. Either the child 1) never picks up a Cheerio because they never spilled any or 2) picks up a Cheerio and keeps picking up Cheerios one at a time while he can see a Cheerio on the floor (that is until they are all picked up).

Infinite Loops

At this point, it is worth mentioning that good programming always provides for a method to ensure that the loop question will eventually be false so that the loop will stop executing and the program continues with the next line of code.  However, if this does not happen, then the program is in an infinite loop.  Infinite loops are a bad thing. Consider the following code:

Pseudocode infinite loop

loop_response = 'y'
While loop_response == 'y'
    Output "What is your age? "
    Input user_age
    Output "What is your friend's age? "
    Input friend_age
    Output "Together your ages add up to: "
    Output user_age + friend_age

The programmer assigned a value to the flag before the loop which is correct. However, they forgot to update the flag. Every time the test expression is asked it will always be true. Thus, an infinite loop because the programmer did not provide a way to exit the loop (he forgot to update the flag). Consider the following code:

loop_response = 'y';
While loop_response = 'y'
    Output "What is your age? "
    Input user_age
    Output "What is your friend's age? "
    Input friend_age
    Output "Together your ages add up to: "
    Output user_age + friend_age
    Output "Do you want to try again? y or n "
    Input loop_response

No matter what the user replies during the flag update, the test expression does not do a relational comparison but does an assignment. It assigns ‘y’ to the variable and asks if ‘y’ is true? Since all non-zero values are treated as representing true, the answer to the test expression is true. Viola, you have an infinite loop.

Counting Loops

The examples above are for an event controlled loop. The flag updating is an event where someone decides if they want the loop to execute again. Often the initialization sets the flag so that the loop will execute at least once.

Another common usage of the while loop is as a counting loop. Consider:

counter = 0
While counter < 5
    Output "I love ice cream!"
    counter += 1

The variable counter is said to be controlling the loop.  It is set to zero (called initialization) before entering the while loop structure and as long as it is less than 5 (five); the loop action will be executed.  But part of the loop action uses the increment operator to increase counter’s value by one.  After executing the loop five times (once for counter’s values of: 0, 1, 2, 3 and 4) the expression will be false and the next line of code in the program will execute. A counting loop is designed to execute the action (which could be more than one statement) a set of given number of times. In our example, the message is displayed five times on the monitor. It is accomplished by making sure all four attributes of the while control structure are present and working properly. The attributes are:

  • Initializing the flag
  • Test expression
  • Action or actions
  • Update of the flag

Missing an attribute might cause an infinite loop or give undesired results (does not work properly).

Infinite Loops

Consider:

counter = 0;
while counter < 5
    Output "I love ice cream!"

Missing the flag update usually causes an infinite loop.

Variations on Counting

In the following example, the integer variable age is said to be controlling the loop (that is the flag). We can assume that age has a value provided earlier in the program. Because the while structure is a test before loop; it is possible that the person’s age is 0 (zero) and the first time we test the expression it will be false and the action part of the loop would never be executed.

While 0 < age
    Output "I love candy!"
    age -= 1

Consider the following variation assuming that age and counter are both integer data type and that age has a value:

counter = 0;
While counter < age
    Output "I love corn chips!"
    counter += 1

This loop is a counting loop similar to our first counting loop example. The only difference is instead of using a literal constant (in other words 5) in our expression, we used the variable age (and thus the value stored in age) to determine how many times to execute the loop. However, unlike our first counting loop example which will always execute exactly 5 times; it is possible that the person’s age is 0 (zero) and the first time we test the expression it will be false and the action part of the loop would never be executed.

What is a structure that allows repeated execution of a block of statements?

A loop is a structure that allows repeated execution of a block of statements. Within a looping structure, a Boolean expression is evaluated.

What is the structure that causes a statement or a set of statements to execute repeatedly?

A repetition structure causes a statement or set of statements to execute repeatedly. Repetition structures are used to perform the same task over and over. A condition-controlled loop uses a Boolean (true/false) condition to control the number of times that it repeats.

Which C++ structure

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 structure causes a statement or set of statements to execute repeatedly quizlet?

More commonly known as a loop, a repetition structure causes a statement or set of statements to execute repeatedly as many times as necessary. What is a condition-controlled loop? A condition-controlled loop causes a statement or set of statements to repeat as long as a condition is true.