0
0
C Sharp (C#)programming~5 mins

Logical operators in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Logical operators help you combine or change true/false values to make decisions in your program.

Checking if two conditions are both true before running some code.
Deciding if at least one condition is true to continue an action.
Reversing a true or false value to test the opposite.
Making complex decisions by combining multiple true/false checks.
Syntax
C Sharp (C#)
condition1 && condition2  // AND: true if both are true
condition1 || condition2  // OR: true if at least one is true
!condition               // NOT: reverses true/false

Use && to check if both conditions are true.

Use || to check if at least one condition is true.

Use ! to reverse a condition's true/false value.

Examples
AND returns true only if both sides are true.
C Sharp (C#)
bool a = true && false;  // a is false
OR returns true if at least one side is true.
C Sharp (C#)
bool b = true || false;  // b is true
NOT reverses the value from true to false.
C Sharp (C#)
bool c = !true;          // c is false
Combining OR and NOT to get the opposite result.
C Sharp (C#)
bool d = !(false || true); // d is false
Sample Program

This program decides if you should take an umbrella based on weather and if you have one.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        bool isSunny = true;
        bool haveUmbrella = false;

        if (isSunny && !haveUmbrella)
        {
            Console.WriteLine("Go outside without umbrella.");
        }
        else if (!isSunny || haveUmbrella)
        {
            Console.WriteLine("Take umbrella just in case.");
        }
    }
}
OutputSuccess
Important Notes

Logical operators work with boolean values: true or false.

Use parentheses to group conditions and control order.

Short-circuiting: && stops if first is false, || stops if first is true.

Summary

Logical operators combine true/false values to help make decisions.

&& means AND, || means OR, and ! means NOT.

Use them to write clear and simple conditions in your code.