If Else Statement in C

If Else Statement is always return ture or false.If it returns true then it excute if() part otherwise it excute the else part.We can divide the if-else statement in three parts.

  1. Genaral If-else Statement
  2. Else if Statement
  3. Nasted if-else statement


Genaral If-else Statement:
if else flowchart example
example1:

  #include<stdio.h>
  int main(){
   int a=5, b=3;
   if(a>b){//becomes ture
    printf("a is grater then b");
   }
   else{ 
    printf("b is grater then a");
   }
  return 0;
 }


Output:
  
    a is grater then b
  

example2:

  #include<stdio.h>
  int main(){
   int a=2, b=3;
   if(a>b){//becomes false
     printf("a is grater then b");
   }
   else{
     printf("b is grater then a");
    }
  return 0;
 }


Output:
    
    b is grater then a
  

else-if Statement:

If the there are sevaral condition then we need to write else-if statement.See the following example.


  #include<stdio.h>
  int main(){
   int a=2, b=2;
   if(a>b){//becomes false
     printf("a is grater then b");
   }
   else if(a==b){//becomes true
     printf("a and b is equal");
   }
   else{
    printf("b is grater then a");
   }
  return 0;
  }


Output:
  
    a and b is equal
  

Nasted if-else statement:

Assume you have a condition if the number is grater than 10 and if the number is even then print "EVEN" if it is odd then print "ODD" otherwise print "Number is less than 10". Let's implement it:


  #include<stdio.h>
  int main(){
   int number;
   number = 12;
   if(a>10){//becomes true
     if(number%2==0){//if true
       printf("EVEN");
      }
     else{//if false
       printf("ODD");
     }
   }
   else{//if false
     printf("Number is less than 10");
    }
  return 0;
 }


Output:

  EVEN