Join Peggy Fisher for an in-depth discussion in this video Working with decisions, part of Learning Java.
- When adding decision making logic in Java the programmer often uses relational operators to compare two values. Some relational operators are less than, less than or equal to, greater than, and greater than or equal to. We also have two equal signs, which allows us to compare two values. Remember that a single equal sign represents an assignment, that's when you're setting one value to another value. So we needed a way to say are these two things equal? And we use two equal signs.
And finally, not equal. You'll see in Java, and many programming languages, we use the exclamation point to indicate "not." So in this case, we would read this as not equal. In addition, sometimes it's necessary to join two or more statements. This is done using logical operators. We have the "and" operator, which looks like this, two ampersands. And then we have the "or" operator, which is two pipes. In Java, we use the keyword "if" and determine if it is true or false.
We also have the keyword "else," which provides the statement or statements that are executed if the first condition evaluates to false. So in our example, if the outside temperature is less than 32, then we want to wear a coat. So we're gonna print a message to the user that says, "Wear a coat." Else. The else means that the outside temp was not less than 32 so it has to be 32 or higher. In that case, we'll have system.out.println, "You don't need a coat." The order of a comparison is very important.
Let's take a look at an example that uses the total sale price to determine the shipping amount. As you can see at the top of my program, I have included the shipping rates. If the total cost is over $75, shipping is free. If you spend between $50 and $75, the cost for shipping is $5. If you spend over $25, but less than $50, then you have to pay $10, and finally, anything less than $25 is a $15 shipping fee.
So, let's go ahead and write a program that will allow us to ask the user for the total cost of the sale and evaluate that and add the appropriate shipping rate. Because we want to prompt the user, I've already gone ahead and added import java.util.Scanner. Let's declare our variables. We know we need to hold the total sale, so let's add totalSale. I'm gonna start it out with a value of 0. We haven't done this yet. When you declare a variable, you can set it to an initial value.
Since at this point, the total sale has not been made, I'm gonna set it equal to 0 to start. Now I need to create my scanner object. So Scanner in = new Scanner (System.in). At this point, we need to ask the user to tell us what the total sale amount. When you hear those words "ask the user," think prompt the user with a system.out.println statement. Okay, so the program will stop here, print the message to the console, and wait for the user to enter in the amount.
So when the user enters it in, I want to read it into my totalSale variable. totalSale = in.nextDouble. Now I need to check and see where it falls in my pricing structure. Let's start with the bottom. If the total sale was less than $25, then we're gonna add a $15 shipping fee. So the first thing I want to do is use an "if" statement. The syntax for the "if" statement is if, open parentheses, and inside, I'm gonna indicate my condition, which will evaluate to true or false.
So if(totalSale is < 25) okay so if that value in the parentheses is true, then we need to execute one or more statements. So the way that I indicate the block of code that I want to execute is I use curly brackets. So if that's the case, then I want to add to my total sale. So I'm gonna do totalSale = totalSale + $15 'cause I said there's a $15 shipping fee if the total sale was less than $25.
I'm gonna scroll down a little bit so you can see more of the code. Now, I want to check the next condition. So here, what I want to do is I want to say else. That means that the total sale was at least $25 or more. The next breaking point that I have is $50, so now I can say else if (totalSale < $50) 'cause I know if it was less than $25, I already added the $15, and I will not execute this statement.
This time, I want to add to my total sale, so totalSale = totalSale + $10. Okay. We have one more condition to check for before we get free shipping. Now, we'll say else if (totalSale < $75. Actually <= because if the total sale price is over $75, so we have to do <= $75, and if that's the case, I need to add a $5 shipping charge to my total sale.
Now I've accounted for all of my scenarios because if the total sales was greater than $75, I don't need to add anything to it. At this point, I want to print out my result. So I will say "Total cost for your sale is." I'll use the + sign 'cause I want to add to it the totalSale value. Okay, we have our program. It looks right. We need to test each one of the different scenarios. Let's run the program and try it. Okay, let's start with a total that is less than $25.
So let's say my total is $10. I know that I should get a $15 shipping fee. It looks like that's working 'cause $10 plus $15 gives me $25. If I run it again, and try the next bracket, let's add one for $30. This time, I should get a $10 shipping fee. So far so good. Let's continue and do one between $50 and $75. I'll do $65. It added $5, that's correct, and then finally, I want one that's over $75.
So let's say our total sale was $100. It looks like it's working. We don't have to pay any shipping since our total sale amount was greater than $75. Okay, this was an example of how to use the decision structure using "if" and "else" statements.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs