Course Introduction ------------------- Reading: Chap 2, 4, 7 0) Introduction - start treatment of C++ - language should be familiar because you have learnt Java - C++ and Java have roots in C C | ModulaII, Ada ------------------ / | | / C++ Java - outline - program structure - declarations - expressions - statements - i/o A) C++ Program Structure includes -> include library files, similar to importing packages in Java int // return type main() // entry point in code { // beginning of main declaration // define names & types of variables statements // executable code return 0; // return value of your program } // end of main - Here is a simple program #include int main() { std::cout << "Hello World!" << std::endl; return 0; } - more details of C++ programs below - many of these features will be familiar to you because of Java B) Declarations - specify variable names and types - names: identical to Java: x, y, aA, a_b, 12a (wrong), a-b (wrong) - types: very similar to Java e.g.: int x; float y; double w; char c; - There is a bool type, but you can use integers as well instead of booleans (see below). C) Expressions 1) Arithmetic, boolean - Identical to Java, except for minor differences. e.g. x + 3 y + 5 z / x - C++ allows more type mixing than Java. char c; float f; x/c; - You can cast as in Java int i, y; float x; x = (float)i; y = (int)x; - Boolean expressions evaluate to 0 (false) or 1 (true) e.g. x < y x <= y x == y evaluates to 1 if x is equal to y x != y x || y (x or y, if x = 0, y = 5, x || y is 1) x && y - Combinations of boolean expressions if ((x > y) || (w <= z)) ... explain short cut boolean operators 2) Assignment x = y; assignment statement (x = y) assignment expression, evaluates to left-hand side, LHS Thus z = (x = y); // evaluates to x, assigned to z, // evaluates to z, value of z not used z = x = y = m; - confusing, avoid it - commor error if (x = y) { .... } // not an error but probably not intended! 3) Autoincrement/autodecrement x = x + 1 can be written as ++x or x++ x = x - 1 can be written as --x or x-- pre and post operators have different semantics y = ++x; x = x + 1; y = x; y = x--; y = x; x = x + 1; confusing. historical (increment instruction cheap, compilers weak). avoid use except in certain constructs. D) Statements 1) If (condition) statement; if (condition) statement; else statement; e.g. if (a > b) diff = a - b; else diff = b - a; e.g. if (a > b) { // compound statment with more than one statements diff = a - b; // surrounded by { } root = sqrt(diff); } else { diff = b - a; root = -1 * sqrt(diff); } any statement in the language can be a compound statement selection operator (expression, not a statement) test ? true_expression : false_expression; 2) Switch statement switch (expression) { case XX: statement; break; case YY: statement; break; default: statement; break; } - explain semantics - alternative to nexted if-then-else's 3) While statement while (expression) statement; // explain semantics e.g. std::cin >> n; // explain while (n >= 0) { std::cout << n << std::endl; std::cin >> n; } - no need for a ; at the end of } - same semantics as Java 4) For statement for (expression1; expression 2; expression 3) statement; e.g. for (i = 0; i < n; i++) { std::cout << i; } expression1; while (expression2) { stmt; expression3; } - useful when number of iterations is known E) I/O - please read Section 2.2, 2.6.4 of the book. - output - allows multiple values to be printed in one statement using namespace std; cout << a << b << endl; - formatted output #include - To set width of the next output cout << setw(8); // spaces (right justified) output 8 characters cout << setprecision(2); // precision for floating point numbers cout << 3.423 << endl; // prints "....3.42" - setprecision has to be set once - setw has to be set everytime some output is printed - input int a; cin >> a; - suppose you type in 2.3, followed by newline ___________________ | 2 | . | 3 | \n | |____|____|____|____| - a gets 2; - cin stops at . - if you were to read an integer again, nothing would be read - what if you typed a string instead of a number ___________________ | t | e | n | \n | |____|____|____|____| - a would not get any value - cin would be in a "failed" state after the first read - worse, the following could cause an infinite loop while (a < 10) { cin >> a; cout << a << endl; } - what if you typed 2, followed by space, 3 and newline ___________________ | 2 | | 3 | \n | |____|____|____|____| - a would get 2 only after the newline - a subsequent read would get the 3 value - solution to cin failure - test for failure after each input cin >> x; if (!cin.fail()) { // correct input } else { // bad input } - another way while (cin >> x) { // correct input }