So far, all our programming has taken a linear form - every time we run the programme the same thing happens, with the only variant being if we output some data that we have read in using the scanner. Our code does not, as yet, take any decisions for us.
The simplest way to enable a decision to be made by the computer is to use an if
statement. This allows us to check if a condition has been met (if the condition has been met, then it is said to be 'true' otherwise it is 'false'). If the condition has been met, then the computer will take some form of action. An analogy of this would be a bartender cashier checking if a customer is 18 (the condition) before selling them a Jagerbomb (the action). We could model this in code as follows:
int drinkingAge = 18;
int customerAge = 18;
if (customerAge == drinkingAge) {
System.out.println("Here is your JagerBomb");
}
There are a few key elements to the 'If' statement, firstly the keyword if
, followed by a statement in parentheses (round brackets). The statement above checks if the customerAge variable is equal to 18. Finally, a set of braces (curly brackets) are used to contain some code, which is only executed if the condition within the parentheses is met.
One common mistake new programmers often make is to confuse the assignment operator: =
(used to set the value for a variable), with the equality check operator: ==
. One way to remember which to use, is to think of the single equals sign as representing the word 'is', and the double equals to represent the words 'is equal'. In the above code we first create a variable called customerAge and say that it is 18, and we later check, in the 'if' statement, that the customer age is equal to 18.
customerAge
variable to 17You may have noticed that the above code is not particularly useful, in that if the customer is over 18 they will not get their Jagerbomb. This is because the check only confirms if the value is exactly 18. We can check if a value is over a certain value by using the greater than operator: >
.
An example solution to this would appear as follows:
if (customerAge > drinkingAge) {
System.out.println("Here is your JagerBomb");
}
Run the code, and for any value over 18 the Jagerbomb line will be output
Again you may notice another problem: if we were to translate this code into English we would say something like “if the customerAge is over 18, give them a Jagerbomb”. Whilst you or I would understand this correctly and give everyone who is '18 or over' the drink, the computer does not have the ability to understand that someone who is 18 may have a drink. Instead we have to be explicit about it. There are two potential solutions to the problem:
>=
if (customerAge >= drinkingAge) {
System.out.println("Here is your JagerBomb");
}
Update the code, so that the system correctly recognises if the customer's age is 18 or over.
You may have already deduced that there are another couple of operators for checking if a value is less than another value, and indeed there are as follows:
<
: Less than<=
: Less than or equal to
Sometimes we may want to check if something is not equal, for example, we could consider that a vehicle is moving if its speed is not 0. We can check something is not equal using the !=
operator. Example code might appears as follows:
if (vehicleSpeed != 0){
System.out.println("Vehicle is moving");
}
Sometimes we might want to store the result of checking the condition of a variable, as we've seen above, the conditions are either met, or they are not - e.g. they are either true or false. A variable that can only every be true or false is known as a boolean value, we've seen how to declare these directly as either true or false in the Data types page, but we can also declare them to hold the value of the result of an expression. Taking the moving vehicle example above, we could create a Boolean variable to hold the true of false value corresponding to whether the vehicle is moving and then use that variable in the if statement instead (which would be more useful if we were going to use it again, or to make code more readable), here's an example:
boolean vehicleIsMoving = (vehicleSpeed != 0); //will be true or false
if (vehicleIsMoving) {
System.out.println("Vehicle is moving");
}
In the same way we can use the !
symbol to say not equal, we can use it to change a boolean value to its opposite, in the above example, if we wanted to check if the vehicle is not moving, we simply prefix the vehicleIsMoving
variable in the if statement with an explanation mark as follows:
if (!vehicleIsMoving) {
System.out.println("Vehicle is stationary");
}
View a model solution for this task
This task will create a simple application to generate life expectancy
isMale
isMale
variable, and if this value is true, print out a message informing him that he could expect to live to around 79You can also view a model solution for this task