AsegGasiaBlog

C Programming - Decisions Control Statement

Decisions Control Statement

1. If Statement
2. If-Else Statement
3. Else-If Statement
4. Nested if-else Statement
5. Switch

1. If Statement

It is used to execute an instruction or sequence / block of instruction only if condition is fulfilled. if statement, expression is evaluated first and then, depending on whether the value of the expression (relation or condition) is true or false, transfers the control to the particular statement or group of statements.

Different forms of implementation of if-statement are :

  • Simple if statement
  • if-else statement
  • Nested if-else statement
  • Else if statement

Simple if statement

This is used to executed the statements only if the condition is true.

Syntax :

    if(condition)
    {
        block of statement;
    }

Example :


#include <stdio.h>
int main()
{
    int number = 0;
    printf("\nEnter an integer : ");
    scanf("%d",&number);

    if (number > 10)
    {
        printf("\nYou entered %d which is greater than 10\n", number);
    }

    if (number < 10)
    {
        printf("\nYou entered %d which is less than 10\n", number);
    }

    return 0;
}

Output :

 Enter an integer : 8
 You entered 8 which is less than 10

2. If-Else Statement

If..else statement is used when different block of statement is to be executed on condition true and false.

Syntax :

    if(condition)
    {
        block 1 of statement;
    }
    else
    {
        block 2 of statement;
    }

if condition is true then block 1 statement will be executed and if condition is false block 2 statement will execute.

Example :


#include <stdio.h>
int main()
{
    int number = 0;
    printf("\nEnter an integer : ");
    scanf("%d",&number);

    if (number > 10)
    {
        printf("\nYou entered %d which is greater than 10\n", number);
    }
    else
    {
        printf("\nYou entered %d which is less than 10\n", number);
    }

    return 0;
}

Output :

 Enter an integer : 8
 You entered 8 which is less than 10

3. Else-If Statement

To show a multi-way decision based on several conditions, we use the else if statement.

Syntax :

 if(condition_2)
 {
    block 1 statement;
 }
 else if (condition_2)
 {
    block 2 statement;
 }
 else if(condition_n)
 {
    block n statement;
 }
 else
 {
    block x statement;
 }

here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluates to true, then statement associated with the given condition is executed and if all condition are false, then control is transferred to statement_x skipping the rest of the condition.

Example :


#include <stdio.h>

int main ()
{
    int mark;

    printf("Enter total marks of student : ");
    scanf("%d",&mark);

    if(mark <= 50)
    {
        printf("\nGrade D" );
    }
    else if (mark <= 60)
    {
        printf("\nGrade C");
    }
    else if (mark <= 70)
    {
        printf("\nGrade B");
    }
    else
    {
        printf("\nGrade A");
    }

    return 0;
}

Output :

 Enter total marks of student : 80
 Grade A

4. Nested If - Else Statement

You can combine multiple if / if-else / if-else-if ladders when a series of decisions are involved. So you can make sure that your program executes certain instructions when a series of conditions are met.

Some use case :

1.
    
    if(condition)
    {
        //block of statement

        if(condition)
        {
            //block of statement
        }
    }
    

2.
    
    if(condition)
    {
        //black of statement
    }
    else
    {

        if(condition)
        {
            //block of statement
        }
        else
        {
            //block of statement
        }

    }
    

3.
    
    if(condition)
    {
        //black of statement
    }
    else if(condition)
    {

        if(condition)
        {
            //block of statement
        }
        else
        {
            //block of statement
        }

    }
    

Example :


#include <stdio.h>
int main()
{
    int numb1, numb2;
    printf("Enter two integers : ");

    scanf("%d %d",&numb1,&numb2);
    //checking whether two integers are equal.
    if(numb1==numb2)
    {
        printf("\nResult: %d = %d",numb1,numb2);
    }
    else
    {
        //checking whether numb1 is greater than numb2.
        if(numb1>numb2)
        {
            printf("\nResult: %d > %d",numb1,numb2);
        }
        else
        {
            printf("\nResult: %d > %d",numb2,numb1);
        }
    }

    return 0;
}

Output :

 Enter two integers : 7 9
 Result: 9 > 7

5. Switch

switch statements are also used when we need our program to make a certain decision based on a condition and then execute accordingly.

Syntax :


switch (<variable>)
{
    case a-constant-expression :
        //Code to execute if <variable> == a-constant-expression
        break;

    case b-constant-expression  :
        //Code to execute if <variable> == b-constant-expression
        break;
    .
    .
    .
    case n-constant-expression :
        //Code to execute if <variable> == n-constant-expression
        break;

    default:
        //Code to execute if <variable> does not equal the value following any of the cases
}

(We will learn about break keyword in Loop section)

Example :


#include <stdio.h>

int main()
{
    int input;

    printf( "1. Play game\n" );
    printf( "2. Load game\n" );
    printf( "3. Play multi-player\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    printf("\n");

    switch ( input )
    {
        case 1: //Note the colon, not a semicolon
            printf( "Play game called" );
            break;
        case 2:
            printf( "Load game called" );
            break;
        case 3:
            printf( "Play Multi-player game called" );
            break;
        case 4:
            printf( "Thanks for playing!\n" );
            break;
        default:
            printf( "Bad input, quitting!\n" );
            break;
    }
    return 0;
}

Output :

    1. Play game
    2. Load game
    3. Play multi-player
    4. Exit
    Selection: 1
    Play game called

Important points

  • The default case is optional.
  • case constant-expression can be int or char (no other datatypes are allowed).







Popular Posts