Loops are essential programming structures that let you repeat code... Show more
Sign up to see the contentIt's free!
Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
94
•
Dec 7, 2025
•
Samantha Montez
@samanthamontez_kjgf
Loops are essential programming structures that let you repeat code... Show more











Loops are programming structures that help you execute the same code multiple times. Instead of writing the same lines of code over and over, loops let your program repeat actions automatically.
Think of loops like putting your music player on repeat - the same song plays again and again until you tell it to stop.
In this section, we'll explore the two main types of loops: while loops and do-while loops, which are essential tools for any programmer.

When you use a loop, each complete run through the loop is called an iteration. Think of it as one complete cycle through the loop.
For example, if you want to print numbers from 1 to 10, your loop will go through 10 iterations - once for each number you need to print. The loop repeats the print instruction 10 separate times, rather than you having to write 10 separate lines of code.
💡 Iterations are like laps around a track - each time you complete the full circuit, that's one iteration!

An accumulator is a special variable that collects or builds up a value during a loop. It's like a running total that changes with each iteration.
For example, instead of just printing numbers 1 through 10, you might want to calculate their sum (1+2+3...+10). The accumulator stores this running total as your loop progresses.
Think of an accumulator like a snowball rolling down a hill - it gets bigger as it picks up more snow along the way.

Let's say we want to find the sum of numbers from 1 to 10. The final answer should be 55 (1+2+3+4+5+6+7+8+9+10).
To solve this with a loop, we need to add each number as we go through the sequence. Our loop will run 10 times (10 iterations), adding each number to our running total.
This is much more efficient than writing out the entire calculation by hand, especially if we needed to sum hundreds or thousands of numbers!
💡 This pattern of accumulating values is super common in programming - you'll use it for everything from calculating averages to building strings!

When calculating a sum, our accumulator starts at zero and grows with each loop iteration. As the loop counts from 1 to 10, we add each number to our accumulator.
For example:
Accumulators aren't just for addition! They can be used for multiplication (like calculating factorials) or even building strings character by character.

A counter is a variable that keeps track of how many iterations your loop has completed. It's like the lap counter at a race track.
Counters typically start at a specific value (like 0 or 1) and increase or decrease with each loop iteration. They serve two important purposes:
For example, if you're adding numbers 1-10, your counter might start at 1 and increase until it reaches 10, with each value being added to your accumulator along the way.
💡 Counter variables are often named with single letters like i, j, or k by convention, though descriptive names like "count" work too!

An infinite loop is a loop that never ends - it just keeps running forever. This usually happens because of a mistake in your code.
Infinite loops occur when the condition that's supposed to eventually stop the loop never becomes false. For example:
while (true) {
System.out.println("This will never end!");
}
These loops can crash your program or make it unresponsive. If you accidentally create one while coding, you'll usually need to force-quit your program.
💡 Be careful with your loop conditions! Always make sure there's a way for your loop to eventually end.

Pre-condition loops like WHILE and FOR check whether they should run before executing any code. They follow this pattern:
This means a while loop might execute zero times if the condition is false from the start.
while (condition) {
// Loop body - only runs if condition is true
}
Pre-condition loops are great when you might not need to run the loop at all depending on the initial conditions.

A post-condition loop (like DO-WHILE) runs its code block first, then checks the condition to see if it should run again. This guarantees the loop body runs at least once.
do {
// Loop body - always runs at least once
} while (condition);
The key difference from pre-condition loops is timing - do-while executes its code first, then checks if it should continue.
This type of loop is perfect when you need to ensure your code runs at least once, like when validating user input or processing at least one item in a collection.
💡 Remember: while loops might run zero times, but do-while loops always run at least once!

