Loops in C Language

Loops in programming are essential tools for automating repetitive tasks, simplifying code, and boosting efficiency in various applications.

Loops in C Language
Loops in C Language
Loops

Loops is statement. Which is use for to reduce the code and also reduce the run time of the code. If we have sequentially statement then use a loop.

Types of loop statement

  1. The while loop statement 
  2. The do while loop statement 
  3. The for loop statement 


The While loop statement

It is a simplest loop structure in C. 

Format of while loop:

While ( test condition)

// body of the loop
}

How to execute the while loop statement 

In this loop statement the compiler test the condition if condition is true then execute. After execution once again evaluate the code . This process repeated Until the condition is true. If condition is false then loops stop and show result.

Example:-

#include<stdio.h>
 int main()
       {
    int i=0;                          // Intilization 
    while(i<5)                    // Testing 
                            {
        printf(" Hello readers! ");
        i++;                           // Increment
    };  
    return 0;
  }


Result:- Hello readers! Hello readers! Hello readers! Hello readers! Hello readers!


The do while loop statement

It is a similar to while loop.

Format of do while loop:

do
{
// Body of the loop
}
While (test condition);

How to execute the do while loop statement 

In this loop statement the compiler execute before test of the condition. then test the condition, if condition satisfied then execute. This process repeated until the condition is true. If condition is false then loops stop and show result.

Example:-

#include<stdio.h>
int main()
{
    int i=0;
    do
    {
    printf(" Hello readers! ");
        i++;
    }
    while(i<5);
        return 0;
}


Result:- Hello readers! Hello readers! Hello readers! Hello readers! Hello readers!


The for loop statement

Most useful loop in the C program. It is another entry controlled loop that's provide a more concise loop control structure.

Format to for loop statement 

For(Initialization; test condition ; increment/decrement )
 {
   // Body of the loop
}

How to execute for loop statement 

In this loop statement the compiler firstly test the condition of condition is true then execute until the condition is true of condition false then stop the loop and show result.

Example:-

#include<stdio.h>
int main()
{
    int i;
    for(i=0;i<5;i++)
    {
    printf(" Hello readers! ");
    };
    return 0;
}

Result:- Hello readers! Hello readers! Hello readers! Hello readers! Hello readers!


The Switch Statement

It is a multi way decision making statement that choose the statement block to be executed by matching the given value with a list of the case value.

Format of switch statement
Switch(expression) { 
case 1; 
// Body 
break;
case 2; 
// Body
break; 
default 
}
Example:-
// addition and subtraction of number using switch

#include<stdio.h>
int main()

int a,b,sum;
char A;
printf("enter first number: ");
scanf("%d",&a);

printf("enter oprator'(+/-)': ");
scanf(" %c",&A);

printf("enter second number: ");
scanf("%d",&b);

switch(A){
case '+':
      sum = a+b;
      printf("%d",sum);
      break;

 case '-':
     sum = a-b;
     printf("%d",sum);
     break;
  default:
  printf("error");

}
    return 0;
}

Post a Comment