Intro to C++ Programming http://www.c-jump.com/bcc/c155c/c155syllabus.htm
if ( condition ) { statement true; } else { statement false; }
#include <iostream> int main() { int x = -1; if ( x > 0 ) { std::cout << "x is a positive number"; } else { std::cout << "x is a negative or zero"; } return 0; }
if ( condition-1 ) { statement; // condition-1 is true } else if ( condition-2 ) { statement; // condition-2 is true } else if ( condition-3 ) { statement; // condition-3 is true } else { // default case: statement; // all above conditions were false }
#include <iostream> int main() { int x = -1; if ( x > 0 ) { std::cout << "x is positive"; } else if ( x < 0 ) { std::cout << "x is negative"; } else { std::cout << "x is zero"; } return 0; }
while ( expression ) { statement; }
int main() { int x = 0; while ( x < 5 ) { // condition // loop body: x = x + 1; } return 0; }
for ( expr1; expr2; expr3 ) { statement; }is similar to
expr1; while ( expr2 ) { statement; expr3; }
for ( init; condition; increment ) { statement; } ...
for ( init; true; increment ) { statement; } ... for ( init; true; increment ) { statement; } ... for ( init; true; increment ) { statement; } ... for ( init; true; increment ) { statement; } ... for ( init; true; increment ) { statement; } ... for ( init; true; increment ) { statement; } ... for ( init; true; increment ) { statement; } ... for ( init; false;increment ) { statement; } ... for ( init; false;increment ) { statement; } ...
#include <iostream> int main() { int x = 0; for ( x = 0; x < 5; ++x ) { std::cout << x; } return 0; }
#include <iostream> int main() { int x = 0; for ( x = 0; x < 5; ++x ) { std::cout << x; } return 0; }
#include <iostream> int main() { for ( ;; ) // forever { // "endless" loop: char answer; std::cout << "Exit program? Y/N:"; std::cin >> answer; if ( answer == 'Y' || answer == 'y' ) { return 0; } } return 0; }
#include <iostream> int main() { int idx = 0; // Prints 01234: do { std::cout << idx; } while ( ++idx < 5 ); return 0; }
#include <iostream> int main() { std::cout << "Do you want to proceed (y or n)?\n"; char answer = 0; std::cin >> answer; // get answer from user switch ( answer ) { case 'y': return true; case 'n': return false; default: std::cout << "Sorry, I don't understand\n"; std::cout << "I'll take that for a no"; return false; } }
#include <iostream> int main() { bool proceed = false ; std::cout << "Do you want to proceed (y or n)? "; // display question char answer = 0; std::cin >> answer; // read answer switch ( answer ) { case 'Y': case 'y': proceed = true; break; default: std::cout << "Sorry, I don't understand\n"; std::cout << "I'll take that for a no\n"; case 'N': case 'n': proceed = false; break; } return 0; }
int main() { int x = 0; while ( x < 5 ) { // condition // loop body: x = x + 1; if ( x == 3 ) break; } return 0; }
int main() { int x = 0; if ( x < 5 ) { break; // Error: illegal break } return 0; }
Inside for, control passes to the increment step.
for ( i = 0; i < n; ++i ) { int result = get_data( i ); if ( result < 0 ) // skip negative values continue; // process zero and positive values... }