Operators & expressions: arithmetic, comparison, logical
Work with arithmetic operators (+, -, *, /, %), comparisons (==, !=, , =), and logical operators (&&, ||, !). Understand precedence and short-circuiting.
Operators & expressions: arithmetic, comparison, logical is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Operator map
Goal: Use arithmetic, comparison, and logical operators to build clear expressions.
- Math with + - * / %
- Compare values
- Combine checks with && and ||
Arithmetic basics
Arithmetic: / with ints truncates; use doubles for fractional results.
using System;
public class Program
{
public static void Main(string[] args)
{
int a = 9;
int b = 4;
Console.WriteLine("a + b = " + (a + b));
Console.WriteLine("a - b = " + (a - b));
Console.WriteLine("a * b = " + (a * b));
Console.WriteLine("a / b = " + (a / b)); // integer division
Console.WriteLine("a % b = " + (a % b)); // remainder
}
}
Comparison basics
Comparison operators produce bool results you can combine with logical operators.
using System;
public class Program
{
public static void Main(string[] args)
{
int x = 5;
int y = 7;
Console.WriteLine("x == y: " + (x == y));
Console.WriteLine("x != y: " + (x != y));
Console.WriteLine("x < y: " + (x < y));
Console.WriteLine("x <= y: " + (x <= y));
Console.WriteLine("x > y: " + (x > y));
Console.WriteLine("x >= y: " + (x >= y));
}
}
Logical operators
Short-circuit: && stops on false left; || stops on true left. Useful to avoid unnecessary work.
using System;
public class Program
{
static bool ExpensiveCheck()
{
Console.WriteLine("ExpensiveCheck ran");
return true;
}
public static void Main(string[] args)
{
bool ok = false;
// && short-circuits when the left is false
if (ok && ExpensiveCheck())
{
Console.WriteLine("Both true");
}
else
{
Console.WriteLine("Left was false, skipped right");
}
bool isAdmin = true;
bool hasTicket = false;
// || short-circuits when the left is true
if (isAdmin || hasTicket)
{
Console.WriteLine("Allowed");
}
// Negation
Console.WriteLine("!hasTicket = " + (!hasTicket));
}
}
Precedence & ()
Precedence affects results; add parentheses to make intent obvious and avoid mistakes.
using System;
public class Program
{
public static void Main(string[] args)
{
int n = 10;
int m = 3;
bool r1 = n - m * 2 == 4; // * before -
bool r2 = (n - m) * 2 == 14; // grouped
Console.WriteLine("r1 = " + r1);
Console.WriteLine("r2 = " + r2);
// Logical precedence: && before ||, but use () for clarity
bool a = true, b = false, c = true;
bool r3 = a || b && c; // b&&c first → true
bool r4 = (a || b) && c; // grouped → true
Console.WriteLine("r3 = " + r3);
Console.WriteLine("r4 = " + r4);
}
}
Pitfalls to avoid
Avoid surprises: promote to double for fractional results; rely on short-circuiting to skip costly calls.
using System;
public class Program
{
static int Counter = 0;
static int Next()
{
Counter = Counter + 1;
return Counter;
}
public static void Main(string[] args)
{
// Integer division pitfall
int a = 1, b = 2;
Console.WriteLine("1/2 int = " + (a / b)); // 0
Console.WriteLine("1/2 double = " + (a / 2.0)); // 0.5
// Side effects: function called only if needed
bool cond = false;
if (cond && Next() > 0)
{
Console.WriteLine("Will not run");
}
Console.WriteLine("Counter = " + Counter); // 0 because Next() skipped
}
}
Short-circuit behavior
Recap
Recap: Use arithmetic for math, comparisons for decisions, and logical operators to combine checks. Prefer parentheses for clarity and rely on short-circuiting wisely.
Frequently asked questions
Is the “Operators & expressions: arithmetic, comparison, logical” lesson free?
Yes — the full text of “Operators & expressions: arithmetic, comparison, logical” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Operators & expressions: arithmetic, comparison, logical”?
Work with arithmetic operators (+, -, *, /, %), comparisons (==, !=, , =), and logical operators (&&, ||, !). Understand precedence and short-circuiting. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Operators & expressions: arithmetic, comparison, logical” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Value vs reference; literals; var inference; const/readonly
- Operators & expressions: arithmetic, comparison, logical
- Suffixes in practice; casting vs conversion; C# 6 object creation