If Else Statement in C

The if else statement in c is used to execute statements based on the condition. The if else statement is also known as conditional statement in c. It helps the compiler to execute the instrunction, based on the given condition. It helps the computer in decision making. different forms of conditional statement.

  • if
  • if else
  • if else-if else
  • Nested if

The IF Condition

Syntax:
if(Boolean_expression)
{
	//statements to be executed
	//if the condition is true
}

Example 1 : Write a C program to calculate sales commission points, based on the condition. Each sales is equal to 2 points.

#include<stdio.h>
int main()
{
int quantity, points=0;
	printf("\nEnter sales quantity for the day:");
	scanf("%d",&quantity);
	points=quantity*2;
	//Earn 5 more points, if sales quantity is greater then 250.
	if(quantity>=250)
	{
		points=points+5;
	}
	printf("\nYou Have Earn %d Points",points);
return 0;
}
Output :
Enter sales quantity for the day: 240
You Have Earn 480 points

Enter sales quantity for the day: 250
You Have Earn 505 points

If Else in C

If we have two different statement to execute, based on the two different condition, we will use if else statement in C language. We need to pass Boolean expression as parameter to the If condition, and it returns true or false. If the condition is satisfied, then if block will execute, and if the condition is not satisfied, and result is false, then else block will execute. before we start, keep in mind that, only if statement accept Boolean expression (condition), as parameter, not to else.

Syntax
if(Boolean_Expression)
{
	//executed, when the condition is true
}
else
{
	//executed, when the condition is false
}

Example 2: C program to check, number is Even or Odd.

#include<stdio.h>
int main()
{
int num;
	printf("\nEnter a number : ");
	scanf("%d",&num);
	if(num % 2 == 0)
	{
		printf("Number is Even");
	}
	else
	{
		printf("Number is Odd");
	}
		
return 0;
}
Output:
Enter a number : 24
Number is Even

If Else-if Else

Some times, we have more then two condition to check, we will use if else-if else in C. In this expression, if and if-else statement accept Boolean expression as parameter. You need to look at syntax, to understand this.

Syntax
if(Boolean_Expression)
{
	//executed, when the condition is true
}
else if(Boolean_Expression)
{
	//executed, when the if condition is false
	//and else-if, returns true
}
else
{
	//executed, when the both if and else-if condition is false
}

Example 3: C program to test, given number is positive, negative or zero.

#include<stdio.h>
int main()
{
int num;
	printf("\nEnter a number: ");
	scanf("%d", &num);
	if(num > 0)
	{
		printf("Number is Positive");
	}
	else if(num < 0)
	{
		printf("Number is Negative");
	}
	else
	{
		printf("Number is Zero");
	}

		
return 0;
}
Sample Output:
Enter a number: -10
Number is Negative

Nested If

Whenever we use if as a statement within if or else block, then it is referred as nested if. The below example will explain better.

Example 4: C program, to find largest number among three given numbers.

#include<stdio.h>
int main()
{
int a,b,c;
	printf("\nEnter 3 numbers: ");
	scanf("%d%d%d", &a, &b, &c);
	if( a > b)
	{
		if( a > c)
		{
			printf("First Number Largest");
		}
		else
		{
			printf("Third Number Largest");
		}

	}
	else if( b > c)
	{
		printf("Second Number is Largest");
	}
	else
	{
		printf("Third Number is Largest");
	}

		
return 0;
}
Sample Output:
Enter 3 numbers: 2 3 1
Second Number is Largest