August 2012
Intermediate to advanced
976 pages
30h 17m
English
Assignments often occur in conditions. Because assignment has relatively low precedence, we usually must parenthesize the assignment for the condition to work properly. To see why assignment in a condition is useful, consider the following loop. We want to call a function until it returns a desired value—say, 42:
// a verbose and therefore more error-prone way to write this loopint i = get_value(); // get the first valuewhile (i != 42) { // do something ... i = get_value(); // get remaining values}
Here we start by calling get_value followed by a loop whose condition uses the value returned from that call. The last statement in this loop makes another call to get_value, and the loop repeats. We can write ...
Read now
Unlock full access