Step 1 of 6 · Reading · ~4 min
Learn
Basics
Variables and Types
By the end of this lesson you can store values in typed variables, convert input text into numbers, and predict what integer division does to your results.
C# is statically typed: every variable has a type fixed at compile time, and the compiler checks every use of it before the program runs. You write a little more up front, and mistakes like adding a date to a number are caught immediately.
int age = 30; // 32-bit whole number, about +/- 2.1 billion
long population = 8000000000L; // 64-bit whole number (note the L suffix)
double ratio = 0.75; // 64-bit floating point
decimal price = 9.99m; // exact decimal arithmetic, for money (note the m suffix)
bool done = false; // true or false
string title = "Dune"; // text, double quotes
char grade = 'B'; // exactly one character, single quotes
var count = 42; // the compiler infers int from the 42
var is not dynamic typing: count is an int for the rest of its life. It only saves you from writing the type name twice. A double stores binary fractions, so 0.1 + 0.2 is not exactly 0.3; use decimal when cents matter.
Whole-number division and remainder
When both operands are int, / divides and throws away the fraction, and % gives what is left over:
int candies = 17;
int perBox = 5;
Console.WriteLine(candies / perBox); // 3 (not 3.4)
Console.WriteLine(candies % perBox); // 2 (17 = 3 * 5 + 2)
Console.WriteLine(17 / 5.0); // 3.4 (one double operand: real division)
double wrong = 17 / 5; // 3 -- the division happens first, as ints
Together, / and % split a quantity into whole units and a remainder. With three units, you must remove the biggest unit before counting the next one. Here 287 cents become dollars, quarters and loose cents:
int cents = 287;
int dollars = cents / 100; // 2
int quarters = cents % 100 / 25; // 87 / 25 = 3
int loose = cents % 25; // 287 = 11 * 25 + 12, so 12
Skip the % 100 and quarters would count the two dollars too: 287 / 25 is 11.
A whole-number type also has a ceiling. One past the top silently wraps around:
int top = int.MaxValue; // 2147483647
Console.WriteLine(top + 1); // -2147483648
Text versus numbers
Console.ReadLine() always returns a string, even when the characters are digits. To do arithmetic you must parse:
int units = int.Parse(Console.ReadLine()); // "12" -> 12
double.Parse does the same for text holding a fraction: double.Parse("2.5") is the number 2.5.
Without the parse, + on two strings means "glue together": "3" + "4" is "34". When a string and a number meet around +, the number is turned into text, which is how you build a line from pieces:
int wins = 4;
Console.WriteLine("wins: " + wins + "!"); // wins: 4!
int.Parse throws an exception (a run-time error that stops the program) on text that is not a number. When input might be garbage, use TryParse, which returns true or false instead and writes the number into an out variable:
int n;
bool ok = int.TryParse("12x", out n);
Console.WriteLine(ok); // False -- "12x" is not a number
The conditionals lesson shows how to act on that true or false, and the exceptions lesson shows how to catch a thrown error.
Common mistakes
- Adding strings instead of ints. Without
int.Parse,"3"and"4"print34instead of7. - Expecting a fraction from
int / int.7 / 2is3. Make one side adoubleif you want3.5. - Forgetting to remove the bigger unit first. If the middle unit is just
total / size, it still contains the whole larger units, so it comes out far too big. - Using
doublefor money. Binary rounding error accumulates;decimaldoes not.
Your exercise
The starter reads a whole number of seconds. Print how many full hours, minutes and seconds it makes up, in the exact format shown. The most likely mistake is letting the minutes count include whole hours (3661 seconds is 1 hour 1 minute, not 61 minutes).
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…