Lesson ThreeOne type of control construct is the Boolean algebra. For those that have had some programming, this will be old news. Boolean algebra can be considered a logical eliminator. In Boolean algebra, you can have three different ways to look at a problem and decide whether it is true or false. The first way is and. If two conditions are both false, then the whole equation is false (false and false = false). If one condition is false and the other is true, seeing as something can't be true and false at the same time, the whole equation is false (false and true = false). However, if both conditions are true, then the whole equation is true (true and true = true). The second way of looking at a problem is or. The only rule with this technique is that if at least one condition is true, the whole equation is true. For example: The third way is not. This means that each condition is the opposite of what it is. So, not true = false, and not false = true. You can use any combination of these Boolean expressions to resolve programming problems you may have. However, you cannot use words to do the comparisons. In programming, we use symbols. For and, we use &&, for or, we use ||, and for not, we use !. Another part of Boolean algebra is the bool type. Like int, float, and char, bool can be use to define a variable as a true or false type. For instance: bool MoreDataToProcess = true; sets "MoreDataToProcess" to true until you program it to be false. This could be part of any loop process where, once you reach the end of the loop, "MoreDataToProcess" would become false. We'll learn more about loops later in the lesson. Some older or early generation compilers may not yet have the bool type. It is then that you need to create a source file to define it. Say we named it "boolean.h". This would be a separate file from your actual program that you would include in the program. If we created the program:
#ifndef BOOLEAN_H This now defines what the bool type is. We then add: #include "boolean.h" to the main program so that we can use the bool type whenever we need it. |