![Java Fundamentals](https://static.frontendmasters.com/assets/courses/2023-05-09-java/thumb.webp)
Check out a free preview of the full Java Fundamentals course
The "For Loop" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
Angie walks through creating a program that will scan a given number of items and keep a running total. A for loop will run a specified number of times and the condition is tested before entering the loop.
Transcript from the "For Loop" Lesson
[00:00:00]
>> So now we'll get to your question. We're gonna write another program about a cashier, and this will scan a number of items and tally the cost, okay? So let's go back to our IDE. We'll write a new class called Cashier. Inside of Cashier let's go ahead and make the main method, And we'll ask them for, The number of items.
[00:00:33]
Enter the number of items you would like to scan. And then we'll import and use our scanner, And we will store the quantity in an integer variable. Okay, now I'm going to declare a variable outside of the loop, and that variable is gonna be updated while the loop runs.
[00:01:17]
So we'll say double total, we can set it to 0 to begin with. And now we're gonna make a loop that's going to run the number of times that was specified, and that sort of loop is a for loop. So we can say for, and inside a parentheses the for loop takes three small statements.
[00:01:40]
The first statement needs to initialize a counter variable basically, this is keeping track. It's an incrementor, if you will, keeping track of how many times the loop has run. We say int, it's convention to call this i for incrementor. Int i equals and you set it equal to some initial number.
[00:02:03]
Usually for loops, it's set at 0, starts at 0. It's 0 cuz it hasn't run any times yet, right? After it's run, that would be updated to 1, meaning it's run one time, all right? int i = 0, the next little small statement is the condition for running this loop.
[00:02:24]
When do you want this loop to end? I would like it to keep running as long as i is less than the quantity. When i gets equal to the quantity, that means we've run this loop the number of times they wanted us to run it, so we should stop.
[00:02:47]
And then the last little mini statement says, how do you want i to change every time that this loop is run? I want 1 to be added to i. The shortcut for this is i++. i++ is equivalent to saying i = i + 1, right? Shorthand, i++. And now, inside of this scoop we'll just run whatever we need to, right?
[00:03:22]
So inside of here we'll ask them to enter the cost of a specific item. So this is gonna handle each item at a time. So we'll say, enter the cost of the item. Okay, and we'll read that in and we'll set that to price. This will be a new variable.
[00:03:50]
Set price, double price equals scanner.nextDouble. And now, we're going to add that to that total variable. So I wanna say total equals whatever is already in total, plus the price that we just got. And that way you accumulate this. It was key that I defined that variable total outside of the loop so that it's not overwritten to 0 every time this loop is run.
[00:04:23]
All right, once we're outside of the loop, let's go ahead and close our scanner, and print out the total. All right. How many items we're scanning? Someone give me a number, I see three.
>> Three, yeah.
>> That's a good one, right? All right, first item is five books, the second item is two.
[00:05:00]
The third item is that 7, let's say 8 for 15. So your total is 15. Questions on the for loop?
>> It seems like that's a lot of boilerplate for writing that for loop. Are there any easier ways or more shorthand ways?
>> There are additional types of for loops that we'll look into in this course, but they are more for when you're dealing with objects.
[00:05:34]
Since this is like, we don't have an object that we're looping through or anything like that. This is your best bet. All right? Okay, so some things to remember about for loops. For loops are count controlled, they're not condition controlled. So, these run a specific number of times, right?
[00:06:05]
The person told us, they have this quantity of items, and that's how many times the loop is run, all right? Remember, when we wrote that code, I didn't have to say it. Yes, I wanna run again or anything like that, it kept a count. They told us three, we said run this that many times.
[00:06:25]
After the three times, boom, it was done. It also tests the condition that we specify before the loop is run again. And then it's best to use this when you know how many times. Like you know this needs to run a specific number of times, not necessarily infinite number of times, okay?
[00:06:48]
You can nest your loops just like you can nest your decision structures. So sometimes our tasks have subtasks and those are also repetitive. And you can nest any of these types of loops together so you could have a while with a for loop inside of it or whatever.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops