Search Result
Details here...

If...Else Statement

Previous

Control Structure in C

Next

For Loop in C


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:
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.

The syntax of If...Else statement is as follows:
if ( Condition ) {
// Code
} else {
// Code
}

Woow!

We can also have nested if-else statement, i.e; an if-else inside an if-else.

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;
}

Output

You are not eligible for driving license

Remember

You can always ommit the else part of the if-else statement in C, if you donot need it.

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.

Did you get it?

If the condition in the above statement were to be evaluated to true, then the code block inside the if block would be executed, the else part would be ommited, and the control would be 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;
}

Output

Enter your age: 19
You are eligible to get a driving license.

What if?

In the above example, when age is assigned 18, you wanted to output the user to be patient and wait for a year. Could we use another if part? Think about it.

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 ( 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.

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;
}

Output

Enter your age: 23
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.

Did you notice?

The block under first true condition is executed. If no any condition is true, then the default code (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;
}

Output

Enter your age: 19
You are elligible to get a driving license.

Remember

We can always use relational operator to shorten the ladder. But, don't mesh up with the brackets.
Also, it is always safer to use brackets for the sake of readability, and more.

Remember

The variables defined inside the if-else statement are local to the if-else block only, and defined only in the scope of the if-else block. Hence, outside the if-else block, the variables are not accessible. We will be covering the concept of global variables and scopes in the coming chapters.

By darpan.kattel
Previous

Control Structure in C

Next

For Loop in C