jeudi 24 mai 2018

If Else statement indentation important or not? (C++)

Does the indentation in an if else statement have any bearing on the execution of the code or is it just something to do for cleaner codes?

example from the book Accelerated C++ written by Andrew Koening:

while(c != cols){
    if(r == pad + 1 && c == pad + 1){
      cout << greet;
      c+=greet.size();
    } else {

      if(r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
        cout << "*";

      else
        cout << " ";
        ++c;


    }

  }

The prefix increment of c is executed regardless of whether r=0 or not but I dont understand why.

If the if statement turns true an asterisk is printed. IF not a blank space is printed and c is incremented. Thats how I am reading it but c gets incremented regardless of what the values of r or c are.

This is what it says in the book but theres no explanation I could find:

Notice how the different indentation of ++c; draws attention to the fact that it is executed regardless of whether we are in the border.

Aucun commentaire:

Enregistrer un commentaire