Introduction
We generally follow a sequence in the program but at times we would just want to execute only selected statements.- This type of conditional processing is really useful to the programs.
- It allows the programmers to build the programs that determine which statements of the code should be executed and which are to be ignored.
Conditional statements
The conditional statements help to jump from one part of a program to another depending if a particular condition is satisfied or not.The conditional statements are:
1. if statement
It is one of the most simple form of decision control statements which is frequently used in decision making.Syntax:
if (test expression)
{
statement 1;
…...............
statement n;
}
statement x;

- The if structure may have one statement or n number of statements which are enclosed within the curly braces ({ }).
- Initially the test expression is evaluated.
- If this expression is true the statement of the if block is executed or all the statements will be skipped and statement x will be executed.
Example: Demonstrating an if statement
Write a program to print the highest number.
#include <stdio.h>
void main()
{
int n;
printf("Enter a number:");
scanf("%d", &n);
printf ("The entered number %d", n);
if (n>100)
printf ("You entered a higher number");
}
In the above program, a number is accepted as an input from the user and the output is given. The expression (n>100) is given. If the user gives a number higher than 100 then only will the message be printed else the statement will be skipped.
2. If-else statement
There are times where we would like to write that if the expression is true an action should be taken and if the expression is false there would be no action. We can also include an action for both the conditions.In such cases, we use the if-else statement.
Syntax:
if (expression)
Statement 1;
else
Statement 2;
If the expression is true statement 1 will be executed and if the expression is false statement 2 will be executed.

Example: Program for leap year
#include <stdio.h>
void main()
{
int yr;
clrscr();
printf("Enter a year:");
scanf("%d", &yr);
if ((yr%4 == 0) && ((yr%100 !=0) || (yr%400==0)))
printf("It's a leap year...!!!!");
else
printf ("It's not a leap year....!!!");
}
Output:
Enter a year: 1997
It's not a leap year...!!!
3. if-else-if statement
- This statement works in the normal way as the if statement.
- Its construct is known as the nested if statement.
- After the first if branch the program can have many other else-if branches depending on the expressions that need to be tested.
if (test expression 1)
{
statement block 1;
}
else if (test expression 2)
{
statement block 2;
}
....................
else
{
statement block x;
}
statement y;

Example: Program to find largest of the three numbers by using && operator.
#include <stdio.h>
void main()
{
int n1=10, n2=30, n3=75;
if (n1>n2 && n1>n3)
printf("%d is the largest number",n1);
if(n2>n1 && n2>n3)
printf("%d is the largest number", n2);
else
printf("%d is the largest number", n3);
}
Output:
75 is the largest number.
4. Switch case
This case statement is a multi-way decision statement which is a simpler version of the if-else block which evaluates only one variable.Syntax:
switch (expression)
{
case expression 1: statement 1;
statement 2;
............
statement n;
break;
case expression 2: statement 1;
statement 2;
..........
statement n;
break;
default: statement 1;
statement 2;
...........
statement n;
}

All the expressions should have a result of an integer value or the character value.
Example: Write a program to check if the character entered is a vowel or not.
#include <stdio.h>
void main( )
{
char ch;
printf("Enter any character:");
scanf("%c", &ch);
switch(ch)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
printf("%c is a vowel",ch);
break;
default:
printf("%c is not a vowel", ch);
}
}
Output:
Enter any character: c
c is not a vowel.


