mercredi 28 juin 2017

How are consecutive, non-nested if statements evaluated?

This is a nonsense example, but I couldn't find an answer to this type of question anywhere. As you can see, the if-statements are not nested. Does line 11 evaluate as true or not after the first if-statement changes the value of k from 1 to 0? This question is similar to one given to me as a practice study question, so I'm not looking for code improvements at this time. Thanks.

1   int main {
2
3     int i = 3, j = 0, k = 1; // variables are initialized
4
5     if ( i < 5 ) {  // evaluates as true
6       i++;
7       k--;
8     } // i now equals 4 and k equals 0   
9     else { ... }
10
11    if ( k > 0 ) // TRUE OR NOT?
12    { ... }
13  } // end main

Aucun commentaire:

Enregistrer un commentaire