Ready to master loops in programming? Loops are powerful tools... 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
Subjects
105
•
Dec 2, 2025
•
Ella Nadine
@nadenden
Ready to master loops in programming? Loops are powerful tools... Show more











Loops are essential programming structures that allow you to repeat code multiple times. Instead of writing the same instructions over and over, you can create a loop that executes automatically based on conditions you set.
When you're building programs that need to process lists, calculate sums, or repeat actions until certain conditions are met, loops are your go-to solution.
Think of loops like telling someone "keep doing this until I tell you to stop" - they're all about automating repetitive tasks.
💡 Quick Tip: Most programming errors with loops happen because of incorrect stopping conditions. Always double-check what makes your loop start and stop!

Iteration refers to each repetition of a loop. When you need to print numbers 1 through 10, your program will go through 10 iterations to complete the task.
Each time your program goes through the loop body, that's one iteration. For example, if you're printing numbers 1-10, the first iteration prints 1, the second prints 2, and so on.
The number of iterations directly relates to how many times your program needs to repeat a task to solve a problem.

An accumulator is a variable that stores running totals during a process. Think of it like a container that collects and combines values as your loop runs.
When calculating the sum of numbers 1-10, your accumulator starts at 0. Then with each loop iteration, you add the next number to this running total.
By the end of all iterations, your accumulator holds the final result - in this case, 55 (the sum of numbers 1 through 10).
💡 Memory Aid: Think of an accumulator like a snowball rolling downhill - it gets bigger with each roll as it collects more snow!

Let's say we want to find the sum of numbers from 1 to 10. Instead of adding them manually (1+2+3+4+5+6+7+8+9+10=55), we can use a loop.
The loop will add each number to our running total as it counts from 1 to 10. This process requires 10 iterations - once for each number in our sequence.
With each iteration, our accumulator grows: first it's 1, then 3 (1+2), then 6 (3+3), and eventually reaches 55 after all 10 iterations.

When summing numbers from 1 to 10, we need a variable to temporarily hold our running total as we go through the loop. This variable is our accumulator.
The accumulator starts at 0 and increases with each iteration. For example, when adding 1-10, the accumulator would change from 0→1→3→6→10→15→21→28→36→45→55 through the loop's execution.
Accumulators aren't just for addition! They can also track multiplications (like when calculating factorials) or any other calculation where you need to build a result across iterations.

A counter is a variable that tracks how many iterations your loop has completed. It helps control when your loop should stop running.
Counters typically start at a specific value (like 0 or 1) and increase or decrease with each loop iteration. When the counter reaches a certain value, the loop stops.
Counters and accumulators often work together. While the counter tracks which number we're processing (like 1, then 2, then 3...), the accumulator collects the running result (like adding those numbers together).
💡 Pro Tip: Always remember to update your counter inside the loop! Forgetting this step is the most common cause of infinite loops.

An infinite loop is a loop that never ends because its termination condition is never met. These are almost always programming mistakes, not intentional features.
Infinite loops happen when you forget to update variables that control the loop, or when your loop condition is written incorrectly. For example, if you write while (x > 0) but never decrease x inside the loop, it will run forever.
When your program gets stuck in an infinite loop, it will keep running until you manually stop it or your computer crashes from resource exhaustion.

A pre-condition loop (like the while loop) checks its condition before executing any code inside the loop body. If the condition is false from the start, the loop body never runs at all.
This behavior makes while loops perfect for situations where you might not want to run the code at all if certain conditions aren't met. For example, if you're processing user input only when it's valid.
In a while loop, the flow goes: check condition → if true, run code → check condition again → if true, run code again → and so on until the condition becomes false.

A post-condition loop (like the do-while loop) executes its code block first, then checks the condition afterward. This guarantees the loop body runs at least once.
The do-while loop is perfect for situations where you need to execute code at least one time before checking whether to continue. For instance, displaying a menu and getting user input at least once.
While a regular while loop might execute zero times if its condition starts false, a do-while loop always executes at least once because the condition check comes after the first execution.
💡 When to use do-while: Choose this loop when you know you need at least one execution before checking conditions!

