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.
§Concept of true and false §true is any value other than zero §false is zero
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;
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
Relational Operators > >= < <= = != Logical Operators && | !--
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;
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
Boolean expressions §Truth tables
Simple Flow of Control §Flow of control l The order in which statements are executed §Branch l Lets program choose between two alternatives
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.
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; }
If: selective execution int p=5; if (p) cout<<“done”;
Boolean expressions Difference between == and =
Boolean expressions Programming help: Instead of x == 3 use 3 == x. Then, the compiler will catch the error if you write only one =
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.
Example gross_pay = rate * * rate * (hours - 40);
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 * * rate * (hours - 40); l The program must choose which of these expressions to use
Designing the Branch §Decide if (hours >40) is true l If it is true, then use gross_pay = rate * * rate * (hours - 40); l If it is not true, then use gross_pay = rate * hours;
Implementing the Branch §if-else statement is used in C++ to perform a branch if (hours > 40) gross_pay = rate * * rate * (hours - 40); else gross_pay = rate * hours;
Boolean expressions Logical AND is represented by && (score >= 0) && (score <= 10) Logical OR is represented by || (score 10)
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)
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.
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!
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.
int x=2, y=9; cout<< (x==y) <<endl; cout<< (x=y) <<endl;
int x=2, y=9; cout<< (x==y) <<endl; // prints 0 cout<< (x=y) <<endl; // prints 9
§Another example
Boolean expressions Pitfall: Be careful with precedences. When in doubt, use parentheses. Relational and logical operators have lower precedence than arithmetic operators 15< is evaluated as 15<(100-90) cout << (a && b); //parenthesis are necessary
Boolean expressions Pitfall: Be careful with precedences. When in doubt, use parentheses. int a=3, c=4; cout<<(a && c)<<endl; //parenthesis are necessary