Most computer languages make a decision regarding what to do next based upon wether a certain expression is true or false. The keyword if is often used for these purposes, as we shall see here. Following is a list of articles on this topic, and the general introductory material is below that.
Decisions are made by evaluating Boolean expressions. Boolean expressions ask questions like:
Does x equal 3?
The 'x equal 3' part in the above question would be the Boolean expression.
In our demo the above question would look like:
if x == 3
The 'x == 3' part in the above code would be the Boolean expression. Yes, that is two equal signs. More about that in a bit.
Again, when decisions are made, Boolean expressions are evaluated. They evaluate to true or false. In our example, the variable x may hold a value of 3, which would make the Boolean expression return true. If x does not equal 3, then the Boolean expression is false.
In the Boolean expression 'x == 3', the '==' part is called a Boolean operator. This Boolean operator means equals. There are other Boolean operators that are used when writing Boolean expressions. Here is a table of some Boolean operators and their meanings:
Boolean operator |
Meaning |
== |
Equals |
!= |
Does not equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
Here is a table that has several examples of how to say and code many Boolean expressions:
Spoken language |
Computer language |
x equals 3 |
x == 3 |
x does not equal 3 |
x != 3 |
x is greater than 3 |
x > 3 |
x is less than 3 |
x < 3 |
x is greater than or equal to 3 |
x >= 3 |
x is less than or equal to 3 |
x <= 3 |
A computer program can change its course of action depending upon its evaluation of a Boolean expression. For example, in a game program a decision could be made if the player just won the game. Suppose it takes 10 or more points to win the game, and suppose that the player's points are kept in a variable named p. Then we would see code like this:
if p >= 10
If the Boolean expression 'p >= 10' evaluated to true, then it is time to issue congradulations, the player won. If not, it's time to do something else, probably to let the player keep playing the game.
THE HEADER So, these Boolean expressions show up as parts to statements in a computer language. They often appear in 'if' statements. These statements judge the value of their Boolean expression and decide if it is true or false. If it's true, the program jumps to block of lines and executes them. If the Boolean expression is false, the program could jump to a different block of code, different from the true block, and execute the code there, in the false block. Therefore, the if statement can branch a program in different directions. By the way, Boolean expressions are part of Boolean algebra, which was developed by mathematician, philosopher, and logician, George Boole. |
Suggested next article:
An article related to this demo: