Comparison and Logical Operators
Combine conditions with comparison and logical operators.
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);
}
}All lessons in this course
- Arithmetic and Assignment Operators
- Comparison and Logical Operators
- Bitwise and Shift Operators
- Operator Precedence and Associativity