Control Flow Statement in C Programming

Depending upon the requirement of the problem it is often required to change or alter the normal sequence of execution of program. The statement which alter or change the normal sequence of execution of a program is called control statement. It is used to control the flow of a program.

1. if statement

If is a powerful decision making statement that is used to make decision depending upon the test condition written within the “if” expression. The syntax for “if” statement is:
if (condition)
{
statement 1;
}
else
{
Statement x;
}
If the test condition is true then the statement followed by ‘if’ is executed and then the statement ‘x’ is executed .
If the condition is false then ‘if block’ statement is skipped and the statement ‘x’ is executed.

Example :

Check if a number is even or odd.

#include<stdio.h>
int main()
{
  int a;
  printf ("Enter a number\n");
  scanf ("%d",&a);
  
  if(a%2 == 0)
  {
    printf ("The number is even");
  }
  else
  {
    printf("The number is odd");
  }
  return 0;
}

2. Ladder else if statement

When more than logical conditions are to be checked then ladder else if statement can be used. That is, if multipath decision is involved then we used ladder else if statement. Ladder else if statement is the chain of ifs in which each else statement is followed by if.
The syntax for ladder else if statement is:
If (test condition 1)
{
Statement 1

Else if (test condition2)
{
Statement 2
}
Else if (test condtion3)
{
Statement 3
}

Example :

Check the character and flip the case.

#include<stdio.h>
void main()
{
  char ch = 'A';
  if(ch >= 65 && ch<=90)
  {
    ch = ch + 32;
    printf ("The character in lower case is % c",ch);
  }
  else if (ch >=97&& ch <=122)
  {
      ch = ch - 32;
      printf("The character in uppercase is %c",ch);
  }
  else
  {
      printf("Other characters");
  }
}

3. Switch statement

Switch statement is the multinary decision making statement that compares the value of the variable given in the switch statement with the value followed by each case statement. If match occurs then statement block of that case is executed. It is used to select one option out of several options provided when a condition matches. The syntax for switch statement is :

switch(expression or variable)
Case 1: ………………………….
Break;
Case 2: ………………………….
Break;
Case 3: ………………………….
Break;
Default : ………………………….
}

Example : 

Enter the day in number and display its corresponding day using switch statement.

#include<stdio.h>
void main()
{
  int d;
  printf("Enter the day in number \n");
  scanf ("%d",&d);
  switch(d)
  {
    case 1: printf("Today is Sunday\n");
      break;
    case 2: printf("Today is Monday\n");
      break;
    case 3: printf("Today is Tuesday\n");
      break;
    case 4: printf("Today is Wednesday\n");
      break;
    case 5: printf("Today is Thursday\n");
      break;
    case 6: printf("Today is Friday\n");
      break;
    case 7: printf("Today is Saturday\n");
      break;
    default:
    printf("Unexpected value. Please enter a value from 1 to 7.\n");
  }
}

0 Like 0 Dislike 0 Comment Share

Leave a comment