Lesson 5.2. Nested if and indentation

Analyze the following code. Knowing that a>b, what does this line display?

if ( a>b ) if ( b>a ) printf (“OK”); else printf (“KO”);

This example raises two questions:

Nested if

When if are nested, an else always refers to the last if encountered that has not yet been assigned an else.

In our initial example, we see below that the code displays KO, so the else refers to the last if, i.e. the if ( b>a ) test.

By the way, note that the compiler alerts us to this ambiguous writing:

main.c:7:40: warning: add explicit braces to avoid dangling else [-Wdangling-else]
  if ( a>b ) if ( b>a ) printf ("OK"); else printf ("KO");

Indenting

If you have been following this course since the beginning, you probably noticed that the source codes are always presented in the same way. After the opening curly braces, the code is shifted to the right:

if (var > 1) {
    printf ("Message");
    if (var > 2) {
        printf ("Another message");
        if (var > 3) {
            printf ("One more message");
            if (var > 4) {
                ...

This type of code presentation is called indenting. The rule is simple:

If the curly brace is omitted because there is only one statement in the block, this instruction is still indented. Let's go back to the example in the introduction, this code layout is much more understandable:

if ( a>b ) {

    if ( b>a ) 
        printf ("OK"); 
    else 
        printf ("KO");
}

If indentation does not change the execution of the code, it is essential to structure the code; primarily for you, but also for others. Some IDEs offer automatic indentation. On Qt Creator, select your code (ctrl-a to select all), then ctrl-i to automatically indent the selection.

Tabulation or spaces?

To indent a code, you can use the tab key or put spaces, usually 2 or 4. Whichever you choose, it will not change the execution of the of the program. The files are smaller with tabs, but with spaces the display of the code will be more consistent from one editor to another.

It's funny to note that this debate has animated the developer community for years. This debate is so heated that the authors of the Silicon Valley series devoted a scene to it:

Personally, I'm an unconditional fan of tabs.

Tabs are better! 😉

Exercise

Using nested if, write a program that displays the honors of a grade entered by the user:

Enter your grade: 13.8
With honors

Quiz

What does the following code display?

if (1) if (0) printf ("Crac"); else printf ("Boum");
Check Bravo! The else is associated to the if (0). Try again...

What does the following code display?

if (1) {
    if (0) 
        printf ("Crac"); 
    else 
        printf ("Boum");
printf ("Hue");
}
Check Bravo! This code is badly indented, it can be misleading. Try again...

What does the following code display?

int score = 75;
if (score>=95) printf ("Grade A\n");
if (score>=85) printf ("Grade B\n");
if (score>=73) printf ("Grade C\n");
if (score>=64) printf ("Grade D\n");
Check Bravo! There is no else, so multiple tests can run. Try again...

Why indenting your code is important

Check Bravo! In professional world, indenting and commenting your code is not optional. Try again...

See also


Last update : 11/22/2022