DEV Community

Sabin Sim
Sabin Sim

Posted on

04. C# (bool and Comparison Operators )

0) Goal of This Lesson

bool represents the result of a question,
and comparison operators are how we ask those questions in code.

This is not about memorizing syntax.
It’s about running the code and seeing how decisions start to appear.


1) What Is bool?

The bool type represents a Boolean value.

It can hold only two values:

  • true
  • false
bool isCorrect = true;
bool isFinished = false;
Enter fullscreen mode Exit fullscreen mode

Key idea

  • bool does not store data
  • it stores the result of a condition
  • the answer to a yes / no question

2) Why Do We Need bool?

Consider this question:

“Is the user input equal to ABC?”

This is not a number.
This is not a string.

👉 It’s a question.

So the result must be:

  • true
  • or false

3) Run This Example

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Type something:");
        var userInput = Console.ReadLine();

        bool isEqualToAbc = userInput == "ABC";

        Console.WriteLine(isEqualToAbc);
    }
}
Enter fullscreen mode Exit fullscreen mode

Try different inputs

  • Input: ABC → Output: true
  • Input: XYZ → Output: false

This is the first real decision your program makes.


4) = vs == (Very Important)

These two are not the same.

Assignment

userInput = "ABC";
Enter fullscreen mode Exit fullscreen mode
  • puts a value into a variable

Comparison

userInput == "ABC";
Enter fullscreen mode Exit fullscreen mode
  • asks a question
  • returns a bool
Operator Meaning
= assignment
== equality comparison

📌 == always produces a bool.


5) Understanding by Observing (Runtime Thinking)

Instead of trusting the output, focus on this line:

bool isEqualToAbc = userInput == "ABC";
Enter fullscreen mode Exit fullscreen mode

At runtime, this variable becomes:

  • true if the input matches
  • false if it doesn’t

This is how program logic begins.


6) Checking “Not Equal”

Using the inequality operator

bool isNotEqual = userInput != "ABC";
Enter fullscreen mode Exit fullscreen mode
  • != means “not equal”

Using logical negation

bool isEqual = userInput == "ABC";
bool isNotEqual = !isEqual;
Enter fullscreen mode Exit fullscreen mode
  • ! means NOT

📌 In practice, != is usually clearer and preferred.


7) Comparing Numbers

int number = 10;

bool greaterThan = number > 5;
bool smallerThan = number < 10;
bool greaterOrEqual = number >= 10;
bool smallerOrEqual = number <= 6;
Enter fullscreen mode Exit fullscreen mode

Comparison operators

Operator Meaning
> greater than
< less than
>= greater than or equal
<= less than or equal

⚠️ Order matters:

=>  // ❌ invalid
>=  // ✅ correct
Enter fullscreen mode Exit fullscreen mode

8) The % (Modulo) Operator

int number = 10;
bool isEven = number % 2 == 0;
Enter fullscreen mode Exit fullscreen mode

What % means

The remainder after division

Examples:

  • 10 % 31
  • 10 % 20

Common real-world uses:

  • even / odd checks
  • periodic execution
  • “every Nth time” logic

9) What Not to Think About Yet

if statements
❌ full control flow
❌ complex logic

Right now, the goal is simpler:

One question → one bool

Examples:

  • Is the input "ABC"?
  • Is the number greater than 10?
  • Is the value even?

10) 30-Second Self Check

Explain the results without running the code:

bool result1 = "ABC" == "ABC";
bool result2 = 10 > 20;
bool result3 = 10 % 2 == 0;
Enter fullscreen mode Exit fullscreen mode

If you can explain why each is true or false,
👉 this lesson is complete.


One-Line Summary

bool doesn’t store data —
it stores the result of a condition.

This is the point where a C# program stops just running
and starts thinking.

Top comments (1)

Collapse
 
peacebinflow profile image
PEACEBINFLOW

This lesson is where things usually click for people.

Up until here, code just runs top to bottom. The moment you introduce bool, the program starts having opinions about reality — is this true or not? Everything after this (if-statements, loops, validation, business rules) is built on this exact idea.

I really want beginners to sit with this part and actually run the examples. Don’t rush to if yet. If you can explain why 10 % 2 == 0 is true without executing it, you’re already thinking like a programmer.

If you’re learning C#, try this: write your own questions.

Is the number odd?

Is the input empty?

Is the value outside a range?

If you can turn real-world questions into bools, the rest of the language becomes way easier.