if-else Statement

Thus, if-else statement are used to perform flow based on some condition only the difference is you can used to execute some statement code block if the expression is evaluated true, otherwise else statement code on block.

There are the following types of if-else statement

  • if statement
  • if-else statement
  • if-else if-ladder statement
  • Nested if statement
  • if statement: If statement are allow you to control if a program enters a section of code or not based on those condition is true or false.In the if statement it can allow the program to first set of code after the end of the if statement will be excited.

    Syntax of the if statement

    if(expression){
    //code to be executed
    }

    Let’s see the example of if statement

    #include<stdio.h>    
    int main(){    
    int number=0;    
    printf("Enter a number:");    
    scanf("%d",&number);    
    if(number%2==0){    
    printf("%d is even number",number);    
    }    
    return 0;  
    }   
    

    Output

    Enter a number:8
    8 is even number
  • if-else statement: If-else statement are perform two operation for a single condition.If statement checks whether condition is true or false.
    If condition is false than another block code executed, and if & else block cannot executed simultaneously.

    The syntax of if-else statement :

    if(expression){
    //code to be executed if condition is true
    }else{
    //code to be executed if condition is false
    }
    

    let’s see the example of if-else statement

    #include <stdio.h>  
    int main()  
    {  
        int age;   
        printf("Enter your age");   
        scanf("%d",&age);  
        if(age>=18)  
        {  
            printf("You are eligible to vote...");   
        }  
        else   
        {  
            printf("you can't vote");   
        }  
    }

    Output

    Enter your age 18
    You are eligible to vote...
    
    Enter your age 13
    you can't vote
  • if-else if-ladder statement: If else-if ladder Statement is a extension of if-else statement. In this case we can the scenario where are multiple cases to be performed for different condition. There are multiple if else blocks are possible.
    When the default case is executed it is similar to switch case statement because in the else block if the case is not matched.

    Syntax of If else-if ladder Statement

    if(condition1){
    //code to be executed if condition1 is true
    }else if(condition2){
    //code to be executed if condition2 is true
    }
    else if(condition3){
    //code to be executed if condition3 is true
    }
    else{
    //code to be executed if all the conditions are false
    }

    Let’s see the example if If else-if ladder statement

    #include <stdio.h>  
    int main()  
    {  
        int marks;   
        printf("Enter your marks");  
        scanf("%d",&marks);   
        if(marks > 85 && marks <= 100)  
        {  
            printf("Congrats!!!! you scored grade A");   
        }  
        else if (marks > 60 && marks <= 85)   
        {  
            printf("You scored grade B +");  
        }  
        else if (marks > 40 && marks <= 60)   
        {  
            printf("You scored grade B ");  
        }  
        else if (marks > 30 && marks <= 40)   
        {  
            printf("You scored grade C ");   
        }  
        else   
        {  
            printf("you are fail");   
        }  
    }

     Output

    Enter your marks 10
    you are fail
    
    Enter your marks 40
    You scored grade C 
    
    Enter your marks 90
    Congrats!!!! you scored grade A

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews