Reader small image

You're reading from  Learning Java by Building Android Games - Third Edition

Product typeBook
Published inMar 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781800565869
Edition3rd Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

If they come over the bridge, shoot them

As we saw in the previous chapter, operators are used in determining whether and how often a loop should execute the code in its body.

We can now take things a step further. Let's look at putting the most common operator, ==, to use with the Java if and else keywords. Then, we can start to see the powerful yet fine control that they offer us.

We will use if and a few conditional operators, along with a small story, to demonstrate their use. The following diagram illustrates how we can devise conditions to determine whether a block of code should execute:

Figure 7.1 – Determining the execution of a block of code

The following is a made-up military situation that is kind of game-like in nature.

The captain is dying and, knowing that his remaining subordinates are not very experienced, he decides to write a Java program to convey his last orders after he has died. The troops must hold one side of a...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learning Java by Building Android Games - Third Edition
Published in: Mar 2021Publisher: PacktISBN-13: 9781800565869

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton