Decision-making in C


August 23, 2021, Learn eTutorial
1478

In this c tutorial, you will master everything about Decision making statements like if, if-else, if-else if, nested if, etc. Also, you will learn how to use the switch and nested if in C with the help of examples.

Almost every algorithm in a program that involves logical operation must pass through a decision-making architecture. ALU, the Arithmetic and Logic Unit of a computer, supervises the execution of the decisions making the statement through logical gates like AND, OR, NOT, NAND, etc. The simplest form of decision-making statement is 'do this if you find that, otherwise do this'. In the coming section, we will learn how to use it in c programming.

if Statement in C:

'if' is an important keyword that tells the compiler to look for some conditions or criteria. If the data satisfy the given conditions a specific block of codes will be executed. In case the provided criteria do not match, it switches the execution control to another predetermined block of codes. 

The prototype of the if statement  in C


if (condition)
{
#body of if ;
}
 
THE FLOWCHART OF IF STATEMENT

THE FLOWCHART OF IF STATEMENT

Suppose, if a number entered by the user is '0', you want to print 'It is Zero'. The code for this will be like :

#include<stdio.h>
void main()
{
    int num;
    printf("Enter a number:");
    scanf("%d",&num);
    if(num==0)
    {
        printf("Entered Number is zero\n");
    }
    printf("Exit");
}

Outputs:

Enter a number:0
Entered Number is zero
Exit
Enter a number:2
Exit

So, you must have noticed that the condition is written immediately after 'if' within the parentheses () and the block of codes to be executed immediately when the condition is met is written within the braces '{}'.

The downside of a single if statement is that it only executes the statement following the truth value, the True and the False case is not addressed.

if..else Statement :

'else' is another useful keyword used only with the 'if' keyword. When the compiler finds 'else' it will execute the block of codes written next to it only when the condition is not satisfied. The prototype 

The prototype of the if-else statement  in C

if (condition)
{
    #body of if ;
}
else
{
    #body of else ;
}
 
THE FLOWCHART OF IF STATEMENT

THE FLOWCHART OF IF STATEMENT

Here is a simple program demonstrating the use of If..else prototype.

#include<stdio.h>
void main()
{
    int num;
    printf("Enter a number:");
    scanf("%d",#);
    if(num==0)
    {
        printf("Entered Number is zero\n");
    }
    else
    {
        printf("Entered Number is not zero\n");
    }

    printf("Exit");
}


Output 1:
Enter a number:0
Entered Number is zero
Exit

Output 2:
Enter a number:2
Entered Number is zero
Exit

if.. else..if.. else

Till now we have dealt with two separate blocks of codes. But many times you will encounter problems where multiple sets of codes are needed, and they have to be executed according to the data input.

The prototype of the if.. else..if.. else statement  in C


if (condition 1)
{
    #body of if ;
}
else if(condition 2)
{
    #body of elseif ;
}
else
{
    #body of else ;
}
 
THE FLOWCHART OF IF STATEMENT

FLOWCHART OF IF ELSE IF ELSE

Suppose we are comparing two numbers a and b. If we want to compare them,  three different cases arise a>b, a=b, and a<b. Here the concept of  'if.. else if.. else' comes into play. We can channelize the code execution by putting a>b, a<b, and a=b in else, else if, and else statement respectively. In the following example it has been illustrated:

#include<stdio.h>
void main()
{
    int a,b;
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b);
    if(a>b)
    {
        printf("\n%d is greater than %d",a,b);
    }
    else if(a<b)
    {
       printf("\n%d is less than %d",a,b);
    }
    else 
    {
       printf("\nTwo numbers are equal");
    }

}
<stdio.h>


Output 1:
Enter the first number 8
Enter the second number 5
8 is greater than 5

Output 1:
Enter the first number 5
Enter the second number 8
5 is less than 8

Output 2:
Enter the first number 5
Enter the second number 5
Two numbers are equal

