Home Segments Index Top Previous Next

202: Mainline

There are many other ways to define power_of_2 using a for loop. Here is one in which the initialization of the result variable is included within the for statement, along with the initialization of the counter variable, the two being separated with a comma:

int power_of_2 (int n) { 
  int counter, result; 
  for (counter = n, result = 1; counter; --counter) 
    result = result * 2; 
  return result; 
}