This lab serves multiple goals:
- To introduce increment and decrement operators,
- To understand the difference between prefix and postfix operators,
- To help you implement your first
while
loops, and - (Optional) to write your first complex loops.
Increment and Decrement Operators
Preamble
Start by answering the following, assuming an int
variable i
has
been initialized and that its current value is 5.
- What does
i++
do toi
? - What does
i--
do toi
? - In your own words, can you explain the difference between
++i
andi++
?
Solution:
i++
increments the value ofi
by 1; if the value ofi
was 5, it would become 6.i--
decrements the value ofi
by 1; if the value ofi
was 5, it would become 4.- An explanation can be found in this
post. In short,
++i
“gives back” the value ofi
after it had been incremented by 1, whilei++
“gives back” the value ofi
before it has been incremented. This makes a difference ifi++
or++i
is part of a larger statement. The next exercises will illustrate this principle.
Observe and Confirm
Download, extract, open in your IDE, and read the following code. Then, compile and execute it. Study the output carefully to make sure you understand the mechanism of the increment and decrement operators.
Compare your answers from the previous section to what you observe in the output. Do your answers correspond to what you observe in the output?
Exercise
For each of the following, determine the final value of n
and x
.
Solution:
You can download, extract, open in your IDE, and execute the following code to check that the answers are:
If you read the source code, you can see that the order of evaluation is
quite difficult to follow. Actually, the way the sum operator +
and
the increment operator ++
interact is sometimes undefined, meaning
that the result may vary from one programming language to another (or
worst, from one version to another): you can read more about this
here of
there (Disclaimer: those are
very technical discussions, much more advanced than the official
documentation
could lead to think).
In short: software developers generally avoid this type of “nested” operations altogether, and use increment or decrement operators more scarsely!
First While Loops
The following asks you to write a series of while
loops performing
simple tasks. The solution to the first question is in this
archive, but it is recommended to try it
on your own first.
- Write a
while
loop that displays the integers between and on the screen with a space between each number. - Write a
while
loop that displays the integers between and on the screen, in decreasing order, with a space between each number. - Write a
while
loop that displays the*
(asterisk symbol) character 100 times on the screen. - Modify your previous loop, so that a new line character is displayed on the screen every time 10 asterisk symbols have been displayed on the screen.
To clarify, your program should display the following on the screen (this example has a space after each asterisk symbol for display purposes):
Pushing Further (Optional)
Here are additional pattern problems, similar to the last one from the
previous section. You are asked to generate these patterns using a
while
loop for each.
- Triangle:
- Triangle of numbers
- Upside-down binary triangle