Now that we understand the concepts behind loops and their flowcharts, let's see how to write them in actual code.
Different programming languages have slightly different syntax for loops, but the core concepts remain the same: defining a starting point, specifying a condition, and updating values to eventually end the loop.
In the following sections, we'll look at specific implementations of while and do-while loops in Java, with practical examples you can try yourself.
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
Ella Nadine
@nadenden
Ready to master loops in programming? Loops are powerful tools that let you repeat sections of code without rewriting them. In this lesson, we'll explore while loops and do-while loops, understanding how they control program flow and help solve repetitive... Show more

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Loops are essential programming structures that allow you to repeat code multiple times. Instead of writing the same instructions over and over, you can create a loop that executes automatically based on conditions you set.
When you're building programs that need to process lists, calculate sums, or repeat actions until certain conditions are met, loops are your go-to solution.
Think of loops like telling someone "keep doing this until I tell you to stop" - they're all about automating repetitive tasks.
💡 Quick Tip: Most programming errors with loops happen because of incorrect stopping conditions. Always double-check what makes your loop start and stop!

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
Iteration refers to each repetition of a loop. When you need to print numbers 1 through 10, your program will go through 10 iterations to complete the task.
Each time your program goes through the loop body, that's one iteration. For example, if you're printing numbers 1-10, the first iteration prints 1, the second prints 2, and so on.
The number of iterations directly relates to how many times your program needs to repeat a task to solve a problem.

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 variable that stores running totals during a process. Think of it like a container that collects and combines values as your loop runs.
When calculating the sum of numbers 1-10, your accumulator starts at 0. Then with each loop iteration, you add the next number to this running total.
By the end of all iterations, your accumulator holds the final result - in this case, 55 (the sum of numbers 1 through 10).
💡 Memory Aid: Think of an accumulator like a snowball rolling downhill - it gets bigger with each roll as it collects more snow!

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. Instead of adding them manually (1+2+3+4+5+6+7+8+9+10=55), we can use a loop.
The loop will add each number to our running total as it counts from 1 to 10. This process requires 10 iterations - once for each number in our sequence.
With each iteration, our accumulator grows: first it's 1, then 3 (1+2), then 6 (3+3), and eventually reaches 55 after all 10 iterations.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
When summing numbers from 1 to 10, we need a variable to temporarily hold our running total as we go through the loop. This variable is our accumulator.
The accumulator starts at 0 and increases with each iteration. For example, when adding 1-10, the accumulator would change from 0→1→3→6→10→15→21→28→36→45→55 through the loop's execution.
Accumulators aren't just for addition! They can also track multiplications (like when calculating factorials) or any other calculation where you need to build a result across iterations.

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 tracks how many iterations your loop has completed. It helps control when your loop should stop running.
Counters typically start at a specific value (like 0 or 1) and increase or decrease with each loop iteration. When the counter reaches a certain value, the loop stops.
Counters and accumulators often work together. While the counter tracks which number we're processing (like 1, then 2, then 3...), the accumulator collects the running result (like adding those numbers together).
💡 Pro Tip: Always remember to update your counter inside the loop! Forgetting this step is the most common cause of infinite loops.

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 because its termination condition is never met. These are almost always programming mistakes, not intentional features.
Infinite loops happen when you forget to update variables that control the loop, or when your loop condition is written incorrectly. For example, if you write while (x > 0) but never decrease x inside the loop, it will run forever.
When your program gets stuck in an infinite loop, it will keep running until you manually stop it or your computer crashes from resource exhaustion.

Access to all documents
Improve your grades
Join milions of students
By signing up you accept Terms of Service and Privacy Policy
A pre-condition loop (like the while loop) checks its condition before executing any code inside the loop body. If the condition is false from the start, the loop body never runs at all.
This behavior makes while loops perfect for situations where you might not want to run the code at all if certain conditions aren't met. For example, if you're processing user input only when it's valid.
In a while loop, the flow goes: check condition → if true, run code → check condition again → if true, run code again → and so on until the condition becomes false.

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 the do-while loop) executes its code block first, then checks the condition afterward. This guarantees the loop body runs at least once.
The do-while loop is perfect for situations where you need to execute code at least one time before checking whether to continue. For instance, displaying a menu and getting user input at least once.
While a regular while loop might execute zero times if its condition starts false, a do-while loop always executes at least once because the condition check comes after the first execution.
💡 When to use do-while: Choose this loop when you know you need at least one execution before checking conditions!

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 concepts behind loops and their flowcharts, let's see how to write them in actual code.
Different programming languages have slightly different syntax for loops, but the core concepts remain the same: defining a starting point, specifying a condition, and updating values to eventually end the loop.
In the following sections, we'll look at specific implementations of while and do-while loops in Java, with practical examples you can try yourself.
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.
4
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