Java Tutorial/Control Structures I - Decision structures: Wikis

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

Study guide

Up to date as of January 14, 2010

From Wikiversity

Decisions structures are used so that your program doesn't have to do the same thing each time it is executed. They basically answer a "question" on the spot, during execution.

Java codes

The decisions structures need a question to answer. This is done through booleans. A boolean is one of several primitives, which are essentially raw data.

A boolean represents two values - true and false. However, to reach these values, many operators can be used. Operators include (but are not limited to):

Primitives
Operator Name Description Example
&& And operator Compares two booleans. If both are true, the result is true. Otherwise, the result is false. `if(true && false)` would result in `false`
|| Or operator Compares two booleans. If one is true, the result is true. Otherwise, the result is false. `if(false || true)` would result in `true`
== Equality operator Compares two numbers. If they are equivalent, the result is true. Otherwise, the result is false. `if(5 == 5)` would result in `true`
!= Inequality operator Compares two numbers. If they are not equivalent, the result is true. Otherwise, the result is false. `if(5 != 5)` would result in `false`
> Greater than operator Compares two numbers. If the first is greater, the result is true. Otherwise (even if they are equal), the result is false. `if(5 > 5)` would result in `false`
>= Greater than or equals operator Compares two numbers. If the first is greater or equivalent to the second, the result is true. Otherwise, the result is false. `if(6 >= 5)` would result in `true`
< Less than operator Compares two numbers. If the first is less, the result is true. Otherwise (even if they are equal), the result is false. `if(4 < 5)` would result in `true`
<= Less than or equals operator Compares two numbers. If the first is less or equivalent to the second, the result is true. Otherwise, the result is false. `if(5 <= 5)` would result in `true`

These operators can be used together. You can use parentheses with these operators as well, to produce different booleans.

```5 == 4 || 3 != 4
```

would result in true

```(9 > 7 && 3 == 3) || false
```

would result in true, as the part within the parentheses is true

```(5 <= 5 || 7 != 7) && (true && false)
```

would result in false as the second part is false

Prepared BY: Chronicle Jerome

Statements

There are several statements that are used with the above booleans.

The `if` statement

The if statement is used by itself. The structure is:

```if(boolean) {
then...
}
```

For example, if you wanted to say something like, "if 5 is not equal to 6, then print out 'not equal'":

```if(5 != 6) {
System.out.println("not equal");
}
```

If you get an error when using an if statement, then make sure that you do not have a semicolon after it:

```if (5 != 6); {
System.out.println("not equal"); //would always be executed, as this is just a code block without the if statement (the if statement does not have a "body", and can only be used for executing other methods)
}
```

The `if, else if` statement

You can use the if statement with the else if and else statements. The structure is:

```if(boolean) {
then...
} else if(boolean2) {
then...
}
```

It means, "if boolean is true, then do ... if not and boolean2 is true, then do ...". For example, in order to write out "If 5 is equal to 6, then write 'equal' to the console. If not and 7 is equal to 7, write '7=7' to the console":

```if(5 == 6) {
System.out.println("equal");
} else if(7 == 7) {
System.out.println("7=7");
}
```

Only "7=7" will be printed. However, if we replaced the first boolean with `5==5`, only "equal" would be printed.

The ```if, else if, else``` statement

You can also add in the else statement. Note that you do NOT have to have an else if statement with the else statement. The structure is:

```if(boolean) {
then...
} else if(boolean2) { //optional
then...           //optional
} else {
then...
}
```

It means, "if boolean is true, then do ... else if boolean2 is true, do ... if both of the previous were false, then do ...". For example, to write out "if 5 equals 5, print '5=5'; if not, print '5!=5'":

```if(5 == 5) {
System.out.println("5=5");
} else {
System.out.println("5!=5);
}
```

Only "5=5" would be printed.

Previous: Up: Next: Variables Java Tutorial Control Structures II - Looping