C Control Statements (Contd.)

Loop Control Structures

Loops are used when we want to repeat a part of our program repeatedly or when we want a repetition structure in our program. Suppose we want to print hello world 10 times on screen. We can do that with printf() function. But, If we wanted to print it 100 times then ? In such situations, loop structures are used.
All loop structures checks a condition, and loop structure will be repeated till the condition remains true.

C Language has 3 loop Statements :

(i) while
(ii) do while
(iii) for

Let's understand each of the above loops in details. Loop structures are really amazing. You can do a lot of fun things with them. For Example - Printing Natural Numbers, Printing Tables, Repeating a program forever etc.

(i) while loop

while loop is really simple, It will only execute when the condition is true. When the condition becomes false, the while loop stops executing and the next statement will be executed.

while loop has the following syntax :
while (condition) {
// statements 
}
Let's understand with an Example -
All Green Colour statements are comments, these are used for explanation only. Compiler ignores comments, thus these are not used in the code.
#include<stdio.h>
void main() {

     int i=1 ;
     /* i is the variable used to test the condition in the while loop */

     while (i<=10) {
     /* The condition is " if 'i' is less than or equal to 10 " */
     /* The above condition will be true for values 1-10 */
     printf("Hello World ! \n");
     i++ ;
     /* The value of i is incremented by 1, it means in the next repetition i will be equals to 2 */
     }

printf("Program Completed");
}
/*  OUTPUT :
     Hello World!
     Hello World!
     Hello World!
     Hello World!
     Hello World!
     Hello World!
     Hello World!
     Hello World!
     Hello World!
     Hello World!
     Program Completed
*/
Program Example :


#include<stdio.h>

void main(){
    int i=1;
    printf("****NUMBERS FROM 1 TO 50****\n\n");
    
    while(i<=50)
    {
        printf("%d\t",i);
        i++ ;
    }
}

We can write such amazing programs with the help of loops. Every loop uses increment or decrement operator to progress through the loop and meet the exit condition. Therefore, we can also include increment/decrement operation in the syntax of any loop. 
For Example - The while loop syntax can be written as :
while (condition) {
// statements 
increment or decrement operation
}

while loop Flowchart

(ii) do - while loop

do - while loop is also same as while loop. The only difference is that do - while loop first executes the loop structure, then it checks the condition. So, We can say that do - while loop is sure to run once even if the condition is false.

do - while loop has the following syntax :
do {
// statements
}while (condition);
Notice the semicolon (;) after the condition in the do while loop.
Let's understand the do - while loop with the help of an Example :
#include<stdio.h>
void main() {
     int i=1 ;
     /* i is the variable used to test the condition in the do - while loop */

     do {
     /* The condition is " if 'i' is less than or equal to 0 " */
     /* The above condition will be false for values 1-10 */
     printf("Hello World ! \n");
     i++ ;
     /* The value of i is incremented by 1, it means in the next repetition i will be equals to 2 */

     }while (i<=0);

printf("Program Completed");
}

/*  Although the condition is false, but do - while loop still runs atleast one time. 
        OUTPUT :
        Hello World!
        Program Completed
*/

do while loop Flowchart

(iii) for loop

This is the most used looping structure available in C Language. It is very useful loop, because this combines the "initialization, condition and increment/decrement" together.

for loop  has the following structure :
for (initialization; condition; update-expression)
{
     // Body of the loop
}
intialization means assigning or providing value to the looping variable. condition defines the checking a given expression, whether it is true or false. "Any looping structure will only execute if and only if it's condition is true". update-expression is the expression which changes the value of the looping variable in the next iteration or repetition.
Notice the semicolons carefully, semicolon (;) are used with initialization and condition expressions but not with update expression.

Let's understands the loop structure with the help of an example.
#include<stdio.h>
void main() {
int i;
/* "i" is the looping variable, will be used to execute the loop */

printf("PRINTING NUMBERS FROM 1 TO 50\n\n");
for (i=1; i<=50; i++)
{
     printf("%d\t",i);
}
/*   OUTPUT :
PRINTING NUMBERS FROM 1 TO 50

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
*/ 
}
Note : You can also declare and initialize the variable in the for loop. In the above Example, you can also do the following :
for (int i=1; i<=50; i++)
{
     printf("%d\t"i);
}

You must remove "int i;" from the above declaration.

We are done with the looping Structures now. Let's visit 2 more statements and then we are done with the Control Statements in C.

<<Previous

Post a Comment