if.C
:
An
if
statement.
quarters.C
:
another example.
Singular vs. plural.
consecutive1.C
and
consecutive2.C
.
if
statements in
consecutive1.C
are a pair of consecutive, mutually exclusive
if
statements.
if
statement will be true, and
one
if
statement will be false.
It could be hard to recognize when a pair of logical expressions are mutually exclusive:
if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0) { cout << year << " is a leap year.\n"; } if (year % 400 != 0 && (year % 100 == 0 || year % 4 != 0)) { cout << year << " is not a leap year.\n"; }
threeway1.C
and
threeway2.C
.
12hour.C
.
Steer the computer in one of three possible directions
to output the current hour on a
12-hour clock.
hour <= 12
better than
hour < 13
?
else/if
you just saw in
threeway2.C
and
12hour.C
,
add a three-way
else/if
to
morning.C
to output one of three possible messages:
Good morning
if the
hour
is less than 12 (noon);
otherwise …
Good afternoon
if the
hour
is less than 17 (5:00 pm);
otherwise …
Good evening
.
leap.C
.
Steer the computer in one of four possible directions.
if
statement; see above.
else/if
you just saw in
leap.C
,
make
morning.C
output one of
four
possible messages:
Good morning
if the
hour
is less than 12 (noon);
otherwise …
Good afternoon
if the
hour
is less than 17 (5:00 pm);
otherwise …
Good evening
if the
hour
is less than 21 (9:00 pm);
otherwise …
Good night
ordinal.C
.
Steer the computer in one of five possible directions.
if (you can remain inside the train) { remain inside the train; } else if (you can go to next car through end doors) { go to next car through end doors; } else if (you can open side door and go out) { open side door and go out; } else { go out emergency windows; //as a last resort }
performancebug1.C
and
performancebug2.C
.
performancebug1.C
tells the computer to do unnecesary work.
performancebug2.C
always produces the same output as
performancebug1.C
,
but never does unnecesary work.
if (fahrenheit >= 100.0) { cout << "It's hot as hell!\n"; } if (fahrenheit >= 212.0) { cout << "In fact, it's hot enough to boil water!\n"; }
performancebug3.C
and
performancebug4.C
.
performancebug3.C
tells the computer to do unnecesary work.
performancebug4.C
always produces the same output as
performancebug3.C
,
but never does unnecesary work.
We can easily flip an
if/else
upside down, taking advantage of three pairs of opposite operators:
==
vs. !=
(“equals” vs. “not equals”)
<
vs. >=
(“less than” vs. “greater than or equal to”)
>
vs. <=
(“greater than” vs. “less than or equal to”)
if/else
is in
yellow,
the smaller section is in
orange.
if (harris == trump) { cout << "The Electoral College is hung!\n"; cout << "What do we do now?\n"; } else { cout << "The Electoral College has reached a decision.\n"; }
if (harris != trump) { cout << "The Electoral College has reached a decision.\n"; } else { cout << "The Electoral College is hung!\n"; cout << "What do we do now?\n"; }
It is especially important to put the big section on the bottom
when the
big section
contains another
if
statement:
if (n >= 0) { if (n == 0) { cout << "The number is zero.\n"; } else { cout << "The number is positive.\n"; } } else { cout << "The number is negative.\n"; }
if (n < 0) { cout << "The number is negative.\n"; } else { if (n == 0) { cout << "The number is zero.\n"; } else { cout << "The number is positive.\n"; } }because now we can easily change it to a chain of
else/if
s:
if (n < 0) { cout << "The number is negative.\n"; } else if (n == 0) { cout << "The number is zero.\n"; } else { cout << "The number is positive.\n"; }
Side by side, without color:
//Original if (n >= 0) { if (n == 0) { cout << "The number is zero.\n"; } else { cout << "The number is positive.\n"; } } else { cout << "The number is negative.\n"; } |
//More straightforward, don't you think? if (n < 0) { cout << "The number is negative.\n"; } else if (n == 0) { cout << "The number is zero.\n"; } else { cout << "The number is positive.\n"; } |
Exercise.
In
morning1.C
,
do what we just did.
Does this make the program easier to read and understand and check for bugs?
Anoher exercise.
Unsnarl
morning2.C
.
The
else/if
s
in
ordinal.C
did not all use the same variable.
Some used
lastTwoDigits
,
and others used
lastDigit
.
But the
else/if
s
in
switch1.C
all use the same variable (weekday
)
and all use the comparison ==
.
switch1.C
.
Comparing the same
int
variable (weekday
)
over and over again with
==
to test for equality.
switch2.C
.
One
switch
statement instead of a chain of
if/else
s.
switch3.C
.
Remove the
break
s.
Exercise.
In
switch4.C
,
change the
else/if
s to one big
switch
statement.
Imitate what we just did in
switch2.C
.
product4.C
:
an
if
statement inside the
for
loop we saw
here.
checkerboard.C
:
draw a checkerboard.
Just like
rectangle.C
,
but with an
if/else
in the inner loop.
toolow.C
:
a guessing game with hints.
infinite.C
,
but we
break
out of the
for
loop.
for1.C
and
toolow2.C
.
pi.C
:
use a Monte Carlo method to
estimate the value of π
from a lot of random numbers.
Is there a way we could get rid of the
sqrt
so we could loop faster?
//While we iterate a billion times, show our progress. if (i % 10000000 == 0) { //Once every 10,000,000 loops, cout << i << "\n"; }
ukraine.C
and
ukraine.png
10
to
nrows/2
so you only need to change one statement when you change the nuber of rows.
france.C
and
france.png
10
to
ncols/3
,
and change
20
to
ncols*2/3
,
so you only need to change one statement when you change the nuber of columns.
japan.C
and
japan.png
.
#include <cmath>
for the
sqrt
function.
usa.C
and
usa.png
.
if
statement in a
for
loop:
colombia.png
.
colombia1.C
:
the comments are wrong
colombia2.C
:
corrected the comments,
put the {
curly braces}
in the conventional positions.
colombia3.C
:
now we can resize the flag simply by changing the initial value of
nrows
.
Nothing else needs to be changed.
colombia4.C
and
colombia4.png
:
The top stripe is now
half
the height of the flag, as in the
real flag of Colombia.
I also used the official colors.
if
statement:
past, present, and future.
monkey1.C
and
monkey4.C
.
currentYear
but then didn’t use it:
MonkeyHW.C
.