1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken...
-
Upload
sara-ramsey -
Category
Documents
-
view
246 -
download
0
Transcript of 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken...
![Page 1: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/1.jpg)
1
Chapter 4, Part 1 If Control Construct
A mechanism for deciding whether an action should be taken
JPC and JWD © 2002 McGraw-Hill, Inc . Modified by S. Sudarshan
![Page 2: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/2.jpg)
Boolean Algebra
Logical expressions have the one of two values - true or false
A rectangle has three sides The instructor has a pleasant smile
The branch of mathematics is called Boolean algebra Developed by the British mathematician George
Boole in the 19th century
Three key logical operators And Or Not
Output of operators defined by truth tables
![Page 3: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/3.jpg)
A Boolean TypeC++ contains a type named bool
Type bool has two symbolic constants true false
Boolean operators The and operator is && The or operator is || The not operator is !
![Page 4: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/4.jpg)
AND/OR Operators
Warning & and | are also operators (bit-wise and/or) so be
careful what you typeConditional evaluation:
E.g. if ((i >= 0) && (sqrt(i) < 5)) second operand (sqrt(i)) evaluated only if first
operand (i>=0) is true similarly for ||, second operation evaluated only if
first is false
![Page 5: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/5.jpg)
A Boolean Type
Example logical expressions
bool P = true;bool Q = false;bool R = true;bool S = (P && Q);bool T = ((!Q) || R);bool U = !(R && (!Q));
![Page 6: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/6.jpg)
Relational Operators
Equality operators ==, !=
Ordering operators <, <=, >, >=
Examples int i = 32; int k = 45; bool q = (i == k); bool r = (i != k); bool p = (i < 10);
![Page 7: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/7.jpg)
Operator Precedence Revisited
Precedence of operators (from highest to lowest)
Parentheses (brackets) Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment
Do not assume you will remember this ordering, use parentheses extensively to make meaning very clear
![Page 8: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/8.jpg)
Conditional Constructs
Provide Ability to control whether a statement list is
executed
Two constructs
If statement if if-else if-else-if
Switch statement Left for reading
![Page 9: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/9.jpg)
The Basic If Statement
Syntaxif (Expression)
Action
If the Expression is true then execute Action
Action is either a single statement or a group of statements within bracesflow charts
Expression
Action
true false
![Page 10: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/10.jpg)
Sorting Two Numbers
cout << "Enter two integers: ";
int Value1;
int Value2;
cin >> Value1 >> Value2;
if (Value1 > Value2) {
int RememberValue1 = Value1;
Value1 = Value2;
Value2 = RememberValue1;
}
cout << "The input in sorted order: "
<< Value1 << " " << Value2 << endl;
![Page 11: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/11.jpg)
Semantics (flowchart)
value2 < value1
int rememberValue1 = value1 value1 = value2 value2 = rememberValue1
true false
Are the numbersout of order
Rearrange value1and value2 to
put their valuesin the proper
order
The numbers wereinitially in order
The numbers wererearranged into the
proper order
The numbers are inorder
![Page 12: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/12.jpg)
What is the Output?
int m = 5;
int n = 10;
if (m >= n)
++m;
++n;
cout << " m = " << m << " n = " n << endl;
![Page 13: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/13.jpg)
Finding the Max
cout << "Enter two integers: ";
int Value1;
int Value2;
cin >> Value1 >> Value2;
int Max;
if (Value1 < Value2) {
Max = Value2;
}
else {
Max = Value1;
}
cout << "Maximum of inputs is: " << Max << endl;
![Page 14: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/14.jpg)
Finding the Max
Value1 < Value2
Max = Value2 Max = Value1
true false
Is Value2 larger than Value1
Yes, it is . So Value2 islarger than Value1. Inthis case, Max is set
to Value2No, its not. So Value1is at least as large asValue2. In this case,Max is set to Value1
Either case, Max is setcorrectly
![Page 15: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/15.jpg)
Selection
It is often the case that depending upon the value of an expression we want to perform a particular action
Two major ways of accomplishing this choice
if-else-if statement if-else statements “glued” together
Switch statement An advanced construct
![Page 16: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/16.jpg)
An If-Else-If Statement
if ( nbr < 0 ){cout << nbr << " is negative" << endl;
}
else if ( nbr > 0 ) {cout << nbr << " is positive" << endl;
}
else {cout << nbr << " is zero" << endl;
}
![Page 17: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/17.jpg)
A Switch Statement
switch (ch) {case 'a': case 'A':case 'e': case 'E':case 'i': case 'I':case 'o': case 'O':case 'u': case 'U':
cout << ch << " is a vowel" << endl;break;
case ‘\0’: cout << “end of line” << endl; break;default:
cout << ch << " is not a vowel" << endl;}
![Page 18: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/18.jpg)
cout << "Enter simple expression: ";
int Left;
int Right;
char Operator;
cin >> Left >> Operator >> Right;
cout << Left << " " << Operator << " " << Right
<< " = ";
switch (Operator) {
case '+' : cout << Left + Right << endl; break;
case '-' : cout << Left - Right << endl; break;
case '*' : cout << Left * Right << endl; break;
case '/' : cout << Left / Right << endl; break;
default: cout << "Illegal operation" << endl;
}
![Page 19: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/19.jpg)
19
End of Chapter 4, Part 1
![Page 20: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/20.jpg)
Boolean Algebra
Truth tables Lists all combinations of operand values and the
result of the operation for each combination
Example
P Q P and Q
False False FalseFalse True FalseTrue False FalseTrue True True
![Page 21: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/21.jpg)
Boolean Algebra
Or truth table
P Q P or Q
False False FalseFalse True TrueTrue False TrueTrue True True
![Page 22: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/22.jpg)
Boolean Algebra
Not truth table
P not P
False TrueTrue False
![Page 23: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/23.jpg)
Boolean Algebra
Can create complex logical expressions by combining simple logical expressions
Example not (P and Q)
A truth table can be used to determine when a logical expression is true
P Q P and Q not (P and Q)
False False False TrueFalse True False TrueTrue False False TrueTrue True True False
![Page 24: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/24.jpg)
Operator Precedence Revisited
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
![Page 25: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/25.jpg)
Operator Precedence Revisited
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
Yuck! Do not write expressions like this!
![Page 26: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/26.jpg)
Operator Precedence Revisited
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
Consider5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
However, for your information it is equivalent to
![Page 27: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/27.jpg)
Example
if (Value < 0) { Value = -Value;}
Value < 0
Value = -Value
true false
Is our number negative?
If Value is not lessthan zero then ournumber is fine as is
If Value is less thanzero then we need toupdate its value tothat of its additive
inverse
Our number isnow definitelynonnegative
![Page 28: 1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.](https://reader035.fdocuments.net/reader035/viewer/2022081516/5697bf741a28abf838c7f9bc/html5/thumbnails/28.jpg)
The If-Else StatementSyntax
if (Expression) Action1
else Action2
If Expression is true then executeAction1 otherwise execute Action2
if (v == 0) {
cout << "v is 0";
} else { cout << "v is not 0";
}
Expression
Action1 Action2
true false