Lec 10
Lec 10
Lecture 9
Recap: What is the Output?
int x = 100;
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
equivalent
if (x == 100) if (x == 100)
cout << "x is 100"; cout << "x is 100";
cout << "x is not 100"; cout << "x is not 100";
-2-
Recap: What is the Output?
-4-
Problem: Even/Odd Number
int number;
cout << "Enter a number\n";
cin >> number;
if (number % 2 == 0)
cout << number << "is Even\n";
else
cout << number << "is Odd\n";
-5-
Logical Operators
AND (&&)
OR (||)
NOT (!)
-6-
Exercise: Logical Operators
Expression Value
! (age > 18) false
! (weight == 150) true
(age > 18) || (weight >= 150) true
(age > 18) && (weight >= 140) true
(weight == 150) || (age < 45) true
(age > 34) || (weight < 140) false
(weight >=120) && (age >=30) false
-7-
Logical Operators
BEWARE:
To check a range,
Math: 2 < x < 10 OK
Computer: if (2 < x < 10) NO!!!
Say x= 11, the computer will check the first
condition (2 < x) which evaluates to true (1). Then
the computer checks (1 < 10) which evaluates to
true!
if ((2 < x) && (x < 10)) YES
-8-
Logical operators - De Morgan’s law
-9-
Note
-10-
Boolean Conditional Values
(a) (b)
This is better
(a) (b)
This is better
-11-
Problem: BMI
BMI Interpretation
BMI< 18.5 underweight
18.5 < BMI < 25 normal
25 < BMI < 30 overweight
BMI >= 30 Seriously overweight
-12-
Solution
float weight, height;
If x = 5, then
cout << ++x;
x is changed to 6, then printed out
If x = 5, then
cout << x++;
Prints out 5 (cout is executed before the
increment).
x then becomes 6
-17-
Increase/Decrease Operators
newNum = 100
i = 11
int i = 10; Same effect as
int newNum = 10 * ++i; i = i + 1;
int newNum = 10 * i;
newNum = 110
i = 11
-18-
Conditional Operator
y = (x > 0) ? 1 : -1 ;
is equivalent to
if (x > 0)
y = 1;
else
y = -1;
-19-
Conditional Operator
-20-
Problem
-21-
Problem: Subtraction
-22-
Problem: Subtraction
-23-
Precedence Rules
-24-
Exercise
Note that:
Function rand() generates random number.
int x = rand() % n;
Generates a random number from 0 to n-1.
-26-
Solution
cout << "What is " << number1 << "-" << number2 << "? ";
int answer;
cin >> answer;
if (number1 - number2 == answer)
cout << "You are correct!\n";
else
cout << "Wrong answer" << endl <<
number1 << "-" << number2 << "=" <<
number1 - number2 << endl;
-27-
Note
-28-
Nested if
Write a program to find the minimum of three
numbers.
int n1,n2,n3;
cout << "Enter three integers: ";
cin >> n1 >> n2 >> n3;
if (n1 < n2)
{
if (n1 < n3) cout << "Their minimum is " << n1 << endl;
else cout << "Their minimum is " << n3 << endl;
}
else // n2 <= n1
{
if (n2 < n3) cout << "Their minimum is " << n2 << endl;
else cout << "Their minimum is " << n3 << endl;
} -29-
Another Solution
int n1,n2,n3;
cout << "Enter three integers: ";
cin >> n1 >> n2 >> n3;
if (n1 <= n2 && n1 <= n3)
cout << "Their minimum is " << n1 << endl;
else if (n2 <= n1 && n2 <= n3)
cout << "Their minimum is " << n2 << endl;
else
cout << "Their minimum is " << n3 << endl;
-30-
Note
Logic Error Empty Body
int i = 1; int i = 1;
int j = 2; int j = 2;
int k = 3; Equivalent
int k = 3; Dangling
if (i > j) if (i > j) else
if (i > k) This is better if (i > k)
cout << "A"; with correct cout << "A";
else indentation else
cout << "B"; cout << "B";
(a) (b)
-34-
Grade
char grade;
Interpretation
cout << "Enter student grade\n";
cin >> grade;
switch (grade) break causes switch to
{ end and the program continues
case 'A': with the first statement after the
cout << "Excellent\n"; break; switch structure.
case 'B':
cout << "Very Good\n"; break;
case 'C':
cout << "Good\n"; break;
case 'D':
cout << "You can do better\n"; break;
case 'E':
cout << "Disappointing\n"; break;
default:
cout << "Invalid Grade\n";
}
-35-
char grade;
Grade
cout << "Enter student grade\n";
cin >> grade; Interpretation
switch (grade)
{
case 'A': // Fall to through to next case
case 'a':
cout << "Excellent\n"; break;
case 'B':
case 'b':
cout << "Very Good\n"; break;
case 'C':
case 'c':
cout << "Good\n"; break;
case 'D':
case 'd':
cout << "You can do better\n"; break;
case 'E':
case 'e':
cout << "Disappointing\n"; break;
default:
cout << "Invalid Grade\n";
}
-36-
Problem
-37-
Exercise: Complete
-38-
Exercise: True/False
-39-
Exercises