NESTED if:

Instead of using if-else structure we can use an if-else statement inside the block of another if. In case of long and complicated programs, this architecture is always preferred by the programmers.

Syntax of Nested if


if (condition)
{
#body of if ;
}
 
THE FLOWCHART OF NESTED IF STATEMENT

THE FLOWCHART OF NESTED IF STATEMENT

The previous program has been rewritten using 'nested if’:


#include<stdio.h>
void main()
{
    int a,b;
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b);
    if(a!=b)
    {
       if(a>b)
        printf("\n%d is greater than %d",a,b);
      else 
       printf("\n%d is less than %d",a,b);
    }  
    else 
       printf("\nTwo numbers are equal");

}


Output 1:
Enter the first number 8
Enter the second number 5
8 is greater than 5

Output 2:
Enter the first number 5
Enter the second number 8
5 is less than 8

Output 3:
Enter the first number 5
Enter the second number 5
Two numbers are equal

in the above snipped initially the condition of non equality is evaluated. If it evaluates to True then control shifts to the nested if where it compares the greatest of two values otherwise it will execute the else part.

SWITCH Statement in C

As the name suggests, it does the job of switching the execution flow to the required section of codes. The coding should be written exclusively for each different 'case' followed by a 'break' statement which gives control to the next layer of coding. If you want to run another block of codes when none of the defined 'case's is valid, you can write it under 'default'.

The Syntax of the switch statement  in C


switch(Expression)
{
  Case 1: Statement(s);
                 Break;
  Case 2: Statement(s);
                 break;
  Case 3: Statement(s);
                 break;
                   .
                   .
                   .

  default: Statement(s);
                 break;
}

 
THE FLOWCHART OF SWITCH STATEMENT

THE FLOWCHART OF SWITCH STATEMENT

Check out the following program


#include<stdio.h>
void main()
{
    int day;
    printf("Enter the Day: ");
    scanf("%d",&day);
    
    switch(day)
    {
        case 0: printf("Today is Sunday\n");
               break;
        case 1: printf("Today is Monday\n");
               break;
        case 2: printf("Today is Tuesday\n");
               break;
        case 3: printf("Today is Wednesday\n");
               break;
        case 4: printf("Today is Thursday\n");
               break;
        case 5: printf("Today is Friday\n");
               break;
        case 6: printf("Today is Saturday\n");
               break;
       default: printf("Invalid Day");

    }

}

Output 1:
Enter the Day:3
Today is Wednesday

Output 2:
Enter the Day:5
Today is Friday

Output 3:
Enter the Day:10
Invalid Day

In this program, if you enter any number other than 0 to 7, the codes under 'default' will execute showing 'Invalid Day'.

NESTED SWITCH Statement

Nested switch statements are symmetrical with nested if. It allows the programmer to define two or multiple separate cases under a single case. The syntax for this is as follows:


switch (variable)
{
    case 1:
        statement;
        switch (variable_1)
        {
            statement; 
        }
        break;
    
    case 2:
        statement;
        switch (variable_2)
        {
            statement;
        }
        break;

    default:
        statement;       
        switch (variable_x)
        {
            statement;
        }
        break;
}

The ? Operator

The symbol '?' works in C as a boolean operator combining if-else statements in a single line. It works with three operands. Here an odd-even program can be written using '?'.


#include<stdio.h>
void main()
{
    int n,a;
    printf("Enter a number:");
    scanf("%d",&n);
    a = ((n%2==0)?0:1);
        if(a==0)
                 printf("\nEven");
        else
                  printf("\nOdd");

}
 

Output 1:
Enter a number:6
Even

Output 2:
Enter a number:5
Odd

You can easily understand that a single line a= ((n%2==0)? 1:0) Implies,

if (n%2==0) a=1;

else a=0;

So the use of conditional operator ? can decrease the volume of codes extensively and make your program look compact.