C - Control Structure

Control structure or control statement hold the execution of a program by any condition or decision. Control structure specifies the order in which the various instructions in a program are to be executed by the computer. Control structure determines the flow of control in a program.

There are 3 types of control structures or control statements:

  1. Decision Control Structure: if, switch
  2. Looping Control Structure: for, while, do while
  3. Jumping Control Structure: break, continue, goto

Decision Control Structure

Decision Control Structure allows the computer to take a decision as which statement is to be executed next. Types of Decision control structure:

  1. If: Specifies the condition and execute statements only if the condition is true otherwise statement will not be executed.
if (condition)
{
    statement 1;
    statement 2;
    .
    .
    statement n;
}

Example 1: Input user's age and check it is eligible for vote or not.

#include<stdio.h>
int main()
{
    int n;
    printf("Enter Age: ");
    scanf("%d",&n);
    if(n>=18)
    {
        printf("User is eligible for vote");
    }
    return 0;
}
Output: 1
Enter Age: 20
User is eligible for vote

Output: 2
Enter Age: 15

In Output 1, user entered value 20 which is greater than 18, so the condition is true, the message will print 'User is eligible for vote'. In Output 2, user entered value 15, makes the condition false, no message will be printed. If you want to print a message when the condition is false, than use if-else condition.

  1. if–else condition: if else condition is divided into 2 parts. If the condition is true first part will be executed, otherwise second part will be executed. Both parts of statements will not be executed in either condition. Syntax:
if (condition)
{
    //execute if condition is true
    statement 1;
    statement 2;
      .
      .
    statement n;
}	
else
{
    //execute if condition is false
    statement 1;
    statement 2;
      .
      .
    statement n;
}

Example 2: Re-write the example 1 using if-else condition.

#include<stdio.h>
int main()
{
    int n;
    printf("Enter Age:");
    scanf("%d",&n);
    if(n>=18)
    {
        printf("User is eligible for vote");
    }
    else
    {
        printf("User is not eligible for vote");
    }
    return 0;
}
Output: 1
Enter Age: 20
User is eligible for vote

Output: 2
Enter Age: 15
User is not eligible for vote
Advertisement
  1. Nested if: It is useful when multiple conditions are to be applied. A condition is depend on another condition.
if (condition 1)
{
    statement 1;
}
else
{
    if (condition 2)
    {
        statement 2;
    }
    else
    {
        statement 3;
    }
}

In the above example, if condition 1 is true first block of statements will be executed, rest of the condition will not executed nor checked. If condition 1 is false and condition 2 is true, than 2nd block of Statements will be executed, 3rd block will not be checked. If the condition 1 and condition 2 is false than 3rd block of statements will execute.

Example 3: Input 3 numbers and find greater number.

#include<stdio.h>
int main()
{
    int a, b, c;
    printf("Enter 3 numbers:");
    scanf("%d%d%d",&a,&b,&c);
   
    if(a>b && a>c)
    {
        printf("A is greater");
    }
    else
    {
        if(b>a && b>c)
        {
            printf("B is greater");
        }
        else
        {
            printf("C is greater");
        }
    }
    return 0;
}
Enter 3 numbers: 10
20
30
C is greater
Advertisement