Close
Close

if and else in C


From here, the real coding part begins. Gear up to travel to the next part of coding. This will be a lot of fun.

Many times, we need to first see the condition and then make a decision.
As an example, consider that a chocolate costs 10 rupees. So, if you have 10 rupees or more, you can buy the chocolate.

if and else in c

But how do we represent this scenario in C?

This type of decision taking is done by using if statement in C.

if statement


Let's consider an example.

#include <stdio.h>
int main()
{
    int a = 5;
    int b = 5;
    if ( a == b )
    {
        printf ( "a and b are equal\n" );
    }
    return 0;
}
Output
a and b are equal

if (a==b)a==b is the condition. Conditions are written inside '()'. Here, condition is true (will return 1). Since the condition is true, statements inside if will be executed.

{} after if represents the body of if. Whatever is written inside '{}' is part of if.

So, the flow is that first the condition of if is checked and if it is true then statement(s) inside if are executed.

#include <stdio.h>
int main()
{
if ( condition )
    {
        statement;
        statement;
        statement;
        ....
    }
    return 0;
}

if...else statement


Now, consider that a chocolate costs 10 rupees and a candy costs 5 rupees. So, if you have at least 10 rupees, you can buy a chocolate, otherwise, you have to buy candy.

In the world of programming, this is done by using if...else statement in C.

Now, let's see the same example as above but with the if...else statement.

#include <stdio.h>
int main()
{
    int a = 5;
    int b = 8;
    if (a == b)
    {
        printf ("a and b are equal\n");
    }
    else
    {
        printf ("a and b are not equal\n");
    }
    return 0;
}
Output
a and b are not equal

In the same way as above, firstly, the condition in if will be checked and since, it is false (5 and 8 are not equal), so statements in else are executed. It is that simple.

#include <stdio.h>
int main()
{
    if(condition)
    {
        statement;
        statement;
        ....
    }
    else
    {
        statement;
        statement;
        ....
    }
    return 0;
}

If the condition is true, statements in if are executed. Otherwise, statements in else are executed.

But what is its use?

It has multiple applications in various fields like robotics and many other. For now, let's consider an example that a person is eligible to vote in an election only if his age is more than 18 years.

The C program for that will be:

#include <stdio.h>
int main()
{
    int age ;
    printf ( " Enter your age " );
    scanf ( "%d" ,&age) ;
    if ( age >= 18 )
    {
        printf ( " Your age is 18+.\n" );
        printf ( " Eligible to vote\n" );
    }
    else
    {
        printf ( " Your age is not yet 18.\n" );
        printf ( " not eligible to vote\n" );
    }
    return 0;
}
Output
Enter your age 20
Your age is 18+.
Eligible to vote

The above example can be understood easily. The condition in if is age >= 18. This means that if 'age' is more than or equal to 18 then it will print statements inside if, otherwise statements inside else will be printed.

else if statements


Many times we fall in situations when 'if' and 'else' are not sufficient. For example, if you have 5 rupees then you will buy candy, or if you have 10 rupees, then chocolate and if more than 100, then cake. Thanks to C, because it provides another tool 'else if' to get this thing done.

Consider this example:

#include <stdio.h>
int main()
{
    int a = 10;
    if(a==30)
    {
        printf("It is 30\n");
    }
    else if(a==10)
    {
        printf("It is 10\n");
    }
    else if(a==5)
    {
        printf("It is 5\n");
    }
    return 0;
}
Output
It is 10

This is a very simple code only to make you understand the use of else if. There can be any number of else if between if and else.

#include <stdio.h>
int main()
{
    if(condition)
    {
        statement
        statement
        ...
    }
    else if(condition)
    {
        statement
        statement
        ...
    }
    else if(condition)
    {
        statement
        statement
        ...
    }
    else
    {
        statement
        statement
        ....
    }
    return 0;
}

If there is a single statement inside if, else or else if, we can skip the braces ({}). Let's look at an example.

