2.1: Boolean Logic

In this module we'll be learning about booleans, boolean logic, writing conditionals, and learning when to use booleans.

Comparison Operators

It's rare to use booleans without using comparison operators. Here are some comparison operators in C#, if you're studied other languages, you'll notice similarities:

Operator

Meaning

==

Equality

!=

Inequality

<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to

&&

And

||

Or

Boolean Logic - if

You can use boolean logic to control the flow of the code in your programs. One way to control this flow is using if statements. if statements return a true or false value.

if statements are used with logical and relational operators. The operator == compares the variables, and the if statement contains the logic that will run depending on the result of the previous comparison.

Type the code below inside the Main function in your Program.cs file:

class Program
{
    static void Main(string[] args)
    {
                //1
        int value = 5;
                //2
        if (value < 10)
        {
                //3
            Console.WriteLine("Yes, the expression is true");
        }
    }
}

Visual Representation

  1. This is our declaration and initialization of a variable. It is the value we are comparing.

  2. Everything in the () is part of a condition. Here we are checking to see if our boolean is true or false.

  3. If the expression value < 10 is true, then we will execute the Console function inside the curly braces.

Next: Boolean Challenges

Last updated