Download presentation
Presentation is loading. Please wait.
1
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September 2008
2
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 2 3.3 Expressions, Statements, and Blocks
3
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 #include int abc (...) {... ; return... ; } int cde (...) {... ; return... ; } int main() {...; return 0; } A Typical Program function statements
4
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 A C++ program comprises a number of functions. A function comprises a number of statements. A statement can be as simple as Can be as complicated as A statement must end with a semicolon ; In a statement, space carries no information. Statements x = a + b; if (choice == Sunday || choice == Saturday) cout << "\nYou're already off on weekends!\n"; x = a + b;
5
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 Expressions A statement comprises one or many expressions. Anything that returns a value is an expression. Examples of expressions 3.2 // Returns the value 3.2 studentPerClass // Returns a constant, say 87 x = a + b; // Here, two expressions // Return a + b and return the value of a variable x Since x = a + b is an expression (i.e. return a value), it can certainly be assigned to another variable. For example, y = x = a + b; // assign y to the value of x which is // equal to the sum of a and b
6
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 Operators An expression often comprises one of more operators. The assignment operator ' = ' assigns a value to a variable or constant, e.g. x = 39; Several mathematical operators are provided by C++. Let a be 3, b be 4 and x is declared as an integer, Operators Meaning +Add -Subtract *Multiply /Divide %modulus Examples x = a + b;// x = 7 x = a - b;// x = -1 x = a * b;// x = 12 x = a / b;// x = 0 (why?) x = a % b;// x = 3 (why?) 3 modulo 4 is 3
7
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 Integer and Floating Point Divisions #include using namespace std; void intDiv(int x, int y) {int z = x/y; cout << "z: " << z << endl; } void floatDiv(int x, int y) {float a = (float)x;// old style float b = static_cast (y);// ANSI style float c = a/b; cout << "c: " << c << endl; } int main() {int x = 5, y = 3; intDiv(x,y); floatDiv(x,y); return 0; } Only give integer division result, i.e. 1 Give floating point division result, i.e. 1.66... Casting Ask the compiler to temporarily consider x as a floating point no., i.e. 5.0 in this case. Casting Ask the compiler to temporarily consider x as a floating point no., i.e. 5.0 in this case. x, y of main() are not x, y of intDiv(int, int)
8
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 For some commonly used expressions, there are some shorthands. c = c + 1; c += 1; c++; ++c; //increment c = c - 1; c -= 1; c--; --c; //decrement c = c + 2; c += 2; c = c - 3; c -= 3; c = c * 4; c *= 4; // there is no c** or c//, c = c / 5; c /= 5; // since it is meaningless Let c be an integer with value 5. Note that a = c++;// a = 5, c = 6 since a = c first and then c++ a = ++c;// a = 6, c = 6 since ++c first and then a = c Shorthand of Expressions
9
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 Mathematical operations are basically performed from left to right. It follows the normal mathematical precedence that multiplication / division first and addition / subtraction next. Hence To change the precedence, we can use parentheses. Parentheses can be nested as in normal arithmetic. Precedence and Parentheses x = 5 + 3 * 8; // x = 29 since we evaluate 3 * 8 first x = (5 + 3) * 8; // x = 64 since we evaluate 5 + 3 first x = 5 * ((3 + 2) + 3 * 2); // x = 5 * (5 + 6) = 55
10
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 Exercise 3.3a #include int main() { int myAge = 39; // initialize two integers int yourAge = 39; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; myAge++; // postfix increment ++yourAge; // prefix increment cout << "One year passes...\n"; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; cout << "Another year passes\n"; cout << "I am: " << myAge++ << " years old.\n"; cout << "You are: " << ++yourAge << " years old\n"; cout << "Let's print it again.\n"; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; return 0; }
11
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 Relational Operators Besides arithmetic operations, we can perform relational operations with C++. A number of relational operators are defined in C++. Each relational operator returns a bool result (true or false). In C++, zero is considered false, and all other values are considered true. “True” is usually represented by 1.
12
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 Relational Operators Operators Meaning ==Equals !=Not Equals >Greater than >=Greater than or equals <Less than <=Less than or equals Examples 100 == 50;// return false 100 != 50;// return true 100 > 50;// return true 100 >= 50;// return true 100 < 50;// return false 100 <= 50;// return false
13
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 Relational operators are often used with if statement. The if statement provides branching of the program execution depending on the conditions. int bigNumber = 10, smallNumber = 5; bool bigger = bigNumber > smallNumber; //bigger = true if (bigger) {doSomeThing(); } Applications of Relational Operators Or you can simply write: if (bigNumber > smallNumber) {doSomeThing(); } Or you can simply write: if (bigNumber > smallNumber) {doSomeThing(); }
14
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 Logical Operators Logical operators do logical operations for bool variables. Each logical operation returns a bool result (true or false). Operators Meaning && AND || OR ! NOT Examples expression1 && expression2 expression1 || expression2 !expression int bigNum = 10, smallNum = 5; if (bigNum 0) {doSomeThing(); } Will doSomeThing() be called??? NO
15
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 int bigNum = 10, smallNum = 5; if ((bigNum 0) || bigNum > smallNum) {doSomeThing(); } Will doSomeThing() be called??? int bigNum = 10, smallNum = 5; if (!(bigNum > smallNum)) {doSomeThing(); } Will doSomeThing() be called??? FT YES NO
16
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 # include using namespace std; int main() { int a, b, c; cout << "Enter 3 numbers: \n"; cin >> a; cin >> b; cin >> c; if (c = (a+b)); {cout << "c = a+b\n"; } return 0; } Exercise 3.3b The following program asks user to enter 3 numbers. If the sum of the first two is equal to the last one, a message is printed to the console. a.Build the project and note the result. Does it perform correctly? b.If not, fix the problem and re-build the program.
17
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE). Teach Yourself C++ in 21 Days, Second Edition (http://newdata.box.sk/bx/c/)
18
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 Reading Read Hours 4-5 in 24 hours
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.