The if statement in C

In programming, the if statement executes a block of code only if a certain condition is met. We can add the else statement to execute another block when the condition is false.

If statement flow chart

Advertise on this site. I promise you will like the rates :)

If statement syntax

The syntax is as follows:

  • the keyword if
  • followed, by the condition, enclosed by parenthesis
  • followed by the block to execute
    if(condition)
    {
        block to execute
    }

In most programming languages the condition is any expression that results in a Boolean value.

Until C99 C did not have a boolean data type. Instead, it always uses 0 and ‘\0’ as false and everything else as true. C99 introduces the _Bool keyword and the header <stdbool.h>, which holds the macros bool, true, false. All that is just for convenience, true and false are still represented as 1 and 0.

In C the condition could be pretty much any expression that can be evaluated. If the result is different from 0 or ‘\0’ the block will be executed.

Using if in C

Here we read a number and use it as a condition.

    int number;
    scanf("%d", &number);
    if(number)
    {
        printf("The number is not a zero.\n");
    }

Let’s read two numbers and see if they are equal:

    int number_1, number_2;
    scanf("%d", &number_1);
    scanf("%d", &number_2);
    if(number_1 == number_2)
    {
        printf("The numbers are equal!");
    }

We can skip the curly brackets {} of the executable block when it contains only one statement. This is equivalent to the above example:

  int number_1, number_2;
    scanf("%d", &number_1);
    scanf("%d", &number_2);
    if(number_1 == number_2)
        printf("The numbers are equal!");

if..else statement

The else statement executes an alternative block when the condition is false.

if..else statement flow chart

else cannot be used on its own. It must be used with an if. The two blocks will never be executed at the same time.
Here is the syntax:   

    if(condition)
    {
        block to execute
    }
    else
    {
        alternative code block
    } 

Using this, let’s check if two symbols are the same:

    char symbol_1, symbol_2;
    scanf("%c%c", &symbol_1, &symbol_2);
    if(symbol_1 == symbol_2)
    {
        printf("The symbols are the same!");
    }
    else
    {
        printf("The symbols differ!");
    }

Nested conditions

We can put an if statement within another if(or else). We call this nested ifs. We use them for more complex tasks.
Check whether your speed is over the limit:

    int speed, limit;
    scanf("%d%d", &speed, &limit);
    if(speed < limit)
    {
        printf("The speed is within the limit. No problem!");
    }
    else
    {
        if(speed == limit)
        {
            printf("The speed is at the limit, be careful!");
        }
        else
        {
            printf("You are going too fast!");
        }
    }

Here is another example, using nested conditions:

    int drinks_count;
    printf("How many drinks you had last night?\n");
    scanf("%d", &drinks_count);
    if(drinks_count == 0)
    {
        printf("That was good for your health.");
    }
    else
    {
        if(drinks_count <= 3)
        {
            printf("Had some fun?");
        }
        else
        {
            if(drinks_count <= 5)
            {
                printf("Is the hangover gone?");
            }
            else
            {
                printf("%d! And you remember that?", drinks_count);
            }
        }
    } 

To do more checks you can use series of if-else statements, however if they are more than 2-3 consider using the switch-case-default mechanism.

Exercise

  1. Write a program that asks the user to input the prices of two products. Check which is cheaper and print it as an output.
  2. Do the previous task, but this time with 3 prices.
  3. Do the second task, but output all prices in ascending order. Test your program with different inputs.

This article is part of two tutorials: Basic C tutorial and keywords in C. Continue accordingly:

C tutorial

Previous lesson: Operators in C

Next lesson: switch statement

Keywords

Previous keyword: goto

Next keyword: int

   Search this site: