The if else in C is used to perform the basic operations on the specific conditions. The statements which are declared in the if else in C is executed if and only if the given condition is true.

In real life, there come situations, when we need to make some decisions and based on these decisions, we decide what should we do next.

Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code.

The following statements are used to perform the task of conditional operators:

  • if Statement.
  • if-else Statement.
  • Nested else-if Statement.
  • Nested if-else Statement.

1. if Statement

if statement is the most simple decision-making statement. It is used to express conditional expression.

It is used to decide whether a certain statement or block of statements will be executed or not. If the given condition is true then it will execute the statement or block of statements otherwise skip the statements.

The simple structure/syntax of ‘if’ statement is as follows:

if (condition)
{
1 statement;
2 statement;
.
.
.
n statement;
}

The expression is evaluated and if the expression is true, then statements will be executed. If the expression is false the statements are skipped and execution continues with the next statements.

if else in c

Let’s see an example of if statement:

//Learnprogramo
#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:

if else in c

2. if else in C Statement

The if-else statement is used to execute either of the two statements depending upon the value of the test expression.

The general form/syntax as follows:

if (condition)
{
1 statement;
2 statement;
}
else
{
3 Statement;
4 Statement;
}

Statement 1, 2 will be executed if the expression or condition is true, Statements 3, 4 will be executed if the expression or condition is false.

The if-else can be shown with a flowchart as depicted in below fig:

if else in c

Example:

//Learnprogramo
#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);    
}    
else{    
printf("%d is odd number",number);    
}     
return 0;  
}    

Output:

if else in c

3. Nested if-else in C Statement:

When a series of decision is required, nested if-else is used.

When an if-else statement is present inside the body of another “if” or “else” then this is called nested if-else. In this situation, one of several different courses of action will be selected.

Syntax of Nested if-else statement is as follows:

if(condition)
{
if(condition2)
{
1 Statement inside the body of nested “if”
}
else
{
2 Statement inside the body of nested “else”
}
}
else
3 Statement inside the body of nested “else”
}

The flowchart of nested if-else Statement is given below:

nested fowchart

Example:

//Learnprogram
#include<stdio.h>    
int main()
{    
int number=0;    
printf("enter a number:");    
scanf("%d",&number);     
if(number==10){    
printf("number is equals to 10");    
}    
else if(number==50){    
printf("number is equal to 50");    
}    
else if(number==100){    
printf("number is equal to 100");    
}    
else{    
printf("number is not equal to 10, 50 or 100");    
}    
return 0;  
}    

Output:

nested exmaple

4. if-else-if Ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

The execution starts from the top and checked for each if condition. The statement of if block will be executed which evaluates to be true. If none of the if the condition evaluates to be true then the last else block is evaluated.

Syntax:

if(<exp1>)
1 Statement;
else if(<exp2>)
2 Statement;
else if(<exp3>)
3 Statement;
else
4 Statement;

The conditional expression is evaluated from the top downward.

As soon as the true condition is found, the statement associated with it is executed, and the rest of the ladder is bypassed. If none of the condition is true, then the final else statement will be executed.

The final else often acts as a default condition; that is, if all other conditions tests fail, then the last else statement is performed.

The flowchart of the if-else-if ladder is shown below:

if else if flowchart

Program:

//Learnprogramo
#include<stdio.h>
int main()
{
int a;
printf("\nEnter a number:");
scanf("%d",&a);
if(a>0)
printf("\nGiven number is positive");
else  if(a==0)
printf("Given number is zzero");
else if(a<0)
printf("\nGiven number is negative");
else
printf("\nInput is not numeric");
return 0;
}

Output:

example

5. Conditional Operator(?:)

We have covered the conditional operator in the second chapter which can be used for two-way decision making.

The main advantage of using ternary operator over the if-else statement is to reduce the number of line codes and improve the performance of the application.

Syntax: condition? value_if_true: value_if_false

If the condition is true, statement 1 will be evaluated, otherwise, statement 2 will be evaluated.

Program:

#include<stdio.h>
int main()
{
int a,b,c,max;
printf("Enter any three numbers\n");
scanf("%d%d%d",&a,&b,&c);
max=(a>b)?(a>c?a:c):(b>c?b:c);
printf("%d is the largest number of given numbers.",max);
return 0;
}

Output:

conditional operator

Conclusion:

In this lesson, we have learned c programming if statement multiple conditions and in the next lesson we will learn switch statement in c and for loops in c.