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

Boolean type behavior in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Booleans help us make decisions in code by representing true or false values.

Checking if a user is logged in or not.
Deciding if a light switch is on or off.
Verifying if a password is correct.
Controlling if a game character is alive or dead.
Determining if a form input is valid.
Syntax
C Sharp (C#)
bool variableName = true; // or false

Boolean variables can only hold true or false.

Use booleans to control program flow with if statements.

Examples
This creates a boolean variable named isRaining and sets it to true.
C Sharp (C#)
bool isRaining = true;
This creates a boolean variable named hasFinished and sets it to false.
C Sharp (C#)
bool hasFinished = false;
This sets isAdult to true if age is 18 or more, otherwise false.
C Sharp (C#)
bool isAdult = age >= 18;
Sample Program

This program shows how a boolean variable isDoorOpen controls the message printed. It starts false, then changes to true, and the if statement checks it.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        bool isDoorOpen = false;
        Console.WriteLine("Is the door open? " + isDoorOpen);

        isDoorOpen = true;
        if (isDoorOpen)
        {
            Console.WriteLine("You can enter the room.");
        }
        else
        {
            Console.WriteLine("The door is closed.");
        }
    }
}
OutputSuccess
Important Notes

Boolean values are case-sensitive: use true and false in lowercase.

Booleans are often used in if, while, and for statements to control flow.

Summary

Booleans store only two values: true or false.

They help your program make decisions.

Use them with conditions to control what your program does.