Increment and Decrement Operators, First Loops

https://csci-1301.github.io/about#authors

March 27, 2024 (07:15:42 PM)

This lab serves multiple goals:

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.

  1. What does i++ do to i?
  2. What does i-- do to i?
  3. In your own words, can you explain the difference between ++i and i++?
Solution:
  1. i++ increments the value of i by 1; if the value of i was 5, it would become 6.
  2. i-- decrements the value of i by 1; if the value of i was 5, it would become 4.
  3. An explanation can be found in this post. In short, ++i “gives back” the value of i after it had been incremented by 1, while i++ “gives back” the value of i before it has been incremented. This makes a difference if i++ 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.

int x = 5;
int n = x++;
int x = 5;
int n = ++x;
int x = 5;
int n = x + x++;
int x = 5;
int n = ++x + ++x;
Solution:

You can download, extract, open in your IDE, and execute the following code to check that the answers are:

n is now 5.
x is now 6.
n is now 6.
x is now 6.
n is now 10.
x is now 6.
n is now 13.
x is now 7.

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.

  1. Write a while loop that displays the integers between 1 and 100 on the screen with a space between each number.
  2. Write a while loop that displays the integers between 100 and −100 on the screen, in decreasing order, with a space between each number.
  3. Write a while loop that displays the * (asterisk symbol) character 100 times on the screen.
  4. 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.

  1. Triangle:
 * 
 * *
 * * *
 * * * * 
 * * * * *
 * * * * * *
 * * * * * * * 
 * * * * * * * * 
 * * * * * * * * * 
 * * * * * * * * * *
  1. Triangle of numbers
1
222
33333
4444444
555555555
  1. Upside-down binary triangle
1010101
10101 
101  
1