Loop Discussion


In class the other night, I discussed the fact that pre-increment for loops are no different from post-increment for loops. This fact does not hold for while loops. Run the following code and see the difference:

Note: I compiled this code using MSVC++. In g++ you can change iostream.h to iostream and instead of using \n you can use endl. Also, try to understand the order in which the for loops execute.

#include   "iostream.h"

void
main(int  argc, char *argv[])  {
   int i=5;
   while(i--){
	   cout << i << " ";
   }
   cout << "End of While \n";
   i = 5;
   while(--i){
	   cout << i << " ";
   }
   cout << "End of While \n";
   for(int k = 5; k; k--){
	   cout << k << " ";
   }
   cout << "End of For  \n";
   for(int j = 5; j; --j){
	   cout << j << " ";
   }
   cout << "End of For  \n";
   cin >> i;

}