C If else

To perform the operation with conditional statements if statements are used . These statements controls the flow of execution of java program based upon conditions .


There are various types of if statement in C

  Simple if

  if - else

  Nested if

  else - if ladder

Simple if

It is the simple conditional statements which executes the block only if the condition is true.

  flow diagram

// Syntax  of using simple if
if(condition)
{
//statements to execute if condition is true
}

Example


#include 
int main() {

    //using simple if
    if (3 > 2) {
        printf("3 is greater than 2");
        printf("\n");
        printf("It is true block of simple if");
    }

    return 0;
}
3 is greater than 2
It is true block of simple if

if else statement in C

This conditional statement checks both true as well as false block. if the condition becomes true than it executes true block other wise it executes false block of statement

//syntax  how to use if-else in C
if(condition)
    {
        //statement to execute when condition become true
    }else
    {
        //statement to execute when condition become false
    }

Simple example of if...else


#include 

int main() {

    int a = 3;
    int b = 5;
    if (a > b) {
        printf("a is greater than b");
    } else {
        printf("a is not greater than b");
    }

    return 0;
}
a is not greater than b

nested if...else statements

whenever any if else statement is use inside another it else statement than it is called nested if..else statement.

//Syntax of using nested if else in C
if(condtion 1 )
{
    if(condition 2 )
    {
     //statment to be execute if both condtion1 and condtion 2 becomes true
    }else
    {
     //statment to be execute if both condtion1 =true and condtion =false
    }
}
else
{
// we can put if ...else block here also depending upon our requirement
}

Simple example of Nested if


#include 

int main() {
    int a = 5;
    int b = 6;
    int c = 1;
    if (a > b) {
        if (a > c) {
            printf("a is greatest one");
        } else {
            printf("c is greatest one");
        }

    } else if (b > c) {
        printf("b is greatest");
    } else {
        printf("c is greatest..");

    }
    return 0;
}
b is greatest

if-else ladder in C

The expression is tested from the top (of the ladder) to downwards. As soon as the true condition will found, the statement associated with it is executed.

The if else ladder statement in C programming language is used to test set of conditions in sequence. An if condition is tested only when all previous if conditions in if-else ladder is false. If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder

//Syntax
if(condition_expression_One) {
    statement1;
} else if (condition_expression_Two) {
    statement2;
} else if (condition_expression_Three) {
    statement3;
} else {
    statement4;
}

First of all condition_expression_One is tested and if it is true then statement1 will be executed and control comes out out of whole if else ladder.

If condition_expression_One is false then only condition_expression_Two is tested. Control will keep on flowing downward, If none of the conditional expression is true.

The last else is the default block of code which will gets executed if none of the conditional expression is true.

Simple Example of If else ladder

//Write a C Program which demonstrate use of if-else-if ladder statement.
#include

int main() {
    int a;
    printf("Enter a Number: ");
    scanf("%d", &a);
    if (a > 0) {
        printf("Given Number is Positive");
    } else if (a == 0) {
        printf("Given Number is Zero");
    } else if (a < 0) {
        printf("Given Number is Negative");
    }
    return 0;
}
Enter a Number: -30
Given Number is Negative

scanf() function is used to take the input.

woman avatar

What we are going to learn in C Control Statements