Boolean Variables


During this sub-section, we will be dealing with a type of variable, called a Boolean. Booleans can only store two values, true and false. For example, if you wanted to track whether a person competed a checkpoint or not, or if they won the game to show a victory splash screen while the person that lost, gets a losing screen. What we will be doing with this, is declaring a Boolean variable and then initializing that Boolean. To demonstrate variables better, create a new console program by the name of "variEx". This will provide us with a space where we can practise with all of the variables.



In order to declare a boolean variable, you need to type the following code ‘Boolean bVari;’ on the first line so it looks like this. What this does, is it creates a boolean variable and labels it according to what you stated it as.



To initialize that boolean, write this code on a second line. ‘bVari = true;’. This will take the name that you gave it and gives it a value of true.



Once you’re done with that, let’s put our boolean to work! Create an if/else statement like this and make the variable value true. As well as add the code after everything so the program stays open until told to close, 'Console.ReadLine();'.



In the if portion, write the code 'Console.WriteLine("This boolean is true!");'. In the else portion, put the code 'Console.WriteLine("This boolean is false!");'. Your code should now look like this.



Now to see what happens when we change the original value of the variable. If we run it as it is, it'll show that the boolean is true. Now if we change the boolean value to false, and then run it, it will display that the boolean is false.



That was pretty easy! You learned how to declare and initialize a variable (specifically a boolean in this case), which is pretty much what you do for every section of the variables section. The next sub-section will be about creating an integer variable or also called an int.