Home Segments Index Top Previous Next

320: Mainline

You can even, if you wish, bring the reassignment of the result variable within the reassignment part of the for loop, joining it to the reassignment of the counter variable. The result is a for loop with an empty embedded statement, which, as you may recall from Segment 284, consists of a semicolon only:

int power_of_2 (int n) { 
  int counter, result; 
  for (counter = n, result = 1;         // Initialization 
       counter;                         // Test 
       --counter, result = result * 2)  // Reassignment 
    ;                                   // Empty statement 
  return result; 
}