Ad

Control Structure in C

Control structures, or control statements, 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 to which statement is to be executed next. Types of decision control structure:

  1. If: Specifies the condition and executes statements only if the condition is true otherwise, the statement will not be executed.

Syntax:

if (condition)
{
    statement 1;
    statement 2;
    .
    .
    statement n;
}

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

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

Output: 2
Enter Age: 15

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

  1. if-else condition: if-else condition is divided into 2 parts. If the condition is true, the first part will be executed, otherwise, the 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: Rewrite the example 1 using an if-else condition.

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

Output: 2
Enter Age: 15
User is not eligible to vote
Advertisement
  1. Nested if: It is useful when multiple conditions are to be applied. A condition is dependent 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, the first block of statements will be executed, rest of the conditions will not be executed nor checked. If condition 1 is false and condition 2 is true, then the 2nd block of statements will be executed, and the 3rd block will not be checked. If condition 1 and condition 2 are false, then the 3rd block of statements will execute.

Example 3: Input 3 numbers and find the greater number.

#include<stdio.h>
void 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");
        }
    }
}
Enter 3 numbers: 10
20
30
C is greater
Advertisement

C Language Feedback, Questions, Suggestions, Discussion.

Ad: