thursday, december 14, 2006 “would you tell me, please, which way i ought to go from here?”...
Post on 21-Dec-2015
213 views
TRANSCRIPT
Thursday, December 14, 2006
“Would you tell me, please, whichway 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.
bool b=true;cout<<b<<endl;b=false;cout<<b<<endl;//zero is false and non-zero is trueb=35;cout<<b<<endl;b=-45;cout<<b<<endl;b=0;cout<<b<<endl;
bool b=true;cout<<b<<endl; //prints 1b=false;cout<<b<<endl; //prints 0//zero is false and non-zero is trueb=35;cout<<b<<endl; //prints 1b=-45;cout<<b<<endl; //prints 1b=0;cout<<b<<endl; //prints 0
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
Simple Flow of Control
Flow of control The order in which statements are executed
Branch Lets program choose between two alternatives
If: selective executionThe "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(<condition>) { <statements> } If <condition> is TRUE (non-zero), then <statements>
are executed.
If <condition> is FALSE (zero), then <statements> are not executed.
If: selective executionint main () {
int age;
cout << "Please enter your age: ";cin >> age;if(age <= 0) { cout << "Age must be > 0\n"; return 0;}cout << "Your age is " << age << endl;return 0;
}
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 executionIf: choosing between two alternatives
Pseudocode: if(<condition>) { <true_statements> } else { <false_statements> } If <condition> is TRUE (non-zero), then
<true_statements> are executed.
If <condition> is FALSE (zero), then <false_statements> are executed.
Branch Example
To calculate hourly wages there are two choices Regular time ( up to 40 hours)
• gross_pay = rate * hours;
Overtime ( over 40 hours)• gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
The program must choose which of these expressions to use
Designing the Branch
Decide if (hours >40) is true If it is true, then use
gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
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 * 40 + 1.5 * 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 < 0) || (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 < 0) || (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 < 0) || (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 < 0) || (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 == yis true if x has the same value as y.
x = yon the other hand, assigns the value of y to the
variable x.
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