Does
while ( attempt < 3 && !open )
do what we want?
Yes, it means:
while ( the user has made less than 3 tries AND the lock is NOT open )
The Boolean expression
attempt < 3 && !open
Should evaluate to true
when the user gets the first or another chance,
and to false
when the user does not get another chance.
The table shows how this works:
Number of tries | open | attempt < 3 | !open | attempt < 3 && !open | Another Chance? |
---|---|---|---|---|---|
0 | false | true | true | true && true | true |
1 | false | true | true | true && true | true |
1 | true | true | false | true && false | false |
2 | false | true | true | true && true | true |
2 | true | true | false | true && false | false |
3 | false | ? | ? | ? && ? | ???? |
3 | true | ? | ? | ? && ? | ???? |
The logic in a computer program is often this complicated (or worse!) A chart like this may be necessary to check that the logic is correct.