Now that we understand the theory behind loops, let's see how to implement them in actual programs.
In programming languages like Java, loops have specific syntax and rules. We'll focus on the practical aspects of creating while and do-while loops in code.
The examples that follow will demonstrate how to translate loop concepts into working programs that solve real problems. Pay close attention to how we set up the loop conditions and how we modify our counter variables within the loops.
Our AI companion is specifically built for the needs of students. Based on the millions of content pieces we have on the platform we can provide truly meaningful and relevant answers to students. But its not only about answers, the companion is even more about guiding students through their daily learning challenges, with personalised study plans, quizzes or content pieces in the chat and 100% personalisation based on the students skills and developments.
You can download the app in the Google Play Store and in the Apple App Store.
That's right! Enjoy free access to study content, connect with fellow students, and get instant help – all at your fingertips.
App Store
Google Play
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user
Samantha Montez
@samanthamontez_kjgf
Loops are essential programming structures that let you repeat code sections without writing them multiple times. They save time and make your code more efficient by automating repetitive tasks through different types of structures like while and do-while loops.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Loops are programming structures that help you execute the same code multiple times. Instead of writing the same lines of code over and over, loops let your program repeat actions automatically.
Think of loops like putting your music player on repeat - the same song plays again and again until you tell it to stop.
In this section, we'll explore the two main types of loops: while loops and do-while loops, which are essential tools for any programmer.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
When you use a loop, each complete run through the loop is called an iteration. Think of it as one complete cycle through the loop.
For example, if you want to print numbers from 1 to 10, your loop will go through 10 iterations - once for each number you need to print. The loop repeats the print instruction 10 separate times, rather than you having to write 10 separate lines of code.
💡 Iterations are like laps around a track - each time you complete the full circuit, that's one iteration!

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
An accumulator is a special variable that collects or builds up a value during a loop. It's like a running total that changes with each iteration.
For example, instead of just printing numbers 1 through 10, you might want to calculate their sum (1+2+3...+10). The accumulator stores this running total as your loop progresses.
Think of an accumulator like a snowball rolling down a hill - it gets bigger as it picks up more snow along the way.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Let's say we want to find the sum of numbers from 1 to 10. The final answer should be 55 (1+2+3+4+5+6+7+8+9+10).
To solve this with a loop, we need to add each number as we go through the sequence. Our loop will run 10 times (10 iterations), adding each number to our running total.
This is much more efficient than writing out the entire calculation by hand, especially if we needed to sum hundreds or thousands of numbers!
💡 This pattern of accumulating values is super common in programming - you'll use it for everything from calculating averages to building strings!

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
When calculating a sum, our accumulator starts at zero and grows with each loop iteration. As the loop counts from 1 to 10, we add each number to our accumulator.
For example:
Accumulators aren't just for addition! They can be used for multiplication (like calculating factorials) or even building strings character by character.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
A counter is a variable that keeps track of how many iterations your loop has completed. It's like the lap counter at a race track.
Counters typically start at a specific value (like 0 or 1) and increase or decrease with each loop iteration. They serve two important purposes:
For example, if you're adding numbers 1-10, your counter might start at 1 and increase until it reaches 10, with each value being added to your accumulator along the way.
💡 Counter variables are often named with single letters like i, j, or k by convention, though descriptive names like "count" work too!

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
An infinite loop is a loop that never ends - it just keeps running forever. This usually happens because of a mistake in your code.
Infinite loops occur when the condition that's supposed to eventually stop the loop never becomes false. For example:
while (true) {
System.out.println("This will never end!");
}
These loops can crash your program or make it unresponsive. If you accidentally create one while coding, you'll usually need to force-quit your program.
💡 Be careful with your loop conditions! Always make sure there's a way for your loop to eventually end.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Pre-condition loops like WHILE and FOR check whether they should run before executing any code. They follow this pattern:
This means a while loop might execute zero times if the condition is false from the start.
while (condition) {
// Loop body - only runs if condition is true
}
Pre-condition loops are great when you might not need to run the loop at all depending on the initial conditions.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
A post-condition loop (like DO-WHILE) runs its code block first, then checks the condition to see if it should run again. This guarantees the loop body runs at least once.
do {
// Loop body - always runs at least once
} while (condition);
The key difference from pre-condition loops is timing - do-while executes its code first, then checks if it should continue.
This type of loop is perfect when you need to ensure your code runs at least once, like when validating user input or processing at least one item in a collection.
💡 Remember: while loops might run zero times, but do-while loops always run at least once!

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Now that we understand the theory behind loops, let's see how to implement them in actual programs.
In programming languages like Java, loops have specific syntax and rules. We'll focus on the practical aspects of creating while and do-while loops in code.
The examples that follow will demonstrate how to translate loop concepts into working programs that solve real problems. Pay close attention to how we set up the loop conditions and how we modify our counter variables within the loops.
Our AI companion is specifically built for the needs of students. Based on the millions of content pieces we have on the platform we can provide truly meaningful and relevant answers to students. But its not only about answers, the companion is even more about guiding students through their daily learning challenges, with personalised study plans, quizzes or content pieces in the chat and 100% personalisation based on the students skills and developments.
You can download the app in the Google Play Store and in the Apple App Store.
That's right! Enjoy free access to study content, connect with fellow students, and get instant help – all at your fingertips.
2
Smart Tools NEW
Transform this note into: ✓ 50+ Practice Questions ✓ Interactive Flashcards ✓ Full Mock Exam ✓ Essay Outlines
App Store
Google Play
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user
The app is very easy to use and well designed. I have found everything I was looking for so far and have been able to learn a lot from the presentations! I will definitely use the app for a class assignment! And of course it also helps a lot as an inspiration.
Stefan S
iOS user
This app is really great. There are so many study notes and help [...]. My problem subject is French, for example, and the app has so many options for help. Thanks to this app, I have improved my French. I would recommend it to anyone.
Samantha Klich
Android user
Wow, I am really amazed. I just tried the app because I've seen it advertised many times and was absolutely stunned. This app is THE HELP you want for school and above all, it offers so many things, such as workouts and fact sheets, which have been VERY helpful to me personally.
Anna
iOS user
I think it’s very much worth it and you’ll end up using it a lot once you get the hang of it and even after looking at others notes you can still ask your Artificial intelligence buddy the question and ask to simplify it if you still don’t get it!!! In the end I think it’s worth it 😊👍 ⚠️Also DID I MENTION ITS FREEE YOU DON’T HAVE TO PAY FOR ANYTHING AND STILL GET YOUR GRADES IN PERFECTLY❗️❗️⚠️
Thomas R
iOS user
Knowunity is the BEST app I’ve used in a minute. This is not an ai review or anything this is genuinely coming from a 7th grade student (I know 2011 im young) but dude this app is a 10/10 i have maintained a 3.8 gpa and have plenty of time for gaming. I love it and my mom is just happy I got good grades
Brad T
Android user
Not only did it help me find the answer but it also showed me alternative ways to solve it. I was horrible in math and science but now I have an a in both subjects. Thanks for the help🤍🤍
David K
iOS user
The app's just great! All I have to do is enter the topic in the search bar and I get the response real fast. I don't have to watch 10 YouTube videos to understand something, so I'm saving my time. Highly recommended!
Sudenaz Ocak
Android user
In school I was really bad at maths but thanks to the app, I am doing better now. I am so grateful that you made the app.
Greenlight Bonnie
Android user
I found this app a couple years ago and it has only gotten better since then. I really love it because it can help with written questions and photo questions. Also, it can find study guides that other people have made as well as flashcard sets and practice tests. The free version is also amazing for students who might not be able to afford it. Would 100% recommend
Aubrey
iOS user
Best app if you're in Highschool or Junior high. I have been using this app for 2 school years and it's the best, it's good if you don't have anyone to help you with school work.😋🩷🎀
Marco B
iOS user
THE QUIZES AND FLASHCARDS ARE SO USEFUL AND I LOVE THE SCHOOLGPT. IT ALSO IS LITREALLY LIKE CHATGPT BUT SMARTER!! HELPED ME WITH MY MASCARA PROBLEMS TOO!! AS WELL AS MY REAL SUBJECTS ! DUHHH 😍😁😲🤑💗✨🎀😮
Elisha
iOS user
This app is phenomenal down to the correct info and the various topics you can study! I greatly recommend it for people who struggle with procrastination and those who need homework help. It has been perfectly accurate for world 1 history as far as I’ve seen! Geometry too!
Paul T
iOS user