Jul 26 - Overview, Output, Arithmetic, Vars and Types, Input, Branches * Hand out syllabus * Discuss * Hand out homework * Hello, World #include int main() { cout << "Hello, World!\n"; return 0; } * Arithmetic #include int main() { cout << 2; return 0; } cout << 2 + 2; cout << 3 - 2; cout << 2 * 3; cout << 6 / 2; cout << 4 / 3; cout << 31 % 4; cout << ((2 + 2) + (3 - 1) * (2*3)) / (8 / 2); cout << 2.0 / 3.2; * Types, integers, floats * Float downcasts to int. 3.0 / 2 = 1 * Variables int x; float y; int z; x = 2; y = 3.0; z = x / y; cout << z; // Prints 1 * lvalues and rvalues * const float const PI = 3.14159 * ++, --, +=, -=, *=, /= * cin #include int main() { float pricePerGallon; int numGallons; cout << "What is the price per gallon of gas? "; cin >> pricePerGallon; cout << "How many gallons do you want to buy? "; cin >> numGallons; cout << "The total price is "; cout << pricePerGallon * numGallons; cout << "\n"; return 0; } Last three lines can be written cout << "The total price is " << pricePerGallon * numGallons << endl; * 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; }