If/then/else statements and switch statements are two primary ways to make decisions in Java. We’ll talk primarily about if/then statements and leave you to refer to Appendix 5 for information needed to implement switch structures.
If/then/else statements if (logical statement){
If/then statements have the general format
// put code statement(s) here
}
else{
//put more code statement(s) here
}
A logical statement is a statement that is either true or false. Here are some examples:
For example, if we want to determine the maximum of two numbers, we might have code similar to the following.
double biggest=0.0;
if (x>y){//We decide here which one is the max.
biggest=x;
}
else{
biggest=y;
}
You are not required to utilize the else part of the if/then structure. So use it when it makes sense to you!
See Sample Code 5 to check work.