-
Notifications
You must be signed in to change notification settings - Fork 0
Decisions
In Java development, it is important to be able to make decisions and handle choices the user might make. To do this Java has decision structures in place to make it easier to write code to account for different situations.
If statements are the main decision-making structure in Java. It is used a lot and almost always tied to any application that interacts with the user. How If statements works are that you give them a condition to evaluate and if the condition passes or results in a true result then the code within the if statement runs. If the condition does not pass then the if statement does not run.
Example:
if(a == b){
System.out.println("These values are equal");
}
For the example above the two variables, a and b, are compared using a relational operator. Relational operators are used to comparing one variable to another. More relational operators will be discussed later. The relational operator used in the example is used to compare if two variables are equal to each other. If the values are equal to each then the print statement runs and outputs that the value is equal. If the values are not equal then the print statement does not run and any code after the if statement runs. Since if statements are used a lot they also need ways to handle if they don't run. To handle this if statements have alternative statements call if else statements and else statements.
Else if statements are similar to if statements in the sense that they also handle conditions. An else if statement can be used when you have multiple conditions to test. For example, let's say you have to test if a number is higher than another number. If that number is not higher than another number then you want to test if it is lower than a second number. That would look like this:
if(firstNum > secondNum)
{
//statement that runs if true
}
else if(firstNum < secondNum)
{
//statement that runs if true
}
As you can see the else if statement goes right at the end of the if statement and tests a condition. Just like with the if statement the else if statement has a condition that it tests. If that condition is true then the code inside of the body of the statement runs.
Else Statements are statements that run if none of the other statements run. Else statements are different than else if statements because else statements do not have any conditions to evaluate. Else statements run when none of the other statements do. Let's expand on our code above as an exam
if(x > y)
{
statement that runs if true
}
else if(x < z)
{
statement that runs if true
}
else
{
statement that runs the above statements don't run.
}
For the above code, the else statement would only run if the given number is not greater than y and it is not less than z. You also don't need to have else if statements to use else statements, you can use them the same way with if statements.
Relational operators are what drive decisions structures. They are used in the condition sections of decision statements. Below is a table of all relational operators and what they do.
Operator | Meaning | Use |
---|---|---|
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
== | Equal to | X == y |
!= | Not equal to | x != y |
Logical operators can be used in conjunctions with relational operators to test multiple operators at the same time. This can be helpful when you want to test a number range. If you want the test if a number is between 1 and 10 you would use the following statement:
someNum >= 1 && someNum <= 10
For this example, the logical operator using is the AND (&&) operator. This operator tests if both of the conditions are true. Below is a table of the other logical operators.
Operator | Meaning | Explanation |
---|---|---|
&& | AND | Runs if both conditions are true |
|| | OR | Run if either one of the statements is true |
^ | XOR | Runs if one and only one of the statements is true |
| | OR | Same as || buts evaluates both operations first |
& | AND | Same as && but evaluates both operations first |
! | NOT | Runs if that statement on the right is false |
In Java, it is also possible to run if statements inside of other decision statements. This is known as nesting. Nesting is usually when you need to test multiple steps of a decision. For example, if you wanted to test if a number was bigger than multiple numbers:
if(number > otherNumber)
{
if(number > lastNumber)
{
number is bigger than otherNumber and lastNumber
}
}
Let's say for example you want to test multiple cases to give different outputs. You could do this:
if(condition)
{
}
else if (condition)
{
}
else if(conditions)
{
}
else if(condition)
{
}
else
{
}
While this works it is unnecessary and takes to much time for the computer to perform. A better solution is a switch case.
int gradeLevel = 8;
String schoolYear = "";
switch (gradeLevel)
{
case 1: schoolYear = "Freshman";
break;
case 2: schoolYear = "Sophmore";
break;
case 3: schoolYear = "Junior";
break;
case 4: schoolYear = "Senior";
break;
default: month = "Invalid grade level";
break;
}
In the switch case, you can see we take a variable to evaluate, x, and compare it to different cases. Based on which case the variable is equal to then the month string is set because of it. For each case, there is also a line that says break this line tell the program to exit if the case is executed. There is also a default statement that runs if none of the other cases run.
In a switch-case, there is a thing that is known as a fall-through. This happens when multiple cases are executed when the condition of one case is met.
Example int variable = 2;
switch(variable)
{
case 1:
System.out.println("This is case 1");
case 2:
System.out.println("This is case 2");
case 3:
System.out.println("This is case 3");
break;
case 4:
System.out.println("This is case 4");
case 5:
System.out.println("This is case 5");
break;
case 6:
System.out.println("This is case 6");
}
}
**Output:**
This is case 1 This is case 2 This is case 3
In this example, you will notice that the not every case ends in a break statement like it should. This creates a fall-through situation. If the variable that was given match case 1 then the other two cases after it will be executed. This goes for the other cases as well. A fall-through is neither good or bad. It can be a problem that plagues a program or it can be used to perform multiple actions.