Download presentation
Presentation is loading. Please wait.
1
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said the Cat. - LEWIS CARROL, Alice in Wonderland.
2
§Concept of true and false §true is any value other than zero §false is zero
3
bool b=true; cout<<b<<endl; b=false; cout<<b<<endl; //zero is false and non-zero is true b=35; cout<<b<<endl; b=-45; cout<<b<<endl; b=0; cout<<b<<endl;
4
bool b=true; cout<<b<<endl; //prints 1 b=false; cout<<b<<endl; //prints 0 //zero is false and non-zero is true b=35; cout<<b<<endl; //prints 1 b=-45; cout<<b<<endl; //prints 1 b=0; cout<<b<<endl; //prints 0
5
Relational Operators > >= < <= = != Logical Operators && | !--
6
Relational expressions int p=5, q=2; bool bl; bl = true; cout<<bl<<endl; bl = (p<=q); cout<<bl<<endl; bl=!bl; cout<<bl<<endl;
7
Relational expressions int p=5, q=2; bool bl; bl = true; cout<<bl<<endl; //prints 1 bl = (p<=q); cout<<bl<<endl; //prints 0 bl=!bl; cout<<bl<<endl; //prints 1
8
Boolean expressions §Truth tables
9
Simple Flow of Control §Flow of control l The order in which statements are executed §Branch l Lets program choose between two alternatives
10
If: selective execution §The "if" statement allows us to execute a piece of code or not, based on the value of the expression we pass to it. Pseudocode: if( ) { } If is TRUE (non-zero), then are executed. If is FALSE (zero), then are not executed.
11
If: selective execution int main () { int age; cout << "Please enter your age: "; cin >> age; if(age <= 0) { cout 0\n"; return 0; } cout << "Your age is " << age << endl; return 0; }
12
If: selective execution int p=5; if (p) cout<<“done”;
13
Boolean expressions Difference between == and =
14
Boolean expressions Programming help: Instead of x == 3 use 3 == x. Then, the compiler will catch the error if you write only one =
15
If: selective execution §If: choosing between two alternatives Pseudocode: if( ) { } else { } If is TRUE (non-zero), then are executed. If is FALSE (zero), then are executed.
16
Example gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
17
Branch Example §To calculate hourly wages there are two choices l Regular time ( up to 40 hours) gross_pay = rate * hours; l Overtime ( over 40 hours) gross_pay = rate * 40 + 1.5 * rate * (hours - 40); l The program must choose which of these expressions to use
18
Designing the Branch §Decide if (hours >40) is true l If it is true, then use gross_pay = rate * 40 + 1.5 * rate * (hours - 40); l If it is not true, then use gross_pay = rate * hours;
19
Implementing the Branch §if-else statement is used in C++ to perform a branch if (hours > 40) gross_pay = rate * 40 + 1.5 * rate * (hours - 40); else gross_pay = rate * hours;
20
Boolean expressions Logical AND is represented by && (score >= 0) && (score <= 10) Logical OR is represented by || (score 10)
21
Boolean expressions Logical AND is represented by && (score >= 0) && (score <= 10) is true if score is between 0 and 10 (including 0 and 10). Logical OR is represented by || (score 10)
22
Boolean expressions Logical AND is represented by && (score >= 0) && (score <= 10) is true if score is between 0 and 10 (including 0 and 10). Logical OR is represented by || (score 10) is true exactly in the cases where the previous condition would be false.
23
Boolean expressions Logical NOT is represented by an exclamation mark: ! (score 10) is equivalent to ! ( (score >= 0) && (score <= 10) ) Pitfall: & and | are also operators in C++. However, they do something different!
24
Boolean expressions Pitfall: comparison operator for equality is = = x == y is true if x has the same value as y. x = y on the other hand, assigns the value of y to the variable x.
25
int x=2, y=9; cout<< (x==y) <<endl; cout<< (x=y) <<endl;
26
int x=2, y=9; cout<< (x==y) <<endl; // prints 0 cout<< (x=y) <<endl; // prints 9
27
§Another example
28
Boolean expressions Pitfall: Be careful with precedences. When in doubt, use parentheses. Relational and logical operators have lower precedence than arithmetic operators 15<100-90 is evaluated as 15<(100-90) cout << (a && b); //parenthesis are necessary
29
Boolean expressions Pitfall: Be careful with precedences. When in doubt, use parentheses. int a=3, c=4; cout<<(a && c)<<endl; //parenthesis are necessary
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.