Chapter 4
Chapter 4
Operator Precedence
> >= < <= Highest
== != Lowest
if (intRate > 0)
{ interest = loanAmt * intRate;
cout << interest;
}
else
cout << "You owe no interest.\n";
Copyright © 2017, 2014 Pearson Education, Inc. 4-22
if vs. if/else
If there are two conditions and both of them can be
true or both can be false, then use two if
statements:
if (num > 0)
cout << num << " is positive\n";
if (num %2 == 0)
cout << num << " is even\n";
If the two conditions cannot both be true, then a
single if/else statement can work:
if (num %2 == 0)
cout << num << " is even\n";
else
cout << num << " is odd\n";
• It is better to use
– greater-than or less-than tests, or
– test to see if value is very close to a given
value
if (condition 1)
{ statement set 1;
}
else if (condition 2)
{ statement set 2;
}
. . .
else if (condition n)
{ statement set n;
}
Highest !
&&
Lowest ||
Example:
(2 < 3) || (5 > 6) && (7 > 8)
switch (gender)
{
case 'f': cout << "female";
break;
case 'm': cout << "male";
break;
default : cout << "invalid gender";
}