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: It allows the computer to take a decision as which statement is to be executed next. Types of Decision control structure:

  1. If - It specifies the condition and execute the 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. 283

#include<stdio.h>
#include<conio.h>

void main()
{
    int n;
    printf("Enter Age: ");
    scanf("%d",&n);
    if(n>=18)
    {
        printf("User is eligible for vote");
    }
    getch();
}
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. First part statements will execute if the condition is true and if the condition is false other part of the statements will execute. Both parts of statements will not be executed in either condition. Syntax:
if (condition)
{
    statement 1;
    statement 2;
      .
      .
    statement n;
}	
else
{
    statement 1;
    statement 2;
      .
      .
    statement n;
}

Example 2: Re-write the above program using if-else condition. 198

#include<stdio.h>
#include<conio.h>

void 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");
    }
    getch();
}
Output: 1
Enter Age: 20
User is eligible for vote

Output: 2
Enter Age: 15
User is not eligible for vote
  1. Nested if - It is useful when multiple conditions are to be applied.
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. 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. 229

#include<stdio.h>
#include<conio.h>

void main()
{
    int a, b, c;
    clrscr();
    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");
    }
    getch();
}
Enter 3 numbers: 10
20
30
C is greater