if - else in C

Programming languages or concepts
0

Control Statements


C if else Statement

The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.

There are the following variants of if statement in C language.

  1. If statement
  2. If-else statement
  3. If else-if ladder
  4. Nested if

If Statement

The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below.

Example: 

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

#include <stdio.h>
 
int main() {
    int i = 10;
 
    if (i > 15)
    {
       printf("true");
    }   
    
    printf("false");
}

OUTPUT:
false


How if statement works?

The if statement evaluates the test expression inside the parenthesis ().

If the test expression is evaluated to true, statements inside the body of if are executed.
If the test expression is evaluated to false, statements inside the body of if are not executed.



If-else Statement

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition. The syntax of the if-else statement is given below.

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

Example: 

#include <stdio.h>
 
int main() {
    int i = 20;
 
    if (i < 25){
       
        printf("true");
    }
    else{
       
        printf("false");
    }       
    return 0;   
}

OUTPUT:
                    true

How if...else statement works?

If the test expression is evaluated to true,

statements inside the body of if are executed.
statements inside the body of else are skipped from execution.
If the test expression is evaluated to false,

statements inside the body of else are executed
statements inside the body of if are skipped from execution.



If else-if ladder Statement

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.

The if...else ladder allows you to check between multiple test expressions and execute different statements.
The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.

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  
}  

Example: 

#include <stdio.h>

int main() {
int i = 20;

if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}

Nested if...else

It is possible to include an if...else statement inside the body of another if...else statement.

Example : Nested if...else

This program given below relates two integers using either <, > and = similar to the if...else ladder's example. However, we will use a nested if...else statement to solve this problem.
Example: 

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 >= number2) {
      if (number1 == number2) {
        printf("Result: %d = %d",number1,number2);
      }
      else {
        printf("Result: %d > %d", number1, number2);
      }
    }
    else {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}


Recommended Articles:


Post a Comment

0Comments

Post a Comment (0)
close