Explain when to use these different types of loops

The For Loop

The For Loop is used to allow the repetition of a specific part of code for a predetermined number of times. At times, the computer is the one that knows how many times to repeat the code. The For Loop, for example, will be used in a code to verify the grade of every student in a specific class. The loop will go from 1 to that number. The For Loop allows the code to run through the loop when the number of times to repeat the problem is known, such as for (var I; I 10; i++);. For this it will repetitively increase i up to when the conditions return false, ten can be replaced by any number. However, the code will quit once the condition cannot be satisfied (Lutz, 2013).

The While Loop

Unlike the For Loops, the While Loops consents for more flexibility, in what is set in the code, and when it should stop, while (I < 10). Consequently, this could be substituted in a Boolean expression for 10 and any other type of variable. Therefore, While Loop is employed when there is need to repeat a particular block of code for an indefinite number of times, up to when a condition is met. For instance, if one wants to request the user for a number between 25 and 35, it is not certain the number of times the user may enter a larger number. Therefore, the code keeps asking “while the number is not between 25 and 35” (Lutz, 2013).

How do you include a 'loop' structure programming in Python?

In python, the while statement is considered as an event-controlled loop statement. The while loop is structured to keep repeating as long as the condition is true. The loop is made up of three parts the initialization, condition, and update. The initialization part involves setting up the variable that will be used in the condition. In the conditioning step, a test is done on the variable to determine whether to terminate the loop or execute the body another time. After each successful complete execution of the loop body, the variable is updated. The figure below is a flow control diagram for while loop;

In python, for loops allow for iterate over sequences directly. The following is an example of a for loop in python that counts from 1-8;

How do you control a loop?

The break statement is used to control a loop. It makes it possible to exit a loop at any point within its body. The use of break statement aids in bypassing the normal termination expression of the loop. Once a break statement is run into within a loop, the loop is instantly concluded, and the program control takes up again at the subsequent statement.

The continue statement could also be used to control a loop. The use of this statement commands the next iteration of the loop to occur. The purpose of continue statement in while loops cause control to shift to the test condition and later carry on to the looping process. When using, For Loop, the use of continue statement results in the continuation of the increment part of the loop. A continue statement is mostly used to resume a statement sequence in case an error takes place (Mobbs, 2018).


Lutz, M. (2013). Learning Python: Powerful Object-Oriented Programming. " O'Reilly Media, Inc.".

Mobbs, R. (2018). Using break and continue Within Loops. Le.ac.uk. Retrieved 3 January 2018, from https://www.le.ac.uk/users/rjm1/cotter/page_38.htm

Deadline is approaching?

Wait no more. Let us write you an essay from scratch

Receive Paper In 3 Hours
Calculate the Price
275 words
First order 15%
Total Price:
$38.07 $38.07
Calculating ellipsis
Hire an expert
This discount is valid only for orders of new customer and with the total more than 25$
This sample could have been used by your fellow student... Get your own unique essay on any topic and submit it by the deadline.

Find Out the Cost of Your Paper

Get Price