[prev] 40 [next]

Assignments (cont)

C assignment statements are really expressions
  • they return a result: the value being assigned
  • the return value is generally ignored
Frequently, assignment is used in loop continuation tests
  • to combine the test with collecting the next value
  • to make the expression of such loops more concise
Example: The pattern

v = getNextItem();
while (v != 0) {
   process(v);
   v = getNextItem();
}

can be written as

while ((v = getNextItem()) != 0) {
   process(v);
}