Skip to content

IfThenElse

Chris Ward edited this page Oct 7, 2022 · 2 revisions

We did if checks in python. That is how we decided if a button was pressed, or a touch pad was touched. The concept of the if is the same. It looks like this:

package examples.c_ifthen;

public class SimpleIfThen {
    public static void main(String[] args) throws Exception {
        boolean aValue = true;

        if (aValue == true) {
            System.out.println("It's true!");
        } else {
            System.out.println("It's false");
        }
}

Here we start by setting a boolean to true. We don't need the capital T like in python. Do you know what this will do? It is going to print either "It's true" or "It's false".

Can you change it to make it say "It's false" instead?

How about if you used a number instead of a boolean? Can you change the println to print out if the number is 0, or 1 or 2? That would be three things, not just a true/false. The example code has the answer if you get stuck. Take a look at how the example handled multiple if statement and see if you did it the same way.

There is another way to make a decision if there are multiple paths, or branches. Lets look at switch case.

Clone this wiki locally