# 5.4 – if-else selection statement

by on September 15, 2017

### if-else selection statement:

In previous session we have discoursed simple if selection statement in length. It is generally used to solve a problem with any number of possibilities. There should be as many number of conditions as the number of possibilities.

if-else is another selection statement used to solve a problem with only two possibilities by writing a single condition.

It has two parts that are if part and else part. If the condition is true then the statement or block of statements under the if conditional statement is executed otherwise the statement or block of statements under the else part is executed.

At any given time either if or else part is executed. Both if and else parts of if-else control structure are never executed at once. Hence it is used in the places where we need to check two conditions, out of which one must be true and other must be false if condition is true then the if part is executed otherwise the else part is executed
Example:

```#include<stdio.h>
int main()
{
if(10<20)
printf("One");
else
printf("Two");
printf("\n");
if(10>20)
printf("Three");
else
printf("Four");
return 0;
}
```

Output:
One
Four

Example explained:
(10<20) is true, hence the if part printf(“One”); is selected to executed and else part printf(“Two”); is skipped.
(10>20) is false, hence the if part printf(“Three”); is skipped and else part printf(“Four”); is selected to execute.

Now we will write an equivalent program using if-else to the program already written in simple if

```if(n%2==0)/*checking for even*/
printf("Even number");
if(n%2==1)/*checking for odd*/
printf("Odd number");
```

In the above example for any given integer, one condition must be true and other must be false. So it can be written using if-else selection statement as.

```#include<stdio.h>
int main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
if(n%2==0)
printf("Even number");
else
printf("Odd number");
return 0;
}
```

Execution 1:
Enter any integer: 45
Odd number

Execution 2:
Enter any integer: 42
Even number

Example explained:
During the first execution, the condition is false hence the else part is executed. During the second execution, the condition is true hence the if part is executed.

Now we will write another equivalent program using if-else to the program already written in simple if

```if(n==0)/*checking for zero*/
printf("Zero");
if(n!=0)/*checking for non-zero*/
printf("Not a zero");
```

In the above example for any given number, one condition must be true and other must be false because any number may be a zero or a non-zero. So it can be written using if-else selection statement as.

```/* program to find a zero or a non-zero*/
#include<stdio.h>
int main()
{
float n;
printf("Enter any number:");
scanf("%f",&n);
if(n==0)
printf("Zero");
else
printf("Not a zero");
return 0;
}
```

Execution 1:
Enter any number: 0
Zero

Execution 2:
Enter any number: 14.60
Not a zero

Example explained:
During the first execution the condition is true hence the if part is executed.
During the second execution the condition is false hence the else part is executed.

Previous post: