What is the loop?
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specification number of times or until a particular condition is satisfied. This repetitive operation is done through a loop control instruction.
There are three methods by way of which we can repeat a part of a program. They are: (a)Using a for statement
- Using a while statement
- Using a do-while statement
Each of these methods is discussed in the following :
While Loop
It is often the case in programming that you want to do something a fixed number of times. Perhaps you want to calculate the gross salaries of ten different persons, or you want to convert temperature from centigrade to Fahrenheit for 15 different cities. The while loop is ideally suited for such cases. Let us look at a simple example, which uses a while loop. The flowchart shown below would help you to understand the operation of the while loop.
Flow chat of While Loop
Syntax:
syntax of while loop in C programming:
while(condition){ statement(s); }
Do-While Loop
The do-while loop is similar to the while loop with one important difference. The body of the
do-while loop is executed at least once. Only then, the test expression is evaluated.
Syntax:
syntax of the do-while loop in C programming:
do{ //code to be executed }while(condition); The do-while loops look like this: do { this; and this; and this; and this; } while(this condition is true);
There is a minor difference between the working of while and do-while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this, the do-while tests the condition after having executed the statements within the loop. The figure would clarify the execution of the do-while loop still further.
This means that do-while would execute its statements at least once, even if the condition fails for the first time. The while, on the other hand, will not execute its statements if the condition fails for the first time. This difference was brought about more clearly by the following program.
#include<stdio.h> void main() { while(4<1); printf("Hello there\n"); }
Here, since the condition is false the first time itself, the print() will not get executed at all.
Let’s now write the same program using a do-while loop
#include<stdio.h> void main() { do { printf("Hello there\n"); } while(4<1); }
In this program, the printf() would be executed once, since first the body of the loop is executed and then the condition is tested.
There are some occasions when we want to execute a loop at least once no matter what. This is illustrated in the following example:
break and continue are used with do-while just as they would be in a while or a for a loop. A
break takes you out of the do-while bypassing the conditional test. A continue sends you straight to the test at the end of the loop.
For Loop
Perhaps one reason why few programmers use while is that they are too busy using the for, which is probably the most popular looping in instruction. The for allows us to specify three things about a loop in a single line:
Setting a loop counter to an initial value.
Testing the loop counter to determine whether its value has reached the number of repetitions desired.
Increasing the value of the loop counter each time the program segment within the loop has been executed.
Syntax
syntax of for loop in C programming:
for(Expression1;Expression2;Expression3){ //code to be executed. }
The general form of for statement is as under:
for(initialise counter;test counter;increment counter)
{
do this; and this; and this;
}
The following figure would help in further clarifying the concept of execution of the for loop.
It is important to note that the initialization, testing, and incrementation parts of a for loop can be replaced by any valid expression. Thus the following for loop is perfectly ok.
for( i=10;i ;i--) printf("%d", i ); for(i<4;j=5;j=0) printf("%d", i ); for(i=1;i<=10;print("%d",i++); for(scanf("%d",&i);i<=10;i++) printf("%d",i);
Let us now write down the program to print numbers from 1 to 10.
#include <stdio.h> void main() { int i; for( i=1 ; i<=10; ) { printf("%d\n",I); i=i+1; } }
Here, the incrementation is done within the body of the for loop and not in the for statement. Note that, in spite of this, the semicolon after the condition is necessary.
SUMMARY
The three types of loops available in C are for, while, and do-while. A break statement takes the execution control out of the loop.
A continue statement skips the execution of the statements after it and takes control to the beginning of the loop.
A do-while loop is used to ensure that the statements within the loop are executed at
least once.
The ++ operator increments the operand by 1, whereas, the –- operator decrements it by 1.
The operators +=,_=,*=,%= are compound assignment operators. They modify the value of the operand to the left of them.
helpful