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
8
9
10
11
12
13
14
15
16
17
18
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int
a =
1;
int
b =
2;
// Which one is greater?
bool
greaterAB = (a >
b);
Console.WriteLine("a is greater than b = " + greaterAB);
Console.ReadLine();
}
}
}
|
When compiled and ran, the program will display the following output:
a is greater than b = False
The concepts explained in this lesson are also shown visually as part of the following video: