LOOP CONTROL INSTRUCTIONS IN C

LOOP CONTROL INSTRUCTIONS IN C

In this article we will discuss about LOOP CONTROL INSTRUCTIONS IN C. So why loops?. Sometimes we want our program to execute a few set of instructions. Over and over again. For example printing 1 to 100. First even 100 numbers. Hence LOOP CONTROL INSTRUCTIONS IN C make it easy. For a programmer . To tell the compiler that a given set of instructions must be executed repeatedly.

TYPES OF LOOP CONTROL INSTRUCTIONS:
  1. While loop
  2. Do-while loop
  3. For loop
WHILE LOOP:
Syntax: while ( condition is true){
//code
//code
}
For example: int i=10;
while (i< 10){
printf("the value of i is %d" , i):
i++;
}

*NOTE: If the condition never become fails. The while loops keep getting execute. Such a loop is call as an infinite loop.

INCREMENT AND DECREMENT OPERATOR:

I++ ( I is increment by 1). I– ( I is decrement by 1). Printf(“–i=%d” , –i);. This first decrements i then prints it . It is pre decrement operator. Similarly , (“i–=%d” , i–);. This first prints i then decrements it. This is call post decrement operator. Similarly, for incrementation.

DO-WHILE LOOP
Syntax: do{
//code;
//code;} while(condition);

Do- while loop is a while loop which executes at least once. Do-while loops works similarly as while loop. But Do-while loop at least execute once. whether the conditions fails.

FOR LOOPS:
Syntax: for(initialization; condition; increment or decrement){
//code;
//code;
}
Initialization:- setting a loop  counter to an initial value.
Test/condition:- checking the condition
increment:- updating the loop counter. 
For example: for (i=0 ; i<3;i++){
printf ("%d",i);
printf("\n");}
THE BREAK STATEMENT IN C:

The break statement in c is use to exit the loop. Irrespective of whether the condition is true or false. Whenever a “break” is encountered inside the loop. The control sent outside the loop.

THE CONTINUE STATEMENT:

The continue statement in c use to immediately move to the next of the loop. The control is taken to the next iteration. Of the loop. The control is taken to the next iteration thus skipping everything . Below inside the loop for that iteration.

*NOTES:- sometimes , the names of the variable might not indicate the behaviour of program. Break statement completely exits the loop. Continue statement skips that particular iteration of a loop.

for Conditional instructions