#include <stdio.h>
int main()
{
    int a = 10;
    if(a==30)
        printf("It is 30\n");
    else if(a==10)
        printf("It is 10\n");
    else if(a==5)
        printf("It is 5\n");
    return 0;
}
Output
It is 10

As you can see in the above example that a single statement is considered as a part of if, else and else if respectively without any braces ({}).

Nested if/else


We can also use if or else inside another if or else. See the example to print whether a number is greatest or not to understand it.

#include <stdio.h>
int main()
{
    int a = 8 ;
    int b = 4 ;
    int c = 10 ;
    if ( a > b )
    {
        if ( a > c )
        {
             printf ( "a is the greatest number.\n" ) ;
        }
    }
    return 0;
}
Output

In the above example, the first expression i.e., (a>b) is true. So the statements enclosed in curly brackets {} of the first if condition are executed.

Within the curly brackets, the first statement i.e., if(a>c) will be executed first. Since this condition is false, the statement printf(" a is the greatest number."); within the curly brackets of this if condition will not be executed.

So, first we checked if 'a' is greater than 'b' or not, if it is, then we compared it with 'c'.

Isn't that simple?

See this example of finding greatest number by taking input by the user.

#include <stdio.h>
int main()
{
    int a = 8 ;
    int b = 4 ;
    int c = 10 ;
    printf("Enter three numbers\n");
    scanf("%d %d %d",&a,&b,&c);
    if(a>b && a>c)
    {
        printf("%d is greatest\n",a);
    }
    else if(b>a && b>c)
    {
        printf("%d is greatest\n",b);
    }
    else if(c>a && c>b)
    {
        printf("%d is greatest\n",c);
    }
    return 0;
}
Output
Enter three numbers
4
8
2
8 is greatest

In the above example, we have three numbers a, b and c and we have to find the greatest among them. For that, we will first compare the first number with other numbers i.e., 'a' with 'b' and 'c' both. Now, if the condition (a>b && a>c) is true (which means that a is the greatest), then the statements enclosed in the curly brackets {} of the first if condition will be executed. If not so, then it will come to else if and check for (b>a && b>c). If this condition is true, then corresponding statements will be executed otherwise it will check the condition given in the last else if.

Another form of if else - Ternary Operator


We can also judge the condition using ternary operator. Ternary operator checks whether a given condition is true and then evaluates the expressions accordingly. It works as follows.

condition ? expression1 : expression2;

If the condition is true, then expression1 gets evaluated, otherwise expression2 gets evaluated.

#include <stdio.h>
int main()
{
    int age;
    printf("Enter age");
    scanf("%d", &age);
    (age > 18) ? printf("eligible to vote\n") : printf("not eligible to vote\n");
    return 0;
}
Output
Enter age
10
not eligible to vote

Here, if the condition (age > 18) is true, then expression1 i.e. printf("eligible to vote\n") will get evaluated, otherwise expression2 i.e. printf("not eligible to vote\n") will get evaluated. Since the value of age that we entered (10) is less than 18, expression2 got evaluated and "not eligible to vote" got printed.

Let's see another example in which we want to find the greater among two numbers.

#include <stdio.h>
int main()
{
    int num1 = 4, num2 = 5, num;
    num = (num2 > num1) ? num2 : num1;
    printf("The greater number is %d\n", num);
    return 0;
}
Output
The greater number is 5

In this example, if the condition (num2 > num1) is true, then 'num2' i.e. 5 will get assigned to 'num', otherwise 'num1' i.e. 4 will get assigned to 'num'. In our case, the values of 'num1' and 'num2' are 4 and 5 respectively. Thus, the condition 'num2 > num1' is true and the value of num2 i.e. 5 got assigned to num and so 5 got printed.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.
Knowledge is of no value unless you put it into practice.
- Anton Chekhov

Ask Yours
Post Yours
Doubt? Ask question