C#/C# Tutorial

C# Boolean | C# Tutorial for Beginners

DragonTory 2023. 2. 14. 21:35
반응형

C# Boolean

 

In C#, a boolean is a data type that can have one of two values: true or false. 

Boolean values are typically used to represent the result of a comparison or a logical operation.

You can declare a boolean variable like this:

bool b = true;

 

You can also use boolean operators to perform logical operations on boolean values. 

The most common boolean operators are:

  • && (logical and): returns true if both operands are true
  • || (logical or): returns true if at least one operand is true
  • ! (logical not): returns true if the operand is false, and vice versa


Here are some examples:

bool b1 = true;
bool b2 = false;
bool b3 = b1 && b2; // b3 is false
bool b4 = b1 || b2; // b4 is true
bool b5 = !b1; // b5 is false

 

You can also use boolean values in conditional statements to control the flow of your program. 

For example:

bool b = true;
if (b)
{
    // this code will be executed
}
else
{
    // this code will not be executed
}

 

Note that the if statement will execute the code inside its body only if the condition (in this case, the boolean value b) is true. 

If the condition is false, the code inside the else block (if present) will be executed instead.

반응형