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 playerName = "Maya Okonkwo";
int jerseyNumber = 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

int score = 0;
score = 3;

The second line 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

score = score + 2;

Work out the right side using the value in the box now.

Then put the answer back in the same box.

Read every assignment right to left.

Computing Is Not Storing

score + 2;
Terminal window
1-6-gameday.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

8 / 3 printed 2.

Not 2.67. Not 3.

Where did the rest of it go?

Two Whole Numbers In, a Whole Number Out

8 and 3 are both int.

So C# does whole-number division and 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

  • Is a number even? Check whether n % 2 is 0.
  • Splitting people into groups? The remainder is who is left out.
  • Wrapping around a clock or a calendar? Remainder.

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?

% has the same priority as * and /.

When you are unsure, add parentheses.

They cost nothing and they remove all doubt.

Two Lines to Print One

Console.Write("Score: ");
Console.WriteLine(score);

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

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

Put the Value in the Sentence

Console.WriteLine($"Score is {score}.");

The $ before the quote turns on the holes.

Braces mark a hole. C# drops the value in. This is interpolation.

A Hole Can Hold a Calculation

Console.WriteLine($"Two more puts her on {score + 2}.");

Anything that produces a value fits in a hole.

The variable itself does 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-gameday, 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.

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


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 playerName = "Maya Okonkwo";
int score = 0;
Console.Write(playerName);
Console.WriteLine(" tips off.");
Console.Write("Score: ");
Console.WriteLine(score);
score = 3;
Console.Write("Score: ");
Console.WriteLine(score);
Output
Maya Okonkwo tips off.
Score: 0
Score: 3

Task 2: Update It From Itself

  • Add two more baskets, each worth 2, and print the score after each.
  • Do not type 5 or 7 anywhere. The program works them out.
  • Note: the green lines marked with a + sign are new. Do not type the + sign.
Arithmetic, Reassignment, and Interpolation
string playerName = "Maya Okonkwo";
int score = 0;
Console.Write(playerName);
Console.WriteLine(" tips off.");
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);
Output
Maya Okonkwo tips off.
Score: 0
Score: 3
Score: 5
Score: 7

Task 3: Splitting the Tournament

  • Forty-seven players sign up. Teams hold six.
  • Work out the number of full teams and the number of players left over.
  • Both values must be calculated, not typed in.
  • Check it by hand first: 6 × 7 = 42, and 47 − 42 = 5.
Arithmetic, Reassignment, and Interpolation
Console.WriteLine();
int players = 47;
int perTeam = 6;
int fullTeams = players / perTeam;
int leftOver = players % perTeam;
Console.Write("Players: ");
Console.WriteLine(players);
Console.Write("Full teams: ");
Console.WriteLine(fullTeams);
Console.Write("Left over: ");
Console.WriteLine(leftOver);
Output
Players: 47
Full teams: 7
Left over: 5

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.


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.
Arithmetic, Reassignment, and Interpolation
string playerName = "Maya Okonkwo";
int score = 0;
Console.WriteLine($"{playerName} tips off at {score}.");
score = 3;
Console.WriteLine($"Three-pointer. Score is {score}.");
score = score + 2;
Console.WriteLine($"Layup. Score is {score}.");
score = score + 2;
Console.WriteLine($"Another two. Score is {score}.");
Console.WriteLine();
int players = 47;
int perTeam = 6;
int fullTeams = players / perTeam;
int leftOver = players % perTeam;
Console.WriteLine($"{players} players, {perTeam} to a team.");
Console.WriteLine($"Full teams: {fullTeams}");
Console.WriteLine($"Left over: {leftOver}");
Output
Maya Okonkwo tips off at 0.
Three-pointer. Score is 3.
Layup. Score is 5.
Another two. Score is 7.
47 players, 6 to a team.
Full teams: 7
Left over: 5

Task 6: The Entry Bill

  • A full team pays 25 to enter. A leftover player pays 7 to join a mixed team.
  • Print the team total, the solo total, and the sum of both.
  • Do the arithmetic inside the holes. No new variables for the three totals.
  • Now change players to 100 and run it again. Every number should move. Set it back to 47.
Arithmetic, Reassignment, and Interpolation
Console.WriteLine();
int teamFee = 25;
int soloFee = 7;
Console.WriteLine($"Team entries: {fullTeams * teamFee}");
Console.WriteLine($"Solo entries: {leftOver * soloFee}");
Console.WriteLine($"Total: {fullTeams * teamFee + 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
Maya Okonkwo tips off at 0.
Three-pointer. Score is 3.
Layup. Score is 5.
Another two. Score is 7.
47 players, 6 to a team.
Full teams: 7
Left over: 5
Team 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 players 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-gameday 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