Loop control statement

Introduction of loop 

Loop are used when we want to execute a block of statement or a part of program at multiple times. suppose a program to print " hello world " 20 times. one way to get the desired output using printf statements, which is bad programming skill or not preferable . other way we can use loop to print the "hello world " easily.

  Now I can demonstrate to you print " hello world " without using loop.

/*Program to print "hello world" without using loop*/
#include <stdio.h>
int main()
{
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    printf("hello world\n");
    return 0;
}

😃output:
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

In the above program we want to use 20 printf statement when the program have very messy . let now we write above program using loop.


/*Program to print "hello world" using loop*/
#include <stdio.h>
int main()
{
    int i=1;
    while(i<=20)
    {
        printf("Hello world!\n");
        i++;
    }
    return 0;
}

😃output 
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!

using loop we can write one loop statement and only printf statement , and this approach is definitely better than first one , with the help of  loop we can execute a part of the program repeatedly till some expression is true.

Types of  loop


Entry Control loop :  In entry controlled loop  first check the condition then after execute  the body of loop like for and while.

Exit Control loop : In exit controlled loop we can check the condition after the body of the loop is execute. this types of loop execute at least once irrespective condition is true or false. do while loop is exit control loop.

While loop:

while loop is like if-else statement here also we can have either a single statement or a block of statement and here it is know as body of loop. In while loop first the expression is check , if it is true  then the statements in the body of loop is executed. After the execution ,again the expression is checked.

First initialize, then condition checks, and then executes the body and updating can be inside the body.

syntax:

😃initialization_expression
while(expression)
{
     // body of loop
    updation;
}


Example: program to print the sum of digits of any number.

/*Program to print th sum of digits of any number using while loop*/
#include <stdio.h>

int main()
{
    int num, sum = 0, rem;
    printf("Enter a number:");
    scanf("%d", &num);
    int n=num;
    while (num > 0)
    {
        rem = num % 10;  // taking last digit
        sum = sum + rem; // sum of digits
        num = num / 10;  // skip last digit
    }
    printf("\nSum of digits of %d is %d",n,sum);
}

Output :
Enter a number:2543
Sum of digits of 2543 is 14

do while loop

It is similar to while loop but here first the loop body is executed and then the expression is evaluated. If the expression is true , then the loop body is executed and this process continues as long as the expression is true. when the expression is false loop terminates.

do while first executes the body and then the condition check is done.

syntax:

do
{
statement
statement
...............
}while (expression);

Note : that unlike while loop,here a semicolon is placed after the parentheses containing the expression.



example : program to print the number from 1 to 10 using do while loop

/*Program to print the number from 1 to 10 using do while loop*/
#include <stdio.h>

int main()
{
    int i=1;
    do
    {
        printf("%d\t",i);
        i=i+1;
    } while (i<=10);
    return 0;
}

😃Output:
1       2       3       4       5       6       7       8       9       10

for loop

for statement has three expression, semicolons are used for separating these expressions. 

syntax:
for( expression1; expression2; expression3)
{
    statement
    statement
    ...............
}

expression1 is initialization expression it is executed only once when the loop start and is used to initialize the loop variable. 

expression2 is a test expression or condition . It is test before each iteration of  loop. 

expression3 is update expression and is execute each time after the body of loop is executed.

 

Example: program to print numbers in reverse order 

/*Program to print the numbers in reverse order*/
#include <stdio.h>

int main()
{
    int i;
    for (i = 10 /*initialization*/; i >= 1 /*condition*/; i-- /*update*/)
    {
        printf("%d\t", i);
    }
    return 0;
}

😃Output:
10      9       8       7       6       5       4       3       2       1    

nesting of  loop

When a loop is written insid another loop is known nesting of loop. Any type of loop can be nested inside any other type of loop. for example a for loop may be nested inside another  for loop or inside a while or do-while loop.

syntax: nesting of for loops

for ( initialization; condition; update ) 
{
    for ( initialization; condition; update )
     {
     	// statement of inside loop
     }
    // statement of outer loop
}

Or

syntax: nesting of while loops

while(condition)
    {
       while(condition) {
      
      // statement of inside loop
   }

   // statement of outer loop
}

Or

syntax: nesting of do-while loops

do{

   do{
      
      // statement of inside loop
   }while(condition);

   // statement of outer loop
}while(condition);

Note: There is no rule that a loop must be nested inside its own type.

   while(condition) {
      
      for ( initialization; condition; update ) 
      {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }
   

flow chat of nesting of for loop

Nesting of  for  loop

program to demonstrate nesting of loops 

/*Program to print the cartesian product of two sets */
/*suppose Set A = {1,2,3} and set B = {4,5,6} now find Cartesian product*/
#include <stdio.h>

int main()
{
  int arr_a[3]={1,2,3};
  int arr_b[3]={4,5,6};
    /* now find the Cartesian product*/
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            printf("(%d,%d)\t",arr_a[i],arr_b[j]);
        }
    }
    return 0;
}

😃Output :
(1,4)   (1,5)   (1,6)   (2,4)   (2,5)   (2,6)   (3,4)   (3,5)   (3,6)

infinite loop

The loops that go on executing infinitely and never terminate are called infinite loops. These  loop are only stopped by ending the program.

for example : infinite loop using for loop

/*Program to print a statement infinite times using for loop */
#include <stdio.h>

int main()
{
    int i;
    printf("print a statement at infinite times \n");
    for ( ; ; )
    {
        printf("Welcome to Education ki duniya!\n");
    }
    return 0;
}

 ðŸ˜ƒoutput:
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
.................................... soon

infinite loop using while loop

/*Program to print a statement infinite times using While loop */
#include <stdio.h>

int main()
{
    int i;
    printf("print a statement at infinite times \n");
    while(1) // 1 means non-zero value i.e always true
    {
        printf("Welcome to Education ki duniya!\n");
    }
    return 0;
}

😃output:
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
.................................... soon

 infinite loop using do - while loop

/*Program to print a statement infinite times using do-While loop */
#include <stdio.h>

int main()
{
    int i;
    printf("print a statement at infinite times \n");
    do
    {
        printf("Welcome to Education ki duniya!\n");
    } while (1);
    return 0;
}

😃output:
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
.................................... soon

odd loop

The loops is used so far executed the statements within them a finite number of times. However ,at times one comes across a situation when  a user may not know about how many times a loop is executed a loop for unknown number of times, then the concept of odd loop should be used.

for example :

/*Program to print a statement when user is agree to execute body of loop */
#include <stdio.h>

int main()
{
    char ch;
    ch='y';
    while(ch=='y')
    {
        printf("Hello Duniya!\n");
        printf("Do you want to continue (y/n):");
        scanf(" %c",&ch); /*here be use odd loop for execute the body of loop again or not*/
    }
    return 0;
}

😃output:
Hello Duniya!
Do you want to continue (y/n):y
Hello Duniya!
Do you want to continue (y/n):y
Hello Duniya!
Do you want to continue (y/n):y
Hello Duniya!
Do you want to continue (y/n):n

Difference between while loop and do-while loop

while loop do-while loop
1.Condition is checked first then statements is executed. 1.Statements is executed atleast once, thereafter condition is checked.
2.There is no output when condition is false. 2.There is at least one output when condition is false.
3.while loop is entry controlled loop. 3.do-while loop is exit controlled loop.
4.No semicolon at the end of while. while(condition) 4.Semicolon at the end of while. while(condition);
       syntax:
       	while(condition)
        {
        	statements;
         }
         
       
       syntax:
       	do
        {
        	statements;
         }while(condition);
         
       

Comments

Popular posts from this blog

18 Amazing programs in C.