Home »
C programming language
If else statements in C language | Conditional statements in C
Conditional statements are used when you want to execute code (set of statements) on certain conditions. Suppose you want to print the names oh those employees who have 5+ years experience, in this type of situation you will have to use a condition. Such type of cases will be handled by using conditions only.
There are following type of conditions:
- Simple if condition
- if else condition
- ladder (multiple) if condition
- nested (if within if) condition
1) Simple if condition
When you have only one block (set of statements) which is based on a condition i.e. when you have only one condition to check, on which you want to execute some set of statements.
Syntax
if( test-condition)
{
set-of-statements/if body;
}
If the test-condition is true (a non zero value), set-of-statements/if body will be executed.
Example
Consider the following example - program to check whether entered number is negative or positive.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
printf("Enter an integer number:");
scanf("%d", &number);
if (number <= 0) {
printf("Number is either 0 or negative.");
exit(0);
}
printf("Entered number if positive, value is : %d", number);
return 0;
}
Output
First run:
Enter an integer number:-123
Number is either 0 or negative.
Second run:
Enter an integer number:0
Number is either 0 or negative.
Third run:
Enter an integer number:123
Entered number if positive, value is : 123
2) if else condition
This type of if condition is used, when you have one condition and two body of code (two set of statements) to execute based on a single condition.
Syntax
if( test-condition)
{
True-block;
}
else
{
False-block;
}
Here, two blocks, True-block and False-block. If test-condition is true (non zero value), True-block will execute and if test-condition is false, False-block will execute.
Example
Consider the following example
/*program to check entered year is leap year or not.*/
#include <stdio.h>
int main()
{
int year;
printf("Enter year:");
scanf("%d", &year);
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
printf("%d is a leap year", year);
}
else {
printf("%d is not a leap year", year);
}
return 0;
}
Output
First run:
Enter year:1900
1900 is not a leap year
Second run:
Enter year:2015
2015 is not a leap year
Third run:
Enter year:2016
2016 is a leap year
3) if else .. if condition (ladder/multiple if conditions)
This type of if statement is used when you have more than one test conditions and blocks to execute.
Syntax
if(test-condition1)
{
Block1;
}
else if(test-condition2)
{
Block2;
}
else if(test-condition3)
{
Block3;
}
...
else
{
Default-Block;
}
Here, test-condition1 is true, Block1 will execute, if it is false program’s execution executes test-condition2, if it is true then Block2 will execute and so on. If none of the condition is true, Default-Block will be executed.
Example
Consider the following example
/*program to check entered number if ZERO, POSITIVE or NEGATIVE.*/
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer number:");
scanf("%d", &num);
if (num == 0) {
printf("Number is ZERO");
}
else if (num > 1) {
printf("Nunber is POSITIVE");
}
else {
printf("Number is NEGATIVE");
}
return 0;
}
Output
First run:
Enter an integer number:0
Number is ZERO
Second run:
Enter an integer number:123
Number is POSITIVE
Third run:
Enter an integer number:-234
Number is NEGATIVE
4) Nested (if within if) if condition
This type of if is used, when you want to check another condition within if condition’s block, suppose there are two conditions, condition1 and condition2 and you want to execute condition2 if condition1 is true.
Syntax
if( test-condition1)
{
Statements;
if(test-condition2)
{
Block-1;
}
else
{
Block2;
}
}
else
...
Here, test-condition2 will be executed, if the test-condition1 is true.
Example
Consider the following example
/* Program to check to entered character if vowel or consonant.*/
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
printf("%c is a VOWEL", ch);
}
else {
printf("%c is a CONSONANT", ch);
}
}
else {
printf("%c is not a valid character", ch);
}
return 0;
}
Output
First run:
Enter a character: A
A is a VOWEL
Second run:
Enter a character: i
A is a VOWEL
Third run:
Enter a character: W
A is a CONSONANT
Fourth run:
Enter a character: 9
9 is not a valid character