Java Tutorial/Control Structures I - Decision structures
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.
Booleans
editThe 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. They can be assigned directly, but are usually constructed from a set of many operators. These operators include (but are not limited to):
Primitives | |||
---|---|---|---|
Operator | Name | Description | Example |
== | 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
|
&& | 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
|
! | Not operator | Inverts a boolean - true becomes false, and false becomes true. | if !(2 > 5) returns 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
editThere are several statements that are used with the above booleans.
The if
statement
edit
The if statement is used by itself. The structure is:
if(boolean) {
//Java code...
}
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
edit
You can use the if statement with the else if and else statements. The structure is:
if(boolean) {
//Java code...
} else if(boolean2) {
//Java code...
}
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
edit
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) {
//Java code...
} else if(boolean2) { //optional
//Java code... //optional
} else {
//Java code...
}
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: | Variables | Up: | Java Tutorial | Next: | Control Structures II - Looping |
---|