If…else statement in C

 

If…else statement Definition :

In C, the If…else statement is used to carry out operations depending on a particular situation. If and only if the provided condition is true, the operation described in the if block are carried out otherwise else block is carried out.

If else In C

Syntax:

if(condition){

// statements to be executed if condition is true;

}

else{

// statements to be executed if condition is false;

}

Working of if…else statement :

  • Step 1 : In the first step, condition/expression is checked whether it is true or false.
  • Step 2 : If statement is True, if block will be executed for performing the operations.
  • Step 3 : If the condition is false , else block will be executed for performing the executions
Use of If else In C

Example 1:

Run
#include<stdio.h>
int main() {
    int i=5;
    
    if(i>1){     // checking the condition and take decision
        printf("PrepInsta is one of the best for placement preparation");
    }
    else{
        printf("PrepInsta");
    }
    return 0;
}

Output:

PrepInsta is one of the best for placement preparation

In the above example , the value of i initialize as 5 and when the condition of if (i>1) is checked , this is taken as true and block if is executed and “PrepInsta is one of the best for placement preparation” will be print on the output screen.

Example 2:

Run
#include<stdio.h>
int main() {
    int i=5;
    
    if(i<1){     // checking the condition and take decision
        printf("PrepInsta is one of the best for placement preparation");
    }
    else{
        printf("PrepInsta");
    }
    return 0;
}

Output:

In the above example , the value of i initialize as 5 and when the condition of if (i<1) is checked, this is taken as false and block else is executed and “PrepInsta” will be print on the output screen.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription