CIS-60 Home http://www.c-jump.com/CIS60/CIS60syllabus.htm
if ( condition ) { statement true; } else { statement false; }
#include <iostream> void main() { int x = -1; if ( x > 0 ) { std::cout << "x is a positive number"; } else { std::cout << "x is a negative or zero"; } }
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> void 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"; } }
while ( expression ) { statement; }
void main() { int x = 0; while ( x < 5 ) { // condition // loop body: x = x + 1; } }
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> void main() { int x = 0; for ( x = 0; x < 5; ++x ) { std::cout << x; } }
#include <iostream> void main() { int x = 0; for ( x = 0; x < 5; ++x ) { std::cout << x; } }
#include <iostream> void main() { for ( ;; ) // forever { // "endless" loop: char answer; std::cout << "Exit program? Y/N:"; std::cin >> answer; if ( answer == 'Y' || answer == 'y' ) { return; } } }
#include <iostream> void main() { int idx = 0; // Prints 01234: do { std::cout << idx; } while ( ++idx < 5 ); }
#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> void 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; } }
void main() { int x = 0; while ( x < 5 ) { // condition // loop body: x = x + 1; if ( x == 3 ) break; } }
void main() { int x = 0; if ( x < 5 ) { break; // Error: illegal break } }
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... }