C-if else statement


If else statement is similar to the if statement .If condition in the if block is true or correct then code inside the if block execute other wise code present in the else block execute.


Syntax for  C-if else statement:

if(condition) {
   // Statements inside body of if
}
else {
   //Statements inside body of else
}

Flow chart for C-if else statement:




Flow Chart for if-else statement








Example for C-if else statement:


First example:

Problem statement:Write a C program to find maximum between two numbers using if else. C program to input two numbers from user and find maximum between two numbers using if else. How to find maximum or minimum between two numbers using if else in C programming.



Sample Input:

num1=200
num2=100

Sample Output:

max=200

#include <stdio.h>
int main()
{
   int num1,num2;
   printf("Enter two numbers:");  /* Input two numbers from user*/
   scanf("%d%d",&num1,&num2);
   if(num1 > num2 )    /*condition checking*/ 
   {
	/* if condition true then execute*/
	printf("max= %d",num1);
   }
   else
   {
	/* if condition is false then execute*/
	printf("max= %d",num2);
   }
   return 0;
}


Second example:

problem statement: In this problem we are checking a person is eligible for voting or not. condition of eligible voter is he/she must be 18 or greater than 18. As input we take age a of person.


Sample Input:

12 


Sample Output:

You are not eligible for voting 




#include <stdio.h>
int main()
{
   int age;
   printf("Enter your age:");
   scanf("%d",&age);
   if(age >=18)
   {
	/*  code will only execute if the
	 * above condition (age>=18) returns true
	 */
	printf("You are eligible for voting");
   }
   else
   {
	/* This statement will only execute if the
	 * condition specified in the "if" returns false.
	 */
	printf("You are not eligible for voting");
   }
   return 0;
}

INPUT/OUTPUT:

Enter your age:12
You are not eligible for voting