Proof of Correctness: factorial.c

prev     next

Step4: INVARIANT's restriction on index: y <= x + 1 and EXIT prove "eyeballs".

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
 
  // INVARIANT:                y <= x + 1 and EXIT: y > x
  //                y = x + 1 // "eyeballs"      
  // POST-CONDITION: f = x!        

  return f;
}