If else c – If else Statement in C Programming

If else c: The if else statement in C programming language is used to execute a set of statements if condition is true and execute another set of statements when condition is false. Only either if block or else block of code gets executed(not both) depending on the outcome of condition.

Syntax of if else statement

if(condition_expression) {
    /* code to be execute if the condition_expression is true */
    statements;
} else {
    /* code to be execute if the condition_expression is false */
    statements;
}

The if else statement checks whether condition_expression is true or false. If the condition_expression is true, then the block of code inside the if statement is executed but if condition_expression is false, then the block of code inside the else statement is executed.

Both if and else block of statements can never execute at a time.

C-If-Else-Statement

C Program to show use of If Else Control Statement

If else Statement in C Programming

#include<stdio.h>
#include<conio.h>
 
int main() {
    int marks;
    printf("Enter your Marks : ");
    scanf("%d", &marks);
    /* Using if else statement to decide whether 
       a student passed or failed in examination */
    if(marks < 40){
        printf("You Failed :(\n");
    } else {
        printf("Congrats You Passed :)\n");
    }
     
    getch();
    return(0);
}

Above program check whether a student passed or failed in examination using if else statement.

Output

Enter your Marks : 45
Congrats You Passed :)
Enter your Marks : 25
You Failed :(

Points to Remember about If Else Statement

  • The condition_expression is a boolean expression. It must evaluate to true or false value(In C, all non-zero and non-null values are considered as true and zero and null value is considered as false).
  • We may use more than one condition inside if else statement.
    For Example:
    if(condition_one && condition_two) {
    /* if code block */
    } else {
    /* else code block */
    }
  • Opening and Closing Braces are optional, If the block of code of if else statement contains only one statement.
    For Example:
    if(condition_one && condition_two)
    statement1;
    else
    statement2
    In above example only statement1 will gets executed if condition_expression is true otherwise statement2.