if else statement

C language provides a decision control mechanism to execute your code based on a condition.

if condition

if(condition)
    code;

A condition is an expression usually written using 2 operands separated by 1 relational operator. The result of a condition will be either 1 or 0. If the expression is true the condition returns 1 else it returns 0.

An if condition starts with if keyword, followed by condition surrounded by round brackets.

An example,

#include <stdio.h>

void main()
{
    int marks = 80;
    if(marks >= 35)
        printf("Passed");
}

else statement

You can add an else statement to perform something else when the condition in if statement is not success.

if(condition)
  true-code;
else
  false-code;

For example,

#include <stdio.h>

void main()
{
    int marks = 80;
    if(marks>=35)
        printf("Passed");
    else
        printf("Failed");
}

Multiple statements in if-else

If you want to execute multiple statements conditionally, you can enclose all those statements within curly brackets.

if(condition)
{
    statement1;
    statement2;
}
else 
{
    statement1;
    statement2;
}

For example, in the below program we are printing multiple statements when the condition is either success or failure, using curly parenthesis.

#include <stdio.h>

void main()
{
    int marks = 80;
    if(marks>=35)
    {
        printf("Passed");
        printf("\n Marks: %d",marks);
    }

    else
    {
        printf("Failed");
        printf("\n Marks: %d",marks);
    }
}

Nested if conditions

An if condition within another if condition is called the nested if condition.

if(condition 1)
{
    if(condition 2)
    {
         statements;
    }
    statements;
}

Similarly, we can write a nested if condition inside else block too.