If...Else Statement
If...Else Statement is a conditional statement that is used to execute one of many statements as per the condition. We require condition testing to be done in our program. For this, we can use If...Else statement. The syntax of If statement, ommiting the else, is as follows:
The syntax of If...Else statement is as follows:
if ( Condition ) {
// Code
}
The above condition is a boolean expression that is used to test the value of a variable, or even a literal or a variable can be directly used. If the condition is evaluated to true, then the code block inside the if block is executed. If the condition is evaluated to false, then the code block inside the else block is executed. If no else block is present, then no code is executed, but the control is passed to the next statement.
// Code
}
The syntax of If...Else statement is as follows:
if ( Condition ) {
// Code
} else {
// Code
}
// Code
} else {
// Code
}
Let's have a look at an example
C
#include <stdio.h>
int main()
{
int age = 10;
if ( age > 18 ) {
printf("You are eligible to get a driving license.");
}
else {
printf("You are not eligible for driving license");
}
return 0;
}
int main()
{
int age = 10;
if ( age > 18 ) {
printf("You are eligible to get a driving license.");
}
else {
printf("You are not eligible for driving license");
}
return 0;
}
Output
You are not eligible for driving license
Explanation
In the above example, an integer variable age is declared and assigned a constant litreral 10. Then, we want to check if the age is elligible to get a driving license, or not.
For that, a condition is tested using the relational operator >. The condition in the above example is evaluated to false, and hence the code block inside the else block is executed. Then, the control is passed to the next statement.
For that, a condition is tested using the relational operator >. The condition in the above example is evaluated to false, and hence the code block inside the else block is executed. Then, the control is passed to the next statement.
A perfect and more preferred example would be
C
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if ( age > 18 ) {
printf("You are eligible to get a driving license.");
}
else {
printf("You are not eligible for driving license");
}
return 0;
}
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if ( age > 18 ) {
printf("You are eligible to get a driving license.");
}
else {
printf("You are not eligible for driving license");
}
return 0;
}
Output
Enter your age: 19
You are eligible to get a driving license.
You are eligible to get a driving license.
The If...Else/If...Else...If Ladder
As asked in the above widget, we can use an if-else ladder to check multiple conditions, and this helps us to write a code in a more readable way.
The if-else ladder is a sequence of if-else statements, where each if-else statement is connected to the next one by an else statement.
The syntax of the if-else ladder is as follows:
If the condition n is true, then the code inside the (n+1)th else if block is executed. If the condition n is false, then the code inside the else block is executed. Which means, if no any condition matches then the default code (else block) is executed.
The if-else ladder is a sequence of if-else statements, where each if-else statement is connected to the next one by an else statement.
The syntax of the if-else ladder is as follows:
if ( condition 1 ) {
// code
}
else if ( condition 2 ) {
// code
}
.
.
.
else {
// default code
}
We can clearly see, if the condition 1 is true, then the code inside the if block is executed. If the condition 1 is false, then the condition 2 is evaluated and if it is true then the code inside the else if block is executed. It it was to be false, then the next if else condition would be tested.
// code
}
else if ( condition 2 ) {
// code
}
.
.
.
else {
// default code
}
If the condition n is true, then the code inside the (n+1)th else if block is executed. If the condition n is false, then the code inside the else block is executed. Which means, if no any condition matches then the default code (else block) is executed.
Let's have a look at an example
C
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if ( age > 120 ) {
printf("Really, or are you joking?");
}
else if ( age > 18 ) {
printf("You are eligible to get a driving license.");
}
else if ( age < 18 ) {
printf("You aren't eligible to get a driving license.");
}
else {
printf("You have to wait for a year!");
}
return 0;
}
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if ( age > 120 ) {
printf("Really, or are you joking?");
}
else if ( age > 18 ) {
printf("You are eligible to get a driving license.");
}
else if ( age < 18 ) {
printf("You aren't eligible to get a driving license.");
}
else {
printf("You have to wait for a year!");
}
return 0;
}
Output
Enter your age: 23
You are eligible to get a driving license.
You are eligible to get a driving license.
Explanation
An integer variable age is declared and using the scanf function, the input of the user is taken and stored in age. The if statement checks if age > 120, if it is true, then the block immediately after if is executed, if not, then the else if condition are checked. The first else-if that results true is then excuted. If no else-if statements are true, then it is obvious that the else block is executed.
More preferred example
C
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if ( ( age > 120 ) || ( age <= 18) ) {
printf("You aren't eligible to get a driving license.");
}
else {
printf("You are elligible to get a driving license.");
}
return 0;
}
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if ( ( age > 120 ) || ( age <= 18) ) {
printf("You aren't eligible to get a driving license.");
}
else {
printf("You are elligible to get a driving license.");
}
return 0;
}
Output
Enter your age: 19
You are elligible to get a driving license.
You are elligible to get a driving license.