* Boolean type #include int main() { bool x; bool y; bool z; x = true; y = false z = x && y cout << x << " AND " << y << " = " << z << endl; z = x || y cout << x << " OR " << y << " = " << z << endl; z = !x; cout << "NOT " << x << " = " << z << endl; return 0; } first 5 lines can be written bool x = true, y = false, z; #include int main() { int x = 1, y = 2; bool z; z = x == y; cout << x << " == " << y << " = " << z << endl; z = x != y; cout << x << " != " << y << " = " << z << endl; z = x < y; cout << x << " < " << y << " = " << z << endl; z = x > y; cout << x << " > " << y << " = " << z << endl; z = x <= y; cout << x << " <= " << y << " = " << z << endl; z = x >= y; cout << x << " >= " << y << " = " << z << endl; return 0; } * if statement if(bool-statement) { statement1; statement2; ... } #include int main() { int x; const int MAGIC_NUMBER = 5; cout << "Enter a number between 1 and 10: "; cin >> x; if(x == MAGIC_NUMBER) { cout << "You chose the magic number!\n"; } return 0; } What if you also wanted to say something when they didn't choose the magic number? if(bool-expression) { ... } else { ... } Every else must match to an if. int main() { int x; const int MAGIC_NUMBER = 5; cout << "Enter an integer: "; cin >> x; if(x == MAGIC_NUMBER) { cout << "You chose the magic number!\n"; } else { cout << "You chose the wrong number! You lose!\n"; } return 0; } If we have to be within one of the magic number: if((x <= MAGIC_NUMBER + 1) && (x >= MAGIC_NUMBER - 1)) Beware of precedence: cout << x || y; // Will not work because || is higher precedence than << Look at Display 2.3 for information on precedence for each operator. if(bool-expression) { ... } else if(bool-expression) { ... } else { ... } Last else optional. Can have as many else if's as you want, but only one else. Can nest if's if(bool-expression) { ... if(bool-expression) { ... } ... } else { ... if(bool-expression) { ... } ... } conditional operator expression bool-expression ? expr1 : expr2 expr1 and expr2 must be of the same type. The whole expression evaluates to the type of expr1 and expr2. #include int main() { int x = 3, y = 2; int max; max = x > y ? x : y; return 0; } * Switch statement switch(expr) { case const1: statements; break; case const2: statements; break; ... default: statements; break; } const's must all be of the same type, and expr must be of that type. if you don't put the break, the program will continue to process the statements of the next case. #include int main() { int choice; cout << "1 - First Option " << endl; cout << "2 - Second Option " << endl; cout << "3 - Third Option " << endl; cout << "4 - Fourth Optoin " << endl << endl << endl; cout << "Choice :"; cin >> choice; switch(choice) { case 1: cout << "You chose the First Option" << endl; break; case 2: cout << "You chose the First Option" << endl; break; case 3: cout << "You chose the First Option" << endl; break; case 4: cout << "You chose the First Option" << endl; break; default: cout << "You did not choose a valid option" << endl; break; } return 0; } * loops * what if you wanted to write a program that writes out the first n integers, starting with 1, where n is given by input from the user? #include int main() { int i = 1; cout << "How many integers do you want me to output? "; cin >> n; if(n < 1) { cout << "Your integer must be greater than or equal to 1" << endl; return 0; } while(i < n) { cout << i << " "; i++; } cout << endl; return 0; } syntax of while: while(bool-expr) { ... } does everything in the curly braces over and over again until bool-expr is false. if bool-expr is false to begin with, nothing in the curly braces every happens. beware of infinite loops classic infinite loop while(true) {} forget to update index while(i < n) { cout << i << " "; } while statements can represent all loops, but some types are more easily expressed with different syntax. if you always want the statements in the curly braces to happen at least once: do { ... } while(bool-expr); this is just the equivalent of: ... while(bool-expr) { ... } another commonly used loop structure for(initialization; bool-expr; update) { ... } this is the equivalent of initialization; while(bool-statement) { ... update; } let's say we wanted to figure out the sum of the first n integers starting with 1. #include int main() { int n; int i; int total = 0; cout << "How many consecutive integers do you want to sum? "; cin >> n; if(n < 1) { cout << "Your integer must be greater than or equal to 1" << endl; return 0; } for(i = 1; i <= n; i++) { total = total + i; } cout << "1 + ... + " << n << " = " << total << endl; return 0; } Similarly we could write a program that prints out any arithmetic series. #include int main() { int n; int a; int d; int i; cout << "Arithmetic series are of the form an + d\n"; cout << "What is the value of a? "; cin >> a; cout << "What is the value of d? "; cin >> d; cout << "How many values in the series do you want to view, at least 1? "; cin >> n; if(n < 1) { cout << "You must view at least one value" << endl; return 0; } for(i = 0; i < n; i++) { cout << a * i + d << " "; } return 0; } Factorial: #include int main() { int n; int i; int total = 1; cout << "Enter the num you want to take a factorial of: "; cin >> n; for(i = 1; i <= n; i++) { total = total * i; } cout << n << "! = " << total << endl; return 0; } Euclid's Algorithm: gcd(a,b) = if b == 0 then abs(a), else gcd(b,a % b) To translate into a loop: #include int main() { int a, b, r; cout << "a: "; cin >> a; cout << "b: "; cin >> b; cout << "gcd(" << a << "," << b << ") = "; while(b != 0) { r = a % b; a = b; b = r; } cout << (a < 0 ? -a : a) << endl; return 0; }