课程: Java Essential Training: Syntax and Structure

If statements

- [Educator] Oftentimes programs have to make decisions. So we need to provide different paths of execution within our code. To do this, we use what's called decision structures. The If statement is the most common decision structure in programming. It works as a quick detour within the program. If a certain situation occurs, the program executes a specified block of code and then returns to the main path of execution. Let's look at an example. Let's say we have a team of salespeople, all of whom make a standard rate of US$1,000 a week. For any of them who make more than 10 sales that week, they get an additional bonus of US$250. So the main path of the program would be to pay a salesperson US$1,000 since everyone gets this, no matter what. However, at some point we need to check to see if the condition was met of the salesperson making more than 10 sales. If it's met, then we need to add more to their payment. Here we have a class called SalaryCalculator, and in its main method we have the main path of the program. We initialize variables for salary, bonus, and quota. We've prompted the user to enter the number of sales an employee made for the week. Now here on line 26 is where we want to introduce an If statement that will determine if we should add the bonus to this employee's payment based on the number of sales they made. To program an If statement, you write the word if, which is a reserved word in Java, then you follow if with an open parentheses and a closed parentheses. Inside of the set of parentheses is the condition to evaluate. This condition must be an expression that resolves to a boolean value which is true or false. So our expression will be sales greater than quota. Next, we add a set of curly braces to enclose this as a block of code. Everything inside out of these braces will be executed if the condition sales is greater than quota results to true. If the condition results to false, then the code inside of these braces will not execute. So if the employee made more than 10 sales, we want to add a bonus of US$250 to their salary. Inside of the curly braces we'll update the salary variable with the bonus by saying salary equals whatever is already in salary plus the bonus. A quick note about the curly braces for decision structures: they are meant to encompass zero or more statements. However, if we only have one statement, technically we don't need the curly braces. It would still work without them. Okay. Excellent job. To test our code, let's run the program in debug mode so that we can see it step by step. I'll go ahead and add a break point right here on the If statement. To run in debug mode, we can right-click anywhere within the class and instead of run, we can select debug. When it asks how many sales, let's enter 10, and notice here, the program has halted because we put a break point there and we can see this condition results to false because 10 is not greater than 10. So this will not execute this line of code. If we continue running the program, then notice, the output is US$1,000. Okay, let's run again, but this time we do want to go inside of the If statement. So we'll say 15 this time and notice here, we can see that the condition does result to true, which means that it will go inside of here and add the bonus and we can see that in the result here where the pay is US$1,250. Great job with If statements.

内容