What are Loops? For, While & Do-while Loops in Programming (2024)

In computer Programming, a Loop is used to execute a group of instructions or a block of code multiple times, without writing it repeatedly. The block of code is executed based on a certain condition. Loops are the control structures of a program. Using Loops in computer programs simplifies rather optimizes the process of coding. Kids may come across the question ‘what is Loop’ when they begin to learn computer Programming languages like C or Java. Well, to understand ‘what is Loop’ they will have to learn about the structure of various Loops in detail.

The structure of a Loop can be virtually divided into two parts, namely the control statement, and the body. The control statement of a Loop comprises the conditions that have to be met for the execution of the body of the Loop. For every iteration of the Loop, the conditions in the control statement have to be true. The body of a Loop comprises the block of code or the sequence of logical statements that are to be executed multiple times. There are two types of Loops in Python, namely, For Loop, and While Loop. When a Loop is written within another Loop, the control structure is termed as a nested Loop.

Therefore, when you use a Loop in your program, you will not have to write the block of code (written in the body of the Loop), over and over again in it. The block of code will be executed as many times as the control statement will hold true and the Loop will be terminated when the conditions in the control statement become false. If the conditions are not clearly defined in the control statement, the Loop will keep on executing. Such Loops are termed as infinite Loops. If no termination condition is provided in the control statement of a Loop, then it automatically becomes an infinite Loop.

Below is a detailed discussion of ‘what is Loop’, and the various types of Loops in Python. Hence, going through the below information will help kids to understand the concepts of Loops in computer Programming.

Types of Loops

The concept of ‘what is Loop’ will be clearly understood when you get an idea of the syntax and function of various types of Loops. There are basically two types of Loops in most computer Programming languages, namely, entry controlled Loops and exit controlled Loops.

Entry Controlled Loop

In an entry controlled Loop, the control statement is written right at the beginning of the Loop. This type of Loop is also called a pre-checking Loop. The conditions in the control statements are checked at first, and only if the conditions are true, the body of the Loop is executed. If the condition turns out to be false, the lines of code in the body of the Loop will not be executed.

Exit Controlled Loop

In an exit controlled Loop, the control statement is written at the end of the Loop structure. The lines of codes in the body of the Loop are executed once before the condition is checked. Hence, this type of Loop is also called a post-checking Loop.

FOR Loop is an entry controlled Loop, that is, the control statements are written at the beginning of the Loop structure, whereas, do-while Loop is an exit controlled Loop, that is, the control statements are written at the end of the Loop structure.

For Loops

As discussed above, a For Loop is an entry controlled Loop. The general syntax of a For Loop is given below.

for(initialization; condition; incrementation or decrementation)

{

Body of Loop;

}

The variables required for the control statements can be initialized in the For Loop itself. This variable initialized in the For Loop is called the counter and is incremented or decremented with every iteration of the Loop. The condition is a boolean statement that compares the value of the counter to a fixed value, at every iteration, and terminates the Loop when the condition is not satisfied. The increment or decrement value is set in the Loop.

An example of For Loop is given below.

for(int i=1; i<10; i++)

{

print (i);

}

The above For Loop will print the natural numbers 1 to 10 when executed. The variable ‘i’ is of integer type, and the condition will check if the value of ‘i’ is less than 10, at each iteration. After executing the body of the Loop, the value of ‘i’ is incremented by 1, before the next iteration. In this way, the natural numbers 1 to 10 are displayed on the screen, on executing this Loop.

While Loop

A while Loop is an entry controlled Loop. The condition checking is done at the beginning of the Loop structure. The general syntax of the while Loop is given below.

while(condition)

{

Body of the Loop;

}

The condition checking is done before the execution of the body of the while Loop. The block of code in the body of the While Loop is executed only if the condition is true. The body of the Loop gets executed as many times as the condition is true. After each iteration of the Loop, the control moves back to the condition checking part at the beginning of the While Loop. If the condition is not met, that is, if the boolean expression in the braces (), turns out to be false, the while Loop is terminated.

