If else in c


The if-else statement

The if else statement is a simple selection/decision statement that is used when we must choose between two alternatives. So it is called two way decision statement.
Syntax:-
     If(expression)
   {
    stat f1
    stat f2
    stat fn
   }
    else
   {
     stat s1
     stat s2
     stat s3
     stat sn
   }

Flowchart

 
Responsive image
 

 

Note:-the statements f1 to fn are executed if the condition is true.
Note:-the statements s1 to sn are executed if the condition is false.
 

 

W.A.P to print the greater no:-

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b;
 printf("Enter first no\n");
 scanf("%d",&a);
 printf("Enter second no\n");
 scanf("%d",&b);
  if(a>b)
  {
 printf("a is greater no");
  }
   else
  {
 printf("b is greater no");
  } 
  getch();

Output

Enter first no
5
Enter second no
6
b is greater no
 

W.A.P to enter two numbers and check both are equals or not

#include<stdio.h>
#include<conio.h>
void main()

   int a,b;
 printf("Enter Two no:-");
 scanf("%d%d",&a,&b);
 if(a==b)
 {
 printf("both are equals");
 }
 else
 {
 printf("both are not equals");
 }
 getch();
 } 

Output

Enter Two no:-
5
4
both are not equals

W.A.P to enter a no and print even or odd.

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int n;
   printf("enter a no">;
   scanf("%d",&n);
   r==n%2;
   if(r==0)
   }
   printf("even");
  }
   else
  {
   printf("odd");
  }
   getch();
  }

Output

enter a no:
4
even.

W.A.P to enter a no. and check positive or negative.

 #include<stdio.h>
 #include<conio.h>
 void main()
  {
   int n;
   printf("enter a no">;
   scanf("%d",&n);
   if(n<0)
  {
   printf("negative");
  }
   else
  {
   printf("positive");
  }
   getch();
  }

Output

enter a no
-3
negative.

W.A.P to enter a character and check vowel or consonant.

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   char ch;
   printf("enter a character");
   scanf("%c",&ch);
   if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
  {
   printf("vowel");
  }
   else
  {
   printf("consonant");
  }
   getch();
  }
enter a char
a
vowel.
 

The nested if statement

 
An if or if else statement within another if or if else statement is called “nested if statement” when an action has to be performed based on many decisions involving various types of expressions and variables then this statement is used so it is called multi way decision statement.
 
Syntax:-
if(condition)
    {
         if(condition 2)
         {
              Block A
         }
             else
           {
             Block B
           }
   }
           else
        {
           Block C
        }
 

W.A.P to enter 3 no and print greater no using nested if

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c;
 printf("Enter three numbers\n");
 scanf("%d %d %d",&a,&b,&c);
   if(a>b)
    {
   if(a>c)
   printf(“Max =%d”,a);
else
   printf(“Max = %d”,c);
     }
else
      {
  if(b>c)
  printf(“Max =%d”,b);
  else
  printf(“Max=%d”,c);
    }
getch();
}

Output

Enter three numbers
4 5 6
Max=6