Using conditional statements

This tutorial will build on what was learnt in the ‘Introduction to conditional statements’ video (in the previous step) by discussing Boolean logic and how it is used in conditional statements.

Statements in a program are generally executed in order, from top to bottom. However, there are special statements called ‘control flow statements’, such as the ‘if’ statement you have already learnt. Control flow statements allow decision making, looping, and/or branching, which can change the sequence of program execution.

Let’s look back at what was learnt about ‘if’ statements in the last step.

Line 1: int myValue = 5;
Line 2: if (myValue > 0) {
Line 3:		myValue = 0;
Line 4: } 
Line 5: else {
Line 6:		myValue = myValue +1;
Line 7:} 

In the example above, if the myValue variable has a value greater than 0, this will cause myValue to be reset to 0.

How does this happen?

Line 2 checks whether myValue is greater than 0.

If this condition is true (myValue is greater than 0), execution goes to Line 3 where myValue is assigned 0.

If the condition is false (myValue is not greater than 0; in other words myValue is less than or equal to 0), execution moves to Line 5, the ‘else’ part of the if statement. Then in Line 6 it increases myValue by one.

Notice the curly brackets ({}) used in this code. Do you know what they are used for?

Curly brackets are used to group statements. If there are multiple tasks to perform when myValue is greater than 0, these tasks can all be grouped together by placing them between the opening and closing curly brackets.

Similarly, if there are a number of things to be done in the ‘else’ section, all those could be grouped between the curly brackets under the else part of the statement.

How to combine conditions:

Here is an example of a situation that can be resolved by using a conditional statement:

If I am thirsty AND it is cold I will drink a hot chocolate otherwise I will drink water

Here, I will drink hot chocolate only if both of the following conditions are satisfied (true):

  1. I am thirsty
  2. it is cold.

This is a conditional statement that operates only if the two conditions are satisfied.

In Java, the AND operator (&&) can be used to represent this situation programmatically:

Line 1: boolean thirsty = true;
Line 2: boolean cold = true;
Line 3: String  toDo = "nothing";
Line 4: if (thirsty && cold) {
Line 5:		toDo = "Drink hot chocolate";
Line 6: } 
Line 7: else {
Line 8:		toDo = "Drink water";
Line 9:} 

But what if it was an OR condition?

For example:

If I am thirsty OR it is cold I will drink a hot chocolate otherwise I will drink water

Let’s think about all of the possible outcomes for this scenario.

Here the combined condition is satisfied (true) if at least one of the other conditions is true.

In Java, the operator OR (||) can be used to represent this situation programmatically:

Line 1: boolean thirsty = true;
Line 2: boolean cold = true;
Line 3: String  toDo = "nothing";
Line 4: if (thirsty || cold) {
Line 5:		toDo = "Drink hot chocolate";
Line 6: } 
Line 7: else {
Line 8:		toDo = "Drink water";
Line 9:} 

Another useful logical operator is NOT (!). This inverts a value of a boolean. For example:

Line 1: boolean thirsty = true;
Line 2: String  toDo = "nothing";
Line 3: if (!thirsty) {
Line 4:		toDo = "Don’t drink anything";
Line 5: } 
Line 6: else {
Line 7:		toDo = "Drink something";
Line 8: } 

The condition on line 3 is true when the value of thirsty is NOT true, or rather, is false. We can therefore read this condition as ‘if I am not thirsty’.

Because the variable thirsty holds the value of ‘true’, when you invert its value with the use of NOT operator (!), it becomes ‘false’.

Therefore, once this code has executed the value, the variable toDo will be "Drink something" that is, the execution goes to the else part of the if statement.

In this article we have:

  • revisited the ‘if’ statement
  • covered how logical operators (OR, NOT, AND) can be used in code
  • and illustrated how to use if statements in a programme.

Well done!

Share this article:

This article is from the free online course:

Begin Programming: Build Your First Mobile Game

University of Reading