Java Fundamentals

If Statement

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "If Statement" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses the function of an if statement and walks through coding an example if statement. If statements must evaluate to true or false and will apply if the defined condition is met.


Transcript from the "If Statement" Lesson

>> So the next lesson is on decision structures. So oftentimes programs, they have to make some sort of decision, right? So we need to provide different paths of execution within our code. So in this section, we'll look at various types of decision structures, as well as relational and logical operators to help us evaluate conditions.

So the first decision structure we'll look at is the if statement. The if statement says if a certain condition occurs, do something and then go back to the main flow of the program, all right? So let's write another program. This one says that all salespeople get a payment of $1,000 for the week, and anyone who exceeds 10 sales gets an additional bonus of 250, all right?

So let's code this up. Okay, so within our project part, we're going to create a new package, right? Cuz we're talking about a new subject again, package, just typically some sort of logical grouping, okay? So this one, we'll call a decision_structure. Notice I'm starting with a lowercase. Two words, I'm gonna use a underscore.

And in this decision structure, let's create a new class. So we'll call this one SalaryCalculator. Salary begins with a capital S. All right, great. First thing we need to create in here, who remembers?
>> Main.
>> Main method, all right. Another trick, I can type main tab. So within my main method, let's go ahead and initialize the things that we already know.

So we'll declare and initialize some variables. So we can say the salary is 1000, that was given to us in the prompt, right? And then the bonus was 200, also in the prompt. And then the quota for bonus was 10, right? All right, now let's get values for things that we don't know.

So we'll need to do a little bit of input and output. So we'll ask them how many sales, Did the employee get this week? And we're gonna wanna read that in, so we'll use the scanner. Now, notice this is a brand new Java file, so I needed to redefine this stuff, you're not using it across.

Got an error for scanner, what do I need to do?
>> Import.
>> Import, very good, so now I have my import statement up top here. And I'm gonna read from the console, so that System .in. And we're going to store the sales in its own variable, so we'll say int sales = scanner .nextInt.

Okay, and now, so this was the main flow of the code, this is applicable to any employee. The prompt said if a person got more than 10 sales, then we need to add something. So that's where you have to make a decision within your code, and we can do so with the if statement.

So you simply write the word if and a set of parentheses. Inside of the parentheses is a condition. This condition must result in true or false. So you're gonna write some sort of expression that's either gonna be true or false, these are known as Boolean values, all right?

So if sales is greater than quota. That will be either true or that will be false. If it is true, it'll go inside of these curly braces after the if and it'll execute any code that's written inside of here, all right? So we're saying if the sales is greater than the quota, then their salary should be updated to whatever their salary already is plus the bonus, okay?

So that's a quick detour. And then we go back to the main flow of the program, which, again, is applicable to anyone who runs this, all right? So we can just simply print out their salary. We'll append salary there, okay? Shall we run it? Okay, so how many sales did the employee get?

Let's say, and I am big on testing, so we'll check a couple of different options with this. So let's say something lower than 10, maybe 5. Salary is 1,000, that's what we expected. All right, let's run it again, this time we'll say right at that cap of 10.

Great, they also get 1,000. And then we'll run it one more time, this time more than 10, let's say 15. This time they should get 1,200, right? 1,200, so now we know that this works.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now