What are loops?
A Loop executes the sequence of statements many times until the stated condition becomes false. A loop consists of two parts, a body of a loop and a control statement. The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false. The purpose of the loop is to repeat the same code a number of times.
What is While Loop?
A While loop is the most straightforward looping structure. It is an entry-controlled loop. In a while loop, a condition is evaluated before processing a body of the loop. If a condition is true, then and only then the body of a loop is executed. After the body of a loop is executed, the control again goes back to the beginning, and the condition is checked. If it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop. In a while loop, if the condition is not true, then the body of a loop will not be executed, not even once.
What is a Do-While Loop?
A Do-while loop is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop. In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop. Otherwise, the control is transferred out of the loop.
Syntax of While loop in C
Here is a syntax of While loop in C programming: In the while loop, we have to write a condition that needs to be evaluated. The statement inside curly braces indicates the code to be executed.
Syntax Do While Loop in C
Here is a syntax of Do while loop in C programming: In the do-while loop, we need to first write the statement inside curly braces, which indicates the code to be executed. After this, we need to mention Java, C, or C++ program expressions that need to be evaluated.
How While Loop Works?
While loop works as follows:
Flow Chart Explanation: Step 1) Start of while loop Step 2) The test expression or condition is evaluated Step 3) Next, if the test expression is true, the program executes the body of do-while loop Step 4) If the test expression is false, the program outside while loop is executed
How Do-While Loop Works?
The Do-while loop works as follows:
Flow Chart Explanation: Step 1) Start the do-while loop Step 2) The body of do-while loop is executed Step 3) The test expression or condition is evaluated Step 4) If the test expression is true, the compiler executes the body of do-while loop Step 5) Next, if the test expression is false, the compiler executes the statements after the loop body Step 6) Statements that come after the loop body are executed
While vs Do-While Loop: Difference Between Them
Here is an important difference between While and Do While Loop:
While Loop Example in C
Following program illustrates while loop in C programming with an example: Output: The above program illustrates the use of a while loop. In the above code, we have printed a series of numbers from 1 to 4 using a while loop. We have initialized a variable called num with value 1. We are going to print from 1 to 4. Hence the variable is initialized with value 1. If we want to print from 0, then assign the value 0 during initialization. Next, in a while loop, we have provided a condition (num<=4), which means the loop will execute the body until the value of num becomes 4. After that, the loop will be terminated, and control will fall outside the loop. In the body of a loop, we have a print function to print our number and an increment operator to increment the value per execution of a loop. An initial value of num is 1, after the execution, it will become 2, and during the next execution, it will become 3. This process will continue until the value becomes 4, and then it will print the series on the console and terminate the loop.
Do While Loop Example in C
The following program is a Do-while loop example to print a table of number 2 in C: Output: In the above example, we have printed a multiplication table of 2 using a do-while loop. First, we have initialized a variable ‘num’ with the value 1. Then we have written a do-while loop. In a loop, we have a print function that will print the series by multiplying the value of num with 2. After each increment, the value of num will increase by 1, and it will be printed on the screen. Initially, the value of num is 1. In a body of a loop, the print function will be executed in this way: 2num where num=1, then 21=2. Hence the value 2 will be printed. This will go on until the value of num becomes 10. Next, the loop will be terminated, and a statement which is immediately after the loop will be executed. In this case, it will return 0.
Which One Should We Choose?
When checking a condition, if the first iteration is compulsory, we need to use the while loop. It can also be used if the number of iterations is unknown or uncertain. Do while loop mainly requires in the case where we have to execute the loop minimum one time. The do-while loop is typically needed in a menu-driven programming language where the final condition is based upon the end-user.