← All lessons

Level 1 / C# Fundamentals / fundamental 2 of 7

Math, comparisons, and strings

Combine values with arithmetic, comparisons, logic, and string interpolation.

Variables only become useful when you do something with them. An expression is any piece of code that produces a value: 5 + 3, age >= 18, or $"Total: {total}". These reps drill the operators you will reach for every day, building on the declarations from the last drill.

Understand the idea

Arithmetic and its traps

+, -, *, and / do what you expect, and % gives the remainder. Dividing two ints throws away the remainder (7 / 2 is 3), so use a double on at least one side when you want 3.5.

Comparisons produce bools

==, !=, <, >, <=, and >= each give back true or false, which you can store in a bool. && means both must be true, || means at least one, and ! flips true and false.

Shortcuts and interpolation

score += 50 is short for score = score + 50, and total++ adds one. A $ before a string turns on interpolation, so anything in braces is evaluated and dropped into the text.

English to C#

“Make an int called total equal to 5 plus 3.”

int total = 5 + 3;
int total =
a declaration, exactly as in the last drill
5 + 3
an expression: evaluated first, then stored
;
ends the statement

Rep 1 of 12

Copy it

In English

Make an int called total equal to 5 plus 3.

Type the code shown below. Watch it turn green as you go.

int total = 5 + 3;

Waiting for you to type · 0 of 7 pieces

Ctrl+Enter checks or moves on. Tab indents. Paste is off on purpose: your hands are learning this too.

Reps

0 of 12 done

Reps unlock in order, because each one builds on the last.

How your code is checked

Drills compare what you type with known-correct C#, token by token. Spacing, line breaks, and comments do not matter; spelling, case, and punctuation do. It is not a compiler, so it accepts the listed spellings of each answer and nothing else.

Try it in a real .NET project

In your console app, read a number with int.Parse(Console.ReadLine()), then print its double, its remainder when divided by 3, and whether it is over 100. Then try 7 / 2 and 7 / 2.0 side by side and explain the difference.

Check your understanding

What does int result = 7 / 2; store, and how would you get 3.5 instead?

Show the explanation

It stores 3. Both operands are ints, so C# does integer division and drops the remainder. Making either side a double, such as 7 / 2.0 or (double)7 / 2, and storing into a double gives 3.5.

Goose School teaches Microsoft's stack. Goose Solutions is not affiliated with, endorsed by, or sponsored by Microsoft.