If Else Condition in C


1.What is if else in C ?

Ans:  The if-else statement is a decision-making statement that is used to decide whether the part of the code will be executed or not based on the specified condition (test expression). If the given condition is true, then the code inside the if block is executed, otherwise the code inside the else block is executed.


Simple Explanation :


The If and Else condition is same as buying something.

In this image we can see that to buy things we give money. The shopkeeper will first check the note. If the note is original he will gives us the product we want. If  the note is Duplicate  he will not give the product.

If and else condition also works same. It will first check the condition and print the statement in if block or print the statement in the else block.



Working of if else :



Syntax  :

        if (condition)

        {

                // code executed when the condition is true

        }

        else

        {

               // code executed when the condition is false

        }

  Example:

 

1. Write a program to find that the number is positive numbers.

 #include<stdio.h>  // Header file

int main ()     // Main

{

int num;

printf("enter your favourite number");

scanf("%d",&num);

if(num%2==0)  // Condition

{

printf("%d is even",num);

}

else

{

printf("%d is odd",num);

}

}

  Output :



2. Write a program to see that the number is equal or not.

#include<stdio.h>    // Header file

int main ( )      // Main

{

int a,b;

printf("enter your favourite number for a");

scanf("%d",&a);

printf("enter your favourite number for b");

scanf("%d",&b);

if(a==b)            // Condition

{

printf("numbers are equal");

}

else

{

printf("numbers are unequal");

}

}

 Output :




3. Write a program to see that the enter alphabet is vowel or consonant.

#include<stdio.h>   // Header file
int main ()       // Main
{
    char a;
    printf("enter any alphabhate you like");
    scanf("%c",&a);
     if (a=='a' || a=='e' || a=='i' || a=='o' || a=='u' || a=='A'|| a=='E' || a=='I' || a=='O' || a=='U')      // Condition
    {
               printf("the alphabate is vowels");
    }
    else 
  
  {
         printf("it is a consonant");
    }
}


Output :