Control structure in C

In C programming, loops are used to execute a block of code repeatedly as long as a certain condition is met. There are three main types of loops in C:

  1. for loop
  2. while loop
  3. do-while loop


1. for Loop :

The for loop is commonly used when you know beforehand how many times you want to execute a block of code. It consists of three parts: initialization, condition, and increment/decrement.

Example :-

 #include<stdio.h>


int main()
{
int i=0;

for(i=0; i<10; i++)
{
printf("Value of i is =%d\n", i);
}

for(i=10; i>0; i--)
{
printf("Value of i is =%d\n", i);
}
return 0;
}




nested-for Loop :

#include<stdio.h>

int main()
{
int n;
printf("Enter the count number to print table\n");
scanf("%d",&n);

printf("Entered number is =%d\n", n);

for(int i=1; i<=n; i++)
{
for(int j=1; j<=10; j++)
{
printf("%d ", i*j);
}

printf("\n");
}
return 0;
}


2. while Loop :

The while loop is used when you want to execute a block of code as long as a certain condition remains true. It checks the condition before entering the loop.

Example :- 

#include<stdio.h>

int main()
{
int count, i=0;
printf("Enter the loop count\n");
scanf("%d",&count);

while (i<count)
{
printf("\t \t Embedded\n");
i++;
}
printf("Loop Ended\n");

return 0;
}


3. do-while Loop :

The do-while loop is similar to the while loop, but it checks the condition after executing the code block. This ensures that the code block is executed at least once, even if the condition is initially false.

Example -

#include<stdio.h>


int main()
{
int x=0;
do
{
printf("Hello\n");
x++;
} while (x<10);
return 0;
}


4. Switch-Case :

Example 1:-

#include<stdio.h>

int main()
{
    int month, days;
    
    while(1) // To run an infinite loop
    {
    printf("Enter the month (1-12)");
    scanf("%d", &month);


    switch(month)
    {
    case 1:
    days = 30;
    break;
    case 2:
    days = 28;
     break;
    case 3:
    days = 31;
    break;
    case 4:
    days = 30;
    break;
    case 5:
    days = 31;
    break;
    case 6:
    days = 30;
    break;
    case 7:
    days = 31;
    break;
    case 8:
    days = 31;
    break;
    case 9:
    days = 30;
    break;
    case 10:
    days = 31;
    break;
    case 11:
    days = 30;
    break;
    case 12:
    days = 31;
    break;
    }
    
        printf("No of days in %d month is =%d\n", month, days);
    }
    return 0;
 
}


Example 2 :-

#include<stdio.h>

int main()
{
char op;
double a, b;

printf("Please enter the valid operator('+', '-', '*', '/')");
scanf("%c", &op);

printf("Enter the 2 operands");
scanf("%lf %lf", &a, &b);

switch (op)
{
case '+':
printf("%.lf + %.lf =%.lf\n", a, b, a+b);
break;
case '-':
printf("%.lf - %.lf =%.lf\n", a, b, a-b);
break;
case '*':
printf("%.lf * %.lf =%.lf\n", a, b, a*b);
break;
case '/':
printf("%.lf / %.lf =%.lf\n", a, b, a/b);
break;
default:
printf("You entered the wrong operator");
break;
}
return 0;
}



5. IF...ELSE :

#include<stdio.h>

int main()
{
int age;
printf("Enter the valid age\n");
scanf("%d", &age);
if(age < 18)
{
printf("Not Eligible for job\n");
}
else if(age >= 18 && age <=50)
{
printf("Eligible for job\n");
}
else if(age >50 && age <=60)
{
printf("Time to Retire from the job\n");
}
else
{
printf("Retired from the job\n");
}

return 0;
}


Comments

Popular posts from this blog

Setting up a USB thermal printer with a Raspberry Pi 3B+

Autostart an app on reboot - Raspberry Pi

Basic Input/Output C Program