Slide 1 Summary Two basic concepts variables and

Slide 1 Summary Two basic concepts: variables and assignments Basic types: int, double, char (string), … Some C++ practical issues: division rule, operator precedence

Slide 2 Expression and statement An expression has a value which is the result of some operation(s) on the associated operands. 4, x-y, 2 -a-(b*c) A statement is a sentence that acts as a command it does not have a value it always ends in a ‘; ’ cin >> x; x = 5; int x;

Boolean type and expressions

Slide 4 Boolean type: bool * C++ contains a type (new!) named bool which can have one of two values * true (corresponding to non-zero value) * false * * (corresponding to zero value) Boolean operators * Logical and: && * Logical or: || * Logical not: ! Examples bool bool P Q R S T U = = = true; false; true; P && Q; (!Q) || R; !(R && !Q);

Slide 5 Boolean expressions l l Arithmetic expression: use arithmetic operators +, , *, /, to produce a number as the final result A Boolean expression has one of the two values: true or false l use relational operators <, >, ==, … l and boolean operators AND (&&), OR (||), NOT (!)

Slide 6 Using Relational Operators Relational operators are used to compare two values Math C++ Plain English = == equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] < > < <= > >= != less than or equal to greater than or equal to not equal to

Slide 7 Examples: number. Of. Students < 200 10 > 20 20 * j == 10 + i

Using Boolean (logical) operators Slide 8 * Boolean operators can be used to form more complex conditional expressions l l l Logical AND operator Logical OR operator Logical NOT operator Examples: * (x>5) && (x<10) (x>10) || (x<5) !(x>5) Warning! n & and | are also operators && || !

Slide 9 Operator Precedence Which comes first? Answer: * < / + <= == % >= != = >

Summary of Operator Precedence Slide 10 • Precedence of operators (from highest to lowest) n n n arithmetic relational logical n n n Parentheses Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment ( … ) ! * / % + < <= >= > == != && || =

Slide 11 Example: 5 != 6 || 7 <= 3 (5 !=6) || (7 <= 3) 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24

Flow control

Slide 13 Introduction Three program structures or constructs: • Sequence Statements in the given order • Conditional statement (branching) Chooses between two (or more) sequences depending on some condition if <condition exists> { <do P> } else { <do Q> } • Iteration statement (looping) repetitively execute a given sequence while <condition exists> { <do P> }

Slide 14 Structured programming Any program can be written as a sequence of three basic program structures!!! 1. sequences, 2. conditionals, 3. and iterations

Slide 15 The fundamental conditional if-else Statement Choose between two alternative actions depending on a test (on the values of variables). • • • Syntax if (Expression) Action 1 else Action 2 If Expression is true then execute Action 1 otherwise execute Action 2 Example if(v == 0) cout << "v is 0"; else cout << "v is not 0"; Expression true false Action 1 Action 2

Slide 16 It’s common in everyday life … ? if <it's sunny>{ <go to beach with sun block> } else{ <go to beach with umbrella> }

Slide 17 Example: Absolute Value st (1 ) // program to read number & print its absolute value #include <iostream> using namespace std; int main(){ int value; int absvalue; cout << "Enter integer: "; cin >> value; if (value < 0) absvalue = -value; else absvalue = value; cout << "The absolute value is " << absvalue << endl; return 0; }

Slide 18 When the action is more than one statement … Put multiple action statements within braces if <it's raining> { <take umbrella> <wear raincoat> } else { <take sunbathing stuff> }

Slide 19 Example: Absolute Value nd (2 ) // program to read number & print its absolute value #include <iostream> using namespace std; int main(){ int value; int absvalue; // absolute value cout << "Enter integer: "; cin >> value; if (value < 0) { absvalue = -value; cout << "The input value is negative and its absolute value is " << absvalue << endl; } else { absvalue = value; cout << "The input value is positive and its absolute value is " << absvalue << endl; } return 0; }

Slide 20 Summary of the conditional if-else Statement A if (cond 1) B else C D = A B D or A C D

Slide 21 Nested if-else Statements n Nested means that one complete statement is inside another if cond 1 { A; if cond 2 { B; } else { C } else { D }

Slide 22 Example double score; cin >> score; if(score >= 90. 0) cout << "Grade = A" << endl; else if(score >= 80. 0) cout << "Grade = B" << endl; else if(score >= 70. 0) cout << "Grade = C" << endl; else if(score >= 60. 0) cout << "Grade = D" << endl; else cout << "Grade = F" << endl;
- Slides: 22