0Pricing
C# Academy · Lesson

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

  1. Arithmetic and Assignment Operators
  2. Comparison and Logical Operators
  3. Bitwise and Shift Operators
  4. Operator Precedence and Associativity
← Back to C# Academy