In all computer programs, all decision making can eventually be reduced to a question of true or false. And because of this, Boolean expressions – truth values – play a central role in programming.
Boolean expressions use relational operators and take primitive types (integers, decimal numbers and single characters) as arguments. The result of boolean expressions are always a boolean value – true or false.
There are the following relational operators:
– greater than
== – equal to (remember that a single “equals sign” means assignment of a value to a variable)
= – greater than or equal to
!= – not equal to
When you compare primitive types in a Boolean expression, simple numbers like integers are easily compared. Decimal numbers on the other hand require some approximation and you can set a constant – usually called TOLERANCE – to determine how much decimal numbers are allowed to diverge before they no longer are to be considered “equal to” or “greater than”.
Single characters are compared by their unicode numbers, that means that A is actually compared through its numerical representation 65.
Reference types such as Strings can’t be compared directly, but should be compared through specialized methods under the String class called “.compare.To” and “.equals” which can be used to compare two strings as two rows of char types.
Boolean expressions can also use logical operators which take boolean arguments and offer resultant types that are boolean.
There are the following logical operators:
! – not
&& – and
|| – or
The way these expressions work in a computer program is whenever a decision is being made. Since the computer only can react upon what it has been programmed to react upon, you have to foresee and program every reaction the program needs to make. The basic way to do this is to set certain conditions that needs to be fulfilled in order for the program to progress. There are several different ways to do this. The simplest is the IF statement:
if (boolean expression)
(then) statement
Some other programming languages need the syntax to be if (this condition is met) then do (this statement). Java doesn’t use that, but I included it here, to make it more obvious how it works.
In a real program, an IF statement might look like this:
if (X>0)
System.out.println(“Have a nice day!”)
Thus, if variable X is greater than 0, the program will print out “Have a nice day!” if the condition isn’t fulfilled, it’ll just skip that part of the program and continue onto the next bit.
This is very basic decision making, and it can be expanded in various ways. There’s the IF-ELSE statement, that allows for two different courses of action, depending on whether the set condition is fulfilled or not:
if (X != 2)
System.out.println(“Have a nice day!”);
else
System.out.println(“Thank you!”);
Here, if X is not equal to 2, the program will print out “Have a nice day!”, otherwise it will print out “Thank you!” before continuing with the rest of the program.
Then there is the WHILE statement:
WHILE (boolean expression)
statement
this kind of decision continues to run, until the boolean expression turns false. This creates the possibility of making an Infinite Loop – eg. if the loop condition is always true.
A WHILE statement might look like this:
while (X>0)
{X–;
System.out.println(“Have a nice day!”);}
Usually, the WHILE statement only uses the next one statement if the condition is met (that variable X is greater than 0), but by putting several statements in {curly braces}, all of those statements in that Code Block are considered as one statement by the initial If. This bit of code will first decrement X by one and then print the words “Have a nice day!” on the screen. Then it will then return to the initial WHILE, and if X is still greater than 0, it will do the same statement again. Thus, if X was set to 5, the WHILE statement would loop 5 times, decrementing X and printing the words 5 times, until X no longer was greater than 0. If there was no decrementation of X, the loop would be an infinite one.
Then there are DO loops, which are very similar to WHILE loops, except for the fact that the condition isn’t evaluated until after the loop which means that the DO loop always runs at least once:
do
statement;
while (boolean expression)
or as a proper example:
do
System.out.println(“what?”);
while(X==5)
which will print out the word “what?” at least once, before examining whether the condition is actually met. If the condition is met (X is equal to 5), then it becomes an infinite loop, as there is no incrementation in the DO statement.
And then there are FOR loops which work best when we already know how many times we want the loop to run:
for (initialisation; condition; increment)
statement;
This loop first initializes a variable, sets a condition through a boolean expression, and increments a value. Then performs a statement. It could look like this:
for (int X = 1; X
So, we initialize X with an integer value of 1, then we say, as long as X is smaller than or equal to 4, increment X by one and print the message “X is still smaller than 4”.
…
Now, with these different forms of loops, it is possible to make much more interesting programs. A really simple one is this MinMax program that I wrote. It allows for the input of two numbers, and outputs the greater of the two.
Or the slightly more advanced Penge program which, based on the user’s input, is capable of adding up your various coinage to tell you how much your loose change is worth.
Yet, these programs are also very simple. As I mentioned earlier, Object Oriented Programming (OOP) such as Java is focused on the idea the program code should be divided and encapsulated as much as possible so that the individual elements – or objects – can be instantiated and modified easily without having to rewrite the entire code.
Java in particular, being a relatively young language with a set design in mind, takes this instantiation to new levels. For instance, Java doesn’t contain a GOTO statement. The GOTO statement is the most primitive form for programming loop you can make. When the program reaches the GOTO statement, it then jumps to the line specified by the statement, thus jumping to another point in the program, possibly to a point earlier in the program, thus looping a section of the code until a set condition is met (if no condition is set, it would be an infinite loop that would continue until you manually stopped the program or the computer couldn’t handle it anymore).
Here’s a simple example of a GOTO program – written in the BASIC programming language:
10 PRINT “BOB IS AWESOME!”
20 GOTO 10which basically will print the line “BOB IS AWESOME!” again and again and again until you stop it.
Though most older programming languages have the GOTO statement, many computer scientists consider it to be a sign of poor programming practice as it would often lead to “spaghetti code” – code full of internal references making it difficult to read and understand – even for the programmer who wrote it.
Java avoids this by design and forces the programmer to divide his program up into several class objects with small but clear functions. All of these objects are then called by the Driver class which contains the main method – where the execution of programs usually start.
The execution of a program is decided by the “Flow of Control”. At first, flow of control recides with the main method, but the main method can in turn call other methods in other objects to perform a function contained within them. By calling another method, the main method transfers flow of control to that method, and they retain control until they in turn call another method or finish their function and returns flow of control to the main method.
This means that Java programs often consist of small sections of code, of methods and classes that are called when needed. Critics of OOP argue that this practice easily results in “Ravioli code” which is the opposite of spaghetti code: A lot of small, encapsulated bits of code that nobody really can tell apart and work out.
In both cases it is a question of being able to read and understand the code in order to be able to modify it, rather than a question of being able to run it. To have maintainable code.
At first, I found that I really just wanted to put in a GOTO statement into my program, to say “just do that bit again”, but since Java doesn’t allow me to do that, I had to change my approach. Instead, I made two separate classes of code: First, a driver class, which really doesn’t contain any more than the beginning of the program, and which then calls one of the three methods in the GameLoop class. By having the code separated like this, you call the bits of code by writing the method name – eg.
GuessingGameLoop.intro();
which transfers flow of control to the intro method in the GuessingGameLoop class. The program works okay, and it is basically a guessing game where the computer “thinks” of a number between one and ten, and then offers you a chance to guess it. Using the loops explained above, I managed to make in such a way that you can play as many times as you like, without having to begin the program again.
If you want to try these little programs out, make sure you have Java 1.5 installed on your computer, then download the compiled .class files:
MinMax
Penge
GuessingGame
GuessingGameLoopand run them from a command line interface using the command
java nameofprogram.class
– just make sure that all of the files are in the folder that you’re trying to run them from. Note that you can’t run GuessingGameLoop on its own, only through its driver class.