Thursday, February 23, 2017

Game Development: Boolean

What is Boolean? Well, Boolean, despite sounding like a funny word, is a condition that is true, or false, and is normally used in loops, such as this:
if (age < 16)
{
   Console.WriteLine("Welcome");
}
The IF statement has a condition, the piece of code within the brackets. The condition, in our case, says that if the variable age is less than the number 16, the program will continue and print out Welcome to the user. If the condition is false, however, the program will end. 

Within the condition in this if statement, we can use logic gates. Before I go on, there are 3 logic gates; AND, OR or NOT gate. 

AND
An AND gate will compare two conditions, and if only BOTH conditions are true, the program will carry on. Logic gates can be represented in a table, such as the one I will attempt to make below.

A    B   Output
0    0    0
1    0    0
0    1    0
1    1    1

The table up above shows a visual representation of the definition I have given. If both conditions are false (0), the output will be false, and the program will end, or simply, it will not commence through with the statement. If one condition is true (1), and the other is false, the output will be false again. In order for the program to continue or commence with the statement, both conditions needs to be true. An AND operation can be expressed by using && within the condition in C#. 

OR
An OR gate will compare two conditions, and if ONE condition is true, the program will carry on or continue. 

A   B   Output
0   0   0
1   0   1
0   1   1
1   1   1

If no conditions are true, the output will be false, and the program, as mentioned countless times before, will end. An OR operation can be expressed by using || within the condition in C#.

NOT
A NOT gate is different than the other logic gates that I have mentioned. The previous logic gates has 2 conditions, whereas a NOT gate only has 1 condition. If one condition is false, the output will be true, and vice versa. 

A   Output
0   1
1   0


As mentioned before in the previous blog entry, I was reading a book by Roland Backhouse, called Algorithmic Problem Solving, and I covered the first chapter of the book about algorithms. C# and other form of high-level language programs available are all mathematically based, meaning that most of the keywords the program has in its System (using System;) is, in some way or form, is connected to maths. Most of the functions we have in C#, are all math symbols.

Functions used in C#
< or > = Greater or less than
<= or >= Greater than or equal to, less than or equal to
= = The assign value (the program takes whatever is on the right, and assign it to the left)
== = Equal or equivalent to
!= = Not equal to
+ - * / = Add, subtract, multiply, divide
-- = Minuses 1 every time a loop is successful
++ = Adds 1 every time a loop is successful

These are some of the functions the user is able to use at their disposal. Some of these functions can be used in conditions, as mathematical equations, comparing 2 values and allowing an if statement or loops to commence within a program. 










No comments:

Post a Comment