Comparison and Logical Operators
Combine conditions with comparison and logical operators.
Comparison and Logical Operators is a free C# Academy lesson on CoddyKit. This is lesson 2 of 4. 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
Comparison Operators
Comparison operators produce a bool: == equal, != not equal, < less, > greater, <= and >=.
using System;
class Program {
static void Main() {
Console.WriteLine(5 == 5);
Console.WriteLine(5 != 3);
Console.WriteLine(5 > 8);
}
}Equality vs Assignment
== compares; = assigns. Mixing them up is a classic bug. C# requires a bool in if, so accidental = usually fails to compile.
using System;
class Program {
static void Main() {
int a = 7;
bool same = (a == 7);
Console.WriteLine(same);
}
}Comparing with Variables
Comparisons work on variables, not just literals. The result is a boolean you can store or print.
using System;
class Program {
static void Main() {
int age = 20;
bool adult = age >= 18;
Console.WriteLine("Adult: " + adult);
}
}Logical AND: &&
&& is true only when both sides are true. It is the logical AND operator.
using System;
class Program {
static void Main() {
int age = 25;
bool ok = age >= 18 && age <= 65;
Console.WriteLine(ok);
}
}Logical OR: ||
|| is true when at least one side is true. It is the logical OR operator.
using System;
class Program {
static void Main() {
string role = "admin";
bool canEdit = role == "admin" || role == "editor";
Console.WriteLine(canEdit);
}
}Logical NOT: !
The ! operator flips a boolean: !true is false and vice versa.
using System;
class Program {
static void Main() {
bool loggedIn = false;
if (!loggedIn) {
Console.WriteLine("Please log in");
}
}
}Short-Circuit Evaluation
&& stops if the left side is false; || stops if the left side is true. The right side is never evaluated then. This is called short-circuiting.
using System;
class Program {
static bool Check() {
Console.WriteLine("Check ran");
return true;
}
static void Main() {
bool result = false && Check();
Console.WriteLine(result);
}
}Short-Circuit as a Guard
Short-circuiting lets you safely test something only when a prior condition holds, avoiding errors.
using System;
class Program {
static void Main() {
string text = null;
if (text != null && text.Length > 3) {
Console.WriteLine("Long text");
} else {
Console.WriteLine("Skipped safely");
}
}
}Combining Conditions
You can chain many conditions. Use parentheses to make the grouping clear and correct.
using System;
class Program {
static void Main() {
int hour = 14;
bool openNow = (hour >= 9 && hour < 12) || (hour >= 13 && hour < 18);
Console.WriteLine("Open: " + openNow);
}
}Comparisons in Branches
Comparison and logical operators are the heart of if statements that drive program flow.
using System;
class Program {
static void Main() {
int score = 82;
if (score >= 90) Console.WriteLine("A");
else if (score >= 80) Console.WriteLine("B");
else Console.WriteLine("C or below");
}
}Boolean Results Are Values
A comparison yields a real bool value. You can return it, store it, or print it directly.
using System;
class Program {
static bool IsPositive(int n) {
return n > 0;
}
static void Main() {
Console.WriteLine(IsPositive(5));
Console.WriteLine(IsPositive(-2));
}
}Quick Check
Test your grasp of logical operators and short-circuiting.
Recap
Comparison operators (== != < > <= >=) produce booleans. Logical operators combine them: && (AND), || (OR), and ! (NOT).
Both && and || short-circuit, skipping the right operand when the result is already known. This makes them efficient and useful as safety guards.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Comparison and Logical Operators” lesson free?
Yes — the full text of “Comparison and Logical Operators” is free to read here on the web. 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. The C# Academy course includes 4 lessons in total.
What will I learn in “Comparison and Logical Operators”?
Combine conditions with comparison and logical operators. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Comparison and Logical Operators” 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
- Arithmetic and Assignment Operators
- Comparison and Logical Operators
- Bitwise and Shift Operators
- Operator Precedence and Associativity