The Concept of Logic and Decision Making
Up to this point in your learning, you have essentially been programming and using your computer as a fancy and rather expensive calculator. You have looked at and written programs that accept input, do some kind of processing, and then provide output. While that can be pretty handy in some cases, there is much more you can do with a computer, and your programs.
The process of decision making in a computer is also called selection or branching. It simply means that the computer can make decisions based on the data with which it is working and/or the circumstances under which the computer is working. Decision making in a computer is simple; it allows computers to decide if a circumstance is true or if it is not, and then the programming process follows a path (or branch) that represents the pre-designated actions appropriately related to that decision.
For example, in your attempt to calculate the roots of a quadratic equation, you begin by calculating the discriminant, which is b2 – 4*a*c. There are three potential results of this mathematical expression; in a generalized way, the result could be negative, it could be positive, or it could be zero. Now the program must consider these three results because they all have a set of actions that would be appropriately related to them. If the result is negative, no roots can be calculated; indeed, if the program attempted to calculate the square root of this quantity, it would be halted because the computer cannot conduct that calculation. If the result is zero, only one root should be displayed, and if the result is positive, two roots can be displayed.
There are two things to note from this analysis. First, you should be able to see why the decision-making process is also called branching or selection. In the course of selecting the right circumstances, the program must follow a path – or branch – of pre-determined activities related to any of the three possible results. Second, it might make you think that a computer can make three-way decisions, or four-way, five-way, ten-way, or beyond decisions. This is not the case. A computer can only make a yes or no, or true or false, kind of decision. Since there are only two choices from any given test in a computer, this is sometimes called a binary decision-making process. The processes are also called Boolean operations in deference to George Boole, who came up with some symbolic tools and rules for managing them.
Boolean conditions represented as binary (i.e., either/or) can be demonstrated with the simplest of actions. For example, a computer can decide if something is: 1) greater than zero, or 2) not greater than zero, which would make it less than or equal to zero. A computer can decide if something is: 1) equal to zero, or 2) not equal to zero, which would make it either greater than or less than zero. Even though there may be more than two potential outcomes for a given decision, the computer keeps it simple.
To be complete, most programming languages, including Java, can combine some tests. For example, a computer can decide if something is: 1) greater than or equal to zero, or 2) not greater than or equal to zero, which would make it less than zero. Again, multiple possible outcomes, but the computer’s part is yes or no, true or false. This is the applied essence of logic; a thing is either true or it is not true (i.e., false). Simple as these actions are however, you will be able to accomplish powerful actions with your programs when you are using the decision-making processes.
The next few topics will guide you through the use of the Boolean variable to hold these binary decisions, and it will help you work through Boolean expressions that generate a true or false result from their comparison actions. As you learned earlier in this reference, the Boolean variable, with the data type boolean, can hold a value of either true or false. So, after this brief discussion of the concepts, you will notice that true and false are shown as programming identifiers in the text. Continue on with the next topics but always keep in mind that there can only be one of two results for any given Boolean test or action (i.e., don’t let it get complicated).