The Boolean variable type is probably the easiest type of variable. It doesn’t have a “maximum range”, it can’t produce “overflow” exceptions, it’s not affected by precision, etc. In fact, Boolean variable type can only have two values: true or false. They 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:
1 2 3 4 5 6 7 |
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:
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 Tags: boolean variable, variable types, variables