An example of the While Loop is given below.

int n=10;

while(n>0)

{

print (n);

n--;

}

In the above example, the variable ‘n’ is initialized as an integer, and its value is assigned as 10. Every time the condition ‘n>0’ is met the While Loop will be executed, and the value of n will be displayed on the screen. At every iteration, the value of n will be decreased by 1. The Loop will be terminated when the value of ‘n’ becomes less than 1. The above While Loop will display the numbers from 10 to 1.

Do - While Loop

A do-while Loop is an exit controlled Loop. The syntax of the do-while Loop is similar to that of the while Loop, with the exception of the condition checking. The condition is always checked at the end of the do-while Loop. The general syntax of the do-while Loop is given below.

do

{

Body of the Loop;

} while(condition);

Unlike the entry controlled Loops, the body of the do-while Loop is executed before the condition is checked. Even if the condition is not true, the body of the Loop will be executed for once. If the condition given in the braces () is true, the control is again moved back to the body of the Loop. If the condition is false, the control is moved out of the Loop and the Loop is terminated.

An example of the do-while Loop is given below.

int i=10;

do

{

print (i);

i--;

} while(n>0);

The above do-while Loop will display the numbers 10 to 1 when executed. Now, consider the following example.

int i=0;

do

{

print (i);

i--;

} while(n>0);

The condition ‘n>0’ turns out to be false at the very first iteration of the above-given do-while Loop. Yet, it will be executed once, and 0 will be displayed on the screen. Unlike the other Loops, the do-while Loop ends with the condition checking expression followed by a semicolon ‘;’.

Loops in Python

The commonly used Loops in Python are For and While Loops. The syntax and functions of the For and While Loops in Python are the same as discussed above. When a For Loop or a While Loop is written within another Loop, the structure is called a nested Loop. For example,

for(int i=0; i<7;i++)

{

for (int j=8; j>4; j--)

{

print (i);

print (j);

}

}

The print statements in the above-given nested Loop will be executed only when the conditions in both the Loops are satisfied. Also, if there is only one line of code to be written in the body of a Loop, it is not mandatory to put the brackets for it.

Example:

for(int n=5; n<0; n--)

print (n);

Some of the control statements supported in Python are ‘break’, ‘continue’, and ‘pass’. When a ‘break’ statement is encountered in a Loop, the Loop is terminated immediately, and the control moves to the code followed by the Loop. When a ‘continue’ statement is encountered in a Loop, the control is transferred to the condition checking part and the rest of the code in the body of the Loop is skipped. The ‘pass’ statement in Python is a null statement. It is quite similar to a commented code, however, unlike the commented code, a pass statement is not ignored by the interpreter. For example, if we want to execute a block of code or any function at a later point in time, we can use the ‘pass’ statement for it. The block of code will not be executed when the ‘pass’ statement is executed. The pass statement has a result of no operation when executed.

Since Loops make an important part of Python Programming, kids should learn the concepts of Loops thoroughly, to write advanced programs. Good knowledge of Loops will come in handy when kids will write programs to design fun interactive games in Python as well.

How Do-While Loops are utilized?

Utilize While Loop Statements and Lesson Your Hassle for keeping a check always while performing tasks repetitively.

While Loop statements are used when you have to execute a particular action twice. But here's a point you must ponder on:

Your command won't be executed when the results of the condition which is tested turn out to be false. The Loop of the body will be skipped and the while Loop will only execute the statement after the whole Loop.

When written under a Boolean condition, the while Loop controls the flow statement which permits repetition.

When is the While Loop used? We use a While Loop when We are not sure that why even iteration is Possible?

We use the while Loop until we find a condition that is true. Therefore, take an example of a Buffer Reader which is kept on a reading line from the file. Now, if according to the condition, the file would be really empty, then it will be correct for the while Loop to CHECK FIRST. This is done to avoid the landing of unnecessary exceptions and exception handling.

We hope that this information might have stirred your brain cells and now you might be wanting to learn more about While Loop Statements? If yes, then hit us up and learn the actual usage of While Loop Statements.

Though these are a few examples, when you dive into the field of learning, and once you start with coding, then you will get to know the real usage of While Loop for yourself.

Want to read offline? download full PDF here

Download full PDF

What are Loops? For, While & Do-while Loops in Programming (2024)

FAQs

What are Loops? For, While & Do-while Loops in Programming? ›

A while loop sets the truth of a statement as a necessary condition for the code's execution. A do-while loop provides for the action's ongoing execution until the condition is no longer true. It is possible and sometimes desirable for the condition to always evaluate to be true. This creates an infinite loop.

What is the purpose of while for and do while loops? ›

If you recall the way the for and while loops work, you will remember that these loop types check for the loop condition at the beginning of the loop. Unless the condition is satisfied the loop will not be executed. The do while loop checks the condition at the end of the loop.

What are for loops and while loops? ›

For loop is used to iterate over a sequence of items. While loop is used to repeatedly execute a block of statements while a condition is true. For loops are designed for iterating over a sequence of items.

What is the difference between a while loop and a do-while loop? ›

The main difference between a while and do while is that in do while, the loop will execute once even before checking the condition, whereas in a while loop, the loop executes if the condition is true.

What are loops in programming? ›

In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.

Why would you use a while loop? ›

A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".

Why do we use for loop? ›

A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.

What are the 3 types of loops? ›

Loop Meaning in Programming
Loop TypeUse Case
For loopWhen the number of iterations is known or fixed.
While loopWhen the loop should only be executed if a specific condition holds true.
Do-while loopWhen the loop should be executed at least once and continue as long as the condition is true.

What is a real life example of a while loop? ›

While loop: A real-life example of a while loop might be a vending machine. The vending machine will continue to dispense products (loop) as long as there are items in stock (condition). Once the stock is depleted, the loop will end.

What are the two 2 kinds of loops? ›

Two major types of loops are FOR LOOPS and WHILE LOOPS. A For loop will run a preset number of times whereas a While loop will run a variable number of times.

What is an example of do-while loop? ›

Example 2: do...while loop

The condition is checked only after the first iteration has been executed. do { printf("Enter a number: "); scanf("%lf", &number); sum += number; } while(number != 0.0);

What is the point of a do while? ›

The do... while statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.

Does a while loop always run once? ›

while loop will always execute once, even if the condition is never true. Below we will demonstrate the syntax of the do... while loop. As you can see, the do portion of the loop comes first, and is followed by while (condition) .

How to explain for loop to a kid? ›

For loops repeat a chunk of code for a specified number of times. While loops repeat a chunk of code while some condition is true, stopping when the condition is no longer true. Some coding languages, like Scratch, also have until loops, which are the opposite of while loops; until loops run until a condition is true.

What is a while loop in programming? ›

In most computer programming languages, 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.

Why do coders use loops? ›

Programming Application: When programmers write code, loops allow them to shorten what could be hundreds of lines of code to just a few. This allows them to write the code once and repeat it as many times as needed, making it more likely for the program to run as expected.

What is the use of while and do-while loop in C? ›

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

What is the purpose of using while? ›

We can use while or as to talk about two longer events or activities happening at the same time. We can use either simple or continuous verb forms: We spent long evenings talking in my sitting-room while he played the music he had chosen and explained his ideas.

What is the advantage of do-while loop? ›

Some of the advantages of using the do while loop are: The structure of a do-while loop is simple and easy to understand. The code inside a do while loop is always executed at least once, regardless of the condition. The code inside a do while loop will continue to execute as long as the condition is True.

What is the purpose of the do-while loop in C++? ›

Like other loops, the do-while loop in C++ is used to loop through a code until the given condition is satisfied. It means that the loop will only terminate if it evaluates the specified condition to be false. This can come in handy when you want to run a piece of code multiple times.

Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5346

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.