//----------------------------------------------------------- // // control.cpp // // A program that illustrates the basic control structures in C++ // // Simon Parsons // // 8th February 2010 // //----------------------------------------------------------- // // Libraries etc. #include using namespace std; //----------------------------------------------------------- // // main // int main() { int i, count; // A for loop that counts to five. count = 0; for ( i=0; i<5; i++ ) { count++; } cout << "for ( i=0; i<5; i++ ) ... count=" << count << endl; // A while loop that counts to five. count = 0; i = 0; while ( i<5 ) { count++; i++; } cout << "i=0; while ( i<5 ) {...i++} count=" << count << endl; // A do/while loop that counts to five. count = 0; i = 0; do { count++; i++; } while ( i<5 ); cout << "i=0; do ... i++; while ( i<5 ) ... count=" << count << endl; cout << endl; // Two for loops that increment 2 at a time count = 0; for ( i=0; i<5; i+=2 ) { count++; } cout << "for ( i=0; i<5; i+=2 ) ... count=" << count << endl; count = 0; for ( i=0; i>5; i+=2 ) { count++; } cout << "for ( i=0; i>5; i+=2 ) ... count=" << count << endl; cout << endl; // Two while loops that increment 2 at a time count = 0; i = 0; while ( i<5 ) { count++; i+=2; } cout << "i=0; while ( i<5 ) {...i+=2;} count=" << count << endl; count = 0; i = 0; while ( i>5 ) { count++; i+=2; } cout << "i=0; while ( i>5 ) {...i+=2;} count=" << count << endl; cout << endl; // Two do/while loops that increment 2 at a time count = 0; i = 0; do { count++; i+=2; } while ( i<5 ); cout << "i=0; do ... i+=2; while ( i<5 ) count=" << count << endl; count = 0; i = 0; do { count++; i+=2; } while ( i>5 ); cout << "i=0; do ... i+=2; while ( i>5 ) count=" << count << endl; } // // End of main // //-----------------------------------------------------------