Menu
Coddy logo textTech

Logical Operators Part 2

Part of the Fundamentals section of Coddy's C# journey — lesson 22 of 69.

Logical operators have a special table called "Truth table" that shows what the combination of logical operators returns.

Truth table for the and (&&) operator:

aba && b
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

The only way to get a true for the and (&&) operator is if both a and b are true

Truth table for the or (||) operator:

aba || b
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

In this case, to get a true result, either a or b should be true.

Truth table for the not (!) operator:

a!a
falsetrue
truefalse

Here the value of a is reversed. If a is false then !a is true

challenge icon

Challenge

Beginner

You need to assign integer values to variables b1 and b2 so that b3 evaluates to true in the expression: b3 = !((b1 + b2) > (b1 * b2)).

Cheat sheet

Truth tables show the results of logical operator combinations:

AND operator (&&): Returns true only when both operands are true

aba && b
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

OR operator (||): Returns true when either operand is true

aba || b
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

NOT operator (!): Reverses the boolean value

a!a
falsetrue
truefalse

Try it yourself

using System;

class Program {
    public static void Main(string[] args) {
        // Type your code below
        int b1 = ?
        int b2 = ?
        bool b3 = !((b1 + b2) > (b1 * b2));
        
        // Don\'t change the line below
        Console.WriteLine("b3 = " + b3);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals