If-Else  Statement

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484

Simple if works as "if some condition is true then do some tasks". It doesn't specifies what to do if condition is false. A good program must think both ways. For example - if user inputs correct account number and pin, then allow money withdrawal otherwise show error message to user.

if...else statement is an extension of simple if. It works as "If some condition is true then, do some task otherwise do some other task".

Syntax of if...else statement

 

if(boolean_expression)

{

    // Body of if

    // If expression is true then execute this

}

else

{

    // Body of else

    // If expression is false then execute this

}

 
Flowchart of if-else statement

If else statement flow chart

Example:

#include <stdio.h>

void main()

{    int num1, num2;

    printf("Enter two numbers: ");

    scanf("%d%d", &num1, &num2);

    if(num1 > num2)

    {

        printf("First number is maximum.");

    }

    else

    {

        printf("Second number is maximum.");

    }

}

 

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484