Back

Activity 1.6: Arithmetic, Reassignment, and Interpolation

divider

Activity 1.6

Arithmetic, Reassignment, and Interpolation

Key Concepts

A Value That Changes

Doing Math

Saying It Cleanly

Your Card Was Frozen

string characterName = "Sephiroth";
int level = 24;

Every value on it was true for the whole program.

A score is not like that. It has to go up.

Change What Is in the Box

One box, printed twice, holding a different number each time.

Change What Is in the Box

int score = 0;
Console.WriteLine(score);
score = 3;
Console.WriteLine(score);
Same variable, twice
0
3

The second assignment has no int. The box already exists.

Putting a new value in one is called reassignment.

This Line Is Not an Equation

score = score + 2;

In math class that would be false. Nothing equals itself plus two.

C# is not claiming anything. It is giving an instruction.

Right Side First

Work out the right side using the value in the box now, then put the answer back in the same box.

Right Side First

int score = 3;
score = score + 2;
Console.WriteLine(score);
Starting from 3
5

The right side read 3 and produced 5. Only then did the box change.

Read every assignment right to left.

Computing Is Not Storing

score + 2;
Terminal window
1-6-arithmetic.cs(2,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
The build failed. Fix the build errors and run again.

The answer was worked out and then thrown away.

C# refuses code that does nothing with its result.

The Four Operators

Console.WriteLine(8 + 3);
Console.WriteLine(8 - 3);
Console.WriteLine(8 * 3);
Console.WriteLine(8 / 3);
Terminal window
11
5
24
2

Look at That Last One

Terminal window
11
5
24
2

Not 2.67. Not 3.

Where did the rest of it go?

Two Whole Numbers In, a Whole Number Out

Terminal window
11
5
24
2

8 and 3 are both int, so C# keeps only the whole part.

The fraction is discarded, not rounded.

Asking for a Decimal Does Not Help

double average = 47 / 6;
Console.WriteLine(average);
Terminal window
7

The division finished before the box ever saw it.

By then there was nothing left to save.

Change One Character

Console.WriteLine(47.0 / 6);
Terminal window
7.833333333333333

47.0 is a double, so the division is not whole-number division at all.

One of the two has to be a decimal. Either one.

Remainder

Console.WriteLine(17 / 5);
Console.WriteLine(17 % 5);
Terminal window
3
2

Five goes into seventeen three times, with two left over.

% hands you the two.

Why Remainder Matters

Odd or even, and wrapping around a clock. Two of the three things you will use it for.

Why Remainder Matters

Console.WriteLine(9 % 2);
Console.WriteLine(12 % 2);
Console.WriteLine(14 % 12);
Odd, even, and 2 o'clock
1
0
2

9 % 2 is 1 and 12 % 2 is 0 — that is an odd-or-even test.

14 % 12 is 2, which is how 14:00 becomes 2 o'clock.

It appears on the AP exam more than you would expect.

Order of Operations

Console.WriteLine(2 + 3 * 4);
Console.WriteLine((2 + 3) * 4);
Terminal window
14
20

The same rules as math class. C# did not invent new ones.

Where Does % Go?

Same priority as * and /, which means the parentheses change the answer.

Where Does % Go?

Console.WriteLine(20 % 7 * 2);
Console.WriteLine(20 % (7 * 2));
Two different answers
12
6

Left to right without them: 20 % 7 is 6, times 2 is 12. With them: 20 % 14 is 6.

When you are unsure, add parentheses. They cost nothing.

Two Lines to Print One

This is how you have joined a label to a value since Activity 1.4.

Two Lines to Print One

Console.Write("Score: ");
Console.WriteLine(score);
Two lines of code
Score: 7

One line of output. It works, and it doubles the length of every program.

Put the Value in the Sentence

One line this time, and it reads like a sentence.

Put the Value in the Sentence

Console.WriteLine($"Score is {score}.");
One line of code
Score is 7.

The $ before the quote turns on the holes. Braces mark a hole, and C# drops the value in.

This is interpolation.

A Hole Can Hold a Calculation

Anything that produces a value fits in a hole.

A Hole Can Hold a Calculation

Console.WriteLine($"Two more would put the total at {score + 2}.");
Console.WriteLine($"score is still {score}.");
Watch the second line
Two more would put the total at 9.
score is still 7.

The hole printed 9, and the next line still prints 7.

The variable did not change. It was only read.

Today's Objectives

  • Reassigning a variable, including from its own current value
  • Using the four operators and remainder
  • Predicting whole-number division
  • Displaying values with interpolation

Key Terms

Reassignment
Putting a new value into a variable that already exists.
Expression
Any piece of code that produces a value.
Whole-number division
Dividing two int values, which discards the fraction.
Remainder
What is left over after dividing, written %.
Interpolation
Dropping values into a string using $ and braces.

'F' → Fullscreen

divider

Build

Create a new C# program named 1-6-arithmetic, starting empty.

Use the same subject you chose for Activity 1.5 — the athlete, character, or officer whose card you built. Today they get a score that moves.

The examples below continue with the RPG character from Activity 1.5. If you built a different kind of card, swap in your own subject and events — the arithmetic works the same either way.

This activity runs over two sessions. Tasks 1 to 4 are the first; Tasks 5 and 6 are the second.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

You never start a new file and you never delete your work. The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.


Task 1: A Value That Changes

  • Declare a string for the name and an int score starting at 0.
  • Print the name, then the score.
  • On a new line, put 3 into the score. Do not write int again.
  • Print the score a second time.

Two lines of output, two different numbers, one variable. That is the whole idea.

Arithmetic, Reassignment, and Interpolation
string characterName = "Sephiroth";
int score = 0;
Console.Write(characterName);
Console.WriteLine(" enters the dungeon.");
Console.Write("Score: ");
Console.WriteLine(score);
score = 3;
Console.Write("Score: ");
Console.WriteLine(score);

Task 2: Update It From Itself

  • Add two more kills, each worth 2, and print the score after each.
  • Do not type 5 or 7 anywhere. The program works them out.
Arithmetic, Reassignment, and Interpolation
string characterName = "Sephiroth";
int score = 0;
Console.Write(characterName);
Console.WriteLine(" enters the dungeon.");
Console.Write("Score: ");
Console.WriteLine(score);
score = 3;
Console.Write("Score: ");
Console.WriteLine(score);
score = score + 2;
Console.Write("Score: ");
Console.WriteLine(score);
score = score + 2;
Console.Write("Score: ");
Console.WriteLine(score);

Task 3: Splitting the Raid

  • Forty-seven adventurers sign up. Parties hold six.
  • Work out the number of full parties and the number of adventurers left over.
  • Both values must be calculated, not typed in.
  • Check it by hand first: 6 × 7 = 42, and 47 − 42 = 5.
  • This goes below what you already have, after a blank Console.WriteLine() to separate the two halves of the output.
Arithmetic, Reassignment, and Interpolation
string characterName = "Sephiroth";
int score = 0;
Console.Write(characterName);
Console.WriteLine(" enters the dungeon.");
Console.Write("Score: ");
Console.WriteLine(score);
score = 3;
Console.Write("Score: ");
Console.WriteLine(score);
score = score + 2;
Console.Write("Score: ");
Console.WriteLine(score);
score = score + 2;
Console.Write("Score: ");
Console.WriteLine(score);
Console.WriteLine();
int adventurers = 47;
int perParty = 6;
int fullParties = adventurers / perParty;
int leftOver = adventurers % perParty;
Console.Write("Adventurers:");
Console.WriteLine(adventurers);
Console.Write("Parties: ");
Console.WriteLine(fullParties);
Console.Write("Left over: ");
Console.WriteLine(leftOver);

Whole-number division did the hard part for you. 47 / 6 is 7 on its own — there is nothing to round down, because the fraction was never kept.


Task 4: Break It on Purpose

Five experiments. Write down exactly what appears each time, copied from the screen rather than from memory. Undo each one before the next.

  1. Print 8 / 3.
  2. Add double average = 47 / 6; and print average.
  3. Print 47.0 / 6.
  4. Print 10 % 3 * 2, then print 10 % (3 * 2).
  5. Put score + 2; on a line by itself, with nothing to the left of it.

One of these refuses to run. The other four produce a number, and at least two of them will not be the number you expected. Which is which is what the sheet asks for.

There is no code block for this task. Each experiment is temporary and gets undone before the next one, so when Task 4 is over your program is exactly what it was at the end of Task 3.


Task 5: Say It in One Line

  • Replace every Console.Write and Console.WriteLine pair with a single interpolated line.
  • Make each line read like a sentence rather than a label and a number.
  • Count your program's lines before and after. Write both numbers down — it should go from 33 lines to 24.
  • This task replaces lines rather than adding them, so your program gets shorter. Each + line below stands in for a Console.Write and Console.WriteLine pair you already had.
Arithmetic, Reassignment, and Interpolation
string characterName = "Sephiroth";
int score = 0;
Console.WriteLine($"{characterName} enters the dungeon at {score}.");
score = 3;
Console.WriteLine($"First kill. Score is {score}.");
score = score + 2;
Console.WriteLine($"Side quest. Score is {score}.");
score = score + 2;
Console.WriteLine($"Another kill. Score is {score}.");
Console.WriteLine();
int adventurers = 47;
int perParty = 6;
int fullParties = adventurers / perParty;
int leftOver = adventurers % perParty;
Console.WriteLine($"{adventurers} adventurers, {perParty} to a party.");
Console.WriteLine($"Parties: {fullParties}");
Console.WriteLine($"Left over: {leftOver}");

Task 6: The Entry Bill

  • A full party pays 25 to enter. A leftover adventurer pays 7 to join a mixed party.
  • Print the party total, the solo total, and the sum of both.
  • Do the arithmetic inside the holes. No new variables for the three totals.
  • This goes below Task 5's program, separated by another blank Console.WriteLine().
  • Now change adventurers to 100 and run it again. Every number should move. Set it back to 47.
Arithmetic, Reassignment, and Interpolation
string characterName = "Sephiroth";
int score = 0;
Console.WriteLine($"{characterName} enters the dungeon at {score}.");
score = 3;
Console.WriteLine($"First kill. Score is {score}.");
score = score + 2;
Console.WriteLine($"Side quest. Score is {score}.");
score = score + 2;
Console.WriteLine($"Another kill. Score is {score}.");
Console.WriteLine();
int adventurers = 47;
int perParty = 6;
int fullParties = adventurers / perParty;
int leftOver = adventurers % perParty;
Console.WriteLine($"{adventurers} adventurers, {perParty} to a party.");
Console.WriteLine($"Parties: {fullParties}");
Console.WriteLine($"Left over: {leftOver}");
Console.WriteLine();
int partyFee = 25;
int soloFee = 7;
Console.WriteLine($"Party entries: {fullParties * partyFee}");
Console.WriteLine($"Solo entries: {leftOver * soloFee}");
Console.WriteLine($"Total: {fullParties * partyFee + leftOver * soloFee}");

Challenge (Optional): Predict Before You Run

  • Write these four as comments, and next to each write what you think it will print:
  • 10 - 2 * 3   (10 - 2) * 3   7 / 2   7 % 2
  • Now print them and compare. The ones you got wrong are the ones worth remembering.
divider

Same Idea, Exam Notation

C#
int total = 17 + 5;
int extra = 17 % 5;
score = score + 2;
Exam Notation
total ← 17 + 5
extra ← 17 MOD 5
score ← score + 2
  • +, -, *, and / are written the same way in both.
  • Remainder is spelled MOD instead of %. Same operation, same priority.
  • Assignment is an arrow. score ← score + 2 is the same instruction as score = score + 2;, read the same way: right side first.
Division is the one place the two disagree, and it costs points in both directions. On the reference sheet 17 / 5 is 3.4. In C# it is 3. Trust the sheet on the exam and C# in your programs — and remember that the sheet never has to worry about types, because it has none.

The sheet also lists one thing you have no C# match for yet:

Exam Notation
roll ← RANDOM(1, 6)

RANDOM(a, b) gives a whole number from a to b, and both ends are included — so RANDOM(1, 6) can give a 1 or a 6. Getting randomness in C# takes a little more, and Activity 1.16 does it.

divider

Checkpoint

Your finished program should produce this.

Example Output
Sephiroth enters the dungeon at 0.
First kill. Score is 3.
Side quest. Score is 5.
Another kill. Score is 7.
47 adventurers, 6 to a party.
Parties: 7
Left over: 5
Party entries: 175
Solo entries: 35
Total: 210
  • The score reaches 7 without 7 appearing anywhere in your program.
  • No Console.Write and Console.WriteLine pair is left.
  • Changing adventurers to 100 moves every number below it.
  • All five Task 4 results are written down, copied from the screen.
divider

Reflection

Answer the following questions before submitting your work.

  1. score = score + 2; would be false as a math equation. Describe, in order, what C# actually does with that line.
  2. 47 / 6 gave 7 and 47.0 / 6 gave 7.833333333333333. Only one character changed. Explain why that changed the answer.
  3. The exam reference sheet says 17 / 5 is 3.4. C# says it is 3. Both are correct. How would you explain that to someone who thinks one of them must be wrong?
  4. Your Task 5 rewrite is shorter than the version it replaced. Name one thing that got easier to read, not just shorter.
divider

Submit

Submit four things to the dropbox:

  1. Your 1-6-arithmetic program file.
  2. A copy of its output.
  3. Your five Task 4 results and your two line counts from Task 5.
  4. Your four reflection answers.

Activity Complete