If Control Construct: A Mechanism For Deciding Whether An Action Should Be Taken
If Control Construct: A Mechanism For Deciding Whether An Action Should Be Taken
19th century
Or
Not
Boolean Algebra
Truth tables
Lists all combinations of operand values and the result of
Example
P Q P and Q
P Q P or Q
P not P
False True
True False
Boolean Algebra
Can create complex logical expressions by combining simple
logical expressions
Example
not (P and Q)
false
Boolean operators
The and operator is &&
The or operator is ||
Warning
& and | are also operators so be careful what you type
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));
Relational Operators
Equality operators
==
!=
Examples
int i = 32;
int k = 45;
bool q = (i == k);
bool r = (i != k);
Relational Operators
Ordering operators
<
>
>=
<=
Examples
int i = 5;
int k = 12;
Two constructs
If statement
if
if-else
if-else-if
Switch statement
Left for reading
The Basic If Statement
Syntax
if (Expression)
Action
Expression
If the Expression is true then
execute Action
true false
Our number is
now definitely
nonnegative
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;
Semantics Are the numbers
out of order
Rearrange value1
and value2 to value2 < value1
put their values
in the proper
order true false
if (m < n)
++m;
++n;
cout << " m = " << m << " n = " n << endl;
The If-Else Statement
Syntax
if (Expression)
Action1
else
Action2 Expression
If Expression is true then execute
Action1 otherwise execute Action2 true false
if (v == 0) {
cout << "v is 0"; Action1 Action2
}
else {
cout << "v is not 0";
}
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;
Finding the Max Is Value2 larger than Value1
Yes, it is . So Value2 is
larger than Value1. In
this case, Max is set No, its not. So Value1
to Value2 is at least as large as
Value2. In this case,
Value1 < Value2 Max is set to Value1
true false
Switch statement
An advanced construct
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;
}
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;
default:
cout << ch << " is not a vowel" << endl;
}
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;
}