Sunday, June 15, 2025 01:32

Table of contents >> Introduction > Boolean variable type

Boolean variable type

The Boolean variable type is probably the easiest type of variable. It doesn’t have a “maximum range”, it can’t produce “overflowexceptions, it’s not affected by precision, etc. In fact, Boolean variable type can only have two values: true  or falseThey are almost exclusively used for calculations of logical expressions. They are declared using the keyword bool.

Here is a short example where we can use Boolean variables:


int a = 1;
int b = 2; 

// Which one is greater? 
bool greaterAB = (a > b); 
Console.WriteLine("a is greater than b = " + greaterAB);
Console.Read();

When compiled and ran, the program will display the following output:
boolean variable type

The concepts explained in this lesson are also shown visually as part of the following video:

EXERCISES
1. Declare a variable isFemale of type bool and assign a value to it depending on your gender.

Solution

bool isFemale = true;

Tags: , ,

Leave a Reply