0

I'm just starting a beginner's guide to games programming tutorial in C. I'm a little confused with the code below.

At main gameOverTick is set to zero, then we have a case when the game is over

case GAME_OVER:
    printStatus("GAME OVER!!! The evil DarkLord was defeated");
    if (++gameOverTick % 100 == 0){
        status = PRINT_GAME_WELCOMESCREEN;  // go back to welcome screen
        gameOverTick = 0;           // reset gameOverTick
    }

I would just like to know what role the ++ (pre-inc) operation does on gameOverTick. Does it set gameOverTick to 1 whilst checking the if, or set it to 0 somehow. I know how post-inc ++ works, but this is a new one for me.

Thanks

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
DigiSweep
  • 51
  • 1
  • 2
  • 7
  • It reset to 0 when gameOverTick become 100. – Jayesh Bhoi May 22 '14 at 11:07
  • If `gameOverTick` is only modified here (and thus is always between 0 and 100), a division **and** a conditional has to be the most expensive way to reset a variable when it reaches some ceiling. – Pascal Cuoq May 22 '14 at 11:11
  • It's the same as post-inc except that the value used in the expression has `1` added to it. This can be implemented as incrementing the variable and then reading it, in most situations. – M.M May 22 '14 at 11:16

4 Answers4

12

We've got four answers here and they are all wrong in the same way. Let me make sure this is very clear in your mind: people who tell you that precedence of operators determines the order in which side effects occur as a result of computation of subexpressions are simply wrong. In C, the order in which subexpressions are computed is not controlled by precedence; it is largely undefined. (In C# and Java is is defined not as precedence order but as left-to-right.)

In the specific case here, the value of the ++ operation must be computed before the % happens, but the assignment associated with the ++ operation can happen at any time. In your particular case it doesn't matter when the assignment happens, but it is easy to construct cases where it does matter.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
0

This increases gameOverTick by one before the modulo is calculated and the comparison is done. (A good rule of thumb in C is that unitary operations have tighter binding than binary, and calculations over logic operations.) It guess the purpose of this is to wait 100 loops until it goes hack to the welcome screen, as gameOverTick is only reset if it have gone up to 100.

fweik
  • 441
  • 3
  • 5
0

Most expressions in C have a value; most expressions have a side effect.

Consider the two expressions below

/* a */ i++
/* b */ ++i

The side effect of both expressions is to increase the value of i (that change is only usable after the next sequence point). The value of the first expression is the value i had on the previous sequence point; the value of the second expression is one more than what i had at the previous sequence point.

pmg
  • 106,608
  • 13
  • 126
  • 198
-8

The code you have written inside parenthesis of if is called an expression, the expression evaluation always follow operator precedence rule, considering the given expression

1.++ operator has highest precedence so '++gameOverTick' is evaluated first as '(gameOverTick = gameOverTick+1)'

2.The next next precedence is for '%' operator so '(gameOverTick+1)%100' is calculated

3.Finally the least precedence operator is '==' hence the obtained result on left side of '==' operator is compared for equality with the right side value.

Example:

-Consider gameOverTick value as 99

-99 is incremented first i.e, 99+1 = 100

-Than the % operation is performed i.e, 100%100 = 0

-Now equality is compared` i.e, 0 == 0 which gives 1

-The evaluation of expression gives 1 hence it is equal to if(1), which indicates a true.

For more on operator precedence you can refer this link http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

prince
  • 1,129
  • 1
  • 15
  • 38
  • Could you explain on what basis a expression is evaluated? – prince May 22 '14 at 11:36
  • 6
    **This answer is deeply misleading. Expression evaluation order does not follow precedence order.** Consider `x = A() + B() * C()`. This can be computed as `a = A(); b = B(); c = C(); d = b * c; x = a + d;` Or `c = C(); a = A(); b = B(); d = b * c; x = a + d;` or `b = B(); c = C(); d = b * c; a = A(); x = a + d;` or several other ways. **Just because `*`is higher precedence than `+` does not mean that the operands of the times are evaluated before the operands of the addition**. C allows *any* ordering of evaluation of subexpressions; C# and Java require left-to-right order. – Eric Lippert May 22 '14 at 15:40
  • 2
    In particular your answer is wrong in the statement that the side effect of the `++` must happen before the `%` is computed. This is completely wrong. The **value** of the `++` must be computed before the `%` but the **assignment** can happen at *any* time before the sequence point ends. – Eric Lippert May 22 '14 at 15:43