Learn arithmetic, comparison and logical operators, the integer division trap, and safe conversion with Parse, TryParse and Convert.
Arithmetic operators
int a = 17, b = 5;
Console.WriteLine(a + b); // addition
Console.WriteLine(a - b); // subtraction
Console.WriteLine(a * b); // multiplication
Console.WriteLine(a / b); // division
Console.WriteLine(a % b); // remainder (modulo)
22
12
85
3
2
Careful: the result of 17 / 5 is 3, not 3.4! When both sides are int, the division is also an integer division and the fraction is thrown away. The remainder operation (%) is used a lot for jobs such as finding out whether a number is even or odd.
The integer division trap
If you want a decimal result, make at least one side a double:
int a = 17, b = 5;
Console.WriteLine(a / b); // 3
Console.WriteLine((double)a / b); // 3.4
Console.WriteLine(a / 5.0); // 3.4
Writing (double) in parentheses is called a type cast. First a is converted to a double, then the division is performed.
Compound assignment and increment
int counter = 10;
counter += 5; // counter = counter + 5 -> 15
counter -= 3; // 12
counter *= 2; // 24
counter++; // 25
counter--; // 24
Comparison and logical operators
Comparison operators (==, !=, <, >, <=, >=) always return a bool. Logical operators combine them: && (and), || (or), ! (not).
double voltage = 3.3;
bool isValid = voltage >= 3.0 && voltage <= 3.6;
Console.WriteLine(isValid); // True
bool alarm = voltage < 3.0 || voltage > 3.6;
Console.WriteLine(alarm); // False
Console.WriteLine(!alarm); // True
Common mistake: == is used for equality; = is assignment. Also, comparing double values with == is dangerous; the expression 0.1 + 0.2 == 0.3 gives False. Compare decimals approximately: Math.Abs(x - y) < 1e-9.
From text to number: Parse and TryParse
ReadLine gives you text; you have to convert it before doing arithmetic with it.
Console.Write("Resistance (ohm): ");
string? input = Console.ReadLine();
int resistance = int.Parse(input!); // if invalid, the program stops with an ERROR
Console.WriteLine(resistance * 2);
If the user types "abc", Parse throws an error. For user input, the safer way is TryParse: it tells you as a bool whether it succeeded and puts the result into its out variable.
Console.Write("Voltage (V): ");
string? input = Console.ReadLine();
if (double.TryParse(input, out double voltage))
{
Console.WriteLine($"The voltage you entered: {voltage} V");
}
else
{
Console.WriteLine("Please enter a valid number.");
}
TryParse looks at the current regional settings, so which decimal separator is accepted depends on them: with English (US) settings you type 3.3, while on a machine set to Turkish you type 3,3, and typing the wrong one may be read as a different value.
The Convert class and implicit conversion
Conversion from a smaller type to a larger one happens automatically (implicit): an int can be put into a double. Conversion from a larger type to a smaller one may lose information, so you have to write it explicitly:
int whole = 7;
double decimalValue = whole; // implicit, no problem
double measurement = 9.99;
int truncated = (int)measurement; // 9: the fraction is dropped, no rounding
int rounded = Convert.ToInt32(measurement); // 10: rounds
Console.WriteLine($"{truncated} {rounded}");
9 10
Tip: Any data coming from the user can be wrong. When you take input, first validate it with TryParse, then use it. This habit saves you a lot of debugging.
Try it yourself
Write a program that takes two numbers from the user (with TryParse) and prints their sum, difference, product and quotient (as a decimal). If the second number is zero, it should print "Cannot divide by zero" instead of dividing.
// Mini quiz
Click an option; you will see right away, with the reason, whether it is correct or not.
Mark it when you finish: your progress is saved and you earn +20 XP.
Tip: ← → keys let you move between lessons.