Proof of Correctness: factorial.c

prev     next

Step2: EXIT condition (y > x) is ALWAYS the negation of the while condition (y <= x).

int factorial(int x) {
int f, y;
  // PRE-CONDITION: x >= 0              
 
  y = 1;
  // y = 1  
  
  f = 1;
  // y = 1 and f = 1
  
                                                                 
  
  while (y <= x) {
    
                                                                   
    
    f = f * y;
    
                                                                   
    
    y = y + 1;
                                                                   
  
  } // end while
 
  //                                          EXIT: y > x
  // f = (y-1)! and y = x + 1      
  // POST-CONDITION: f = x!        

  return f;
}