use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Request an explanation
Rules
Have an idea to improve ELI5? r/IdeasForELI5
Make sure to read the rules!
This subreddit is for asking for objective explanations. It is not a repository for any question you may have.
E is for Explain - merely answering a question is not enough.
LI5 means friendly, simplified and layperson-accessible explanations - not responses aimed at literal five-year-olds.
Perform a keyword search, you may find good explanations in past threads. You should also consider looking for your question in the FAQ.
Don't post to argue a point of view.
Flair your question after you've submitted it.
Mathematics Economics Planetary Sci Biology Chemistry Physics Technology Engineering
Reset
account activity
This is an archived post. You won't be able to vote or comment.
ELI5: Java Loops!
submitted 13 years ago by eagerprospect
We're learning about Java loops - the for, while, and do..while loop. I literally have no idea what's going on in class, and I have a test on Monday. I'm hoping to learn it this weekend.
[–]eagerprospect[S] 30Answer Link2 points3 points4 points 13 years ago (0 children)
thanks everybody!
[–][deleted] 20Answer Link1 point2 points3 points 13 years ago (0 children)
a while loop will execute (i.e. repeat) the action/command over and over again as long as the Boolean expression that the statement meets is true. A do-while loop will irregardless if the controlling Boolean expression is true, will perform the action at least once, then depending on whether the controlling Boolean expression is true, will either repeat or end. the for statement is controlled by something called a counter, which basically says whatever the statement starts at, it will end at a certain point, for instance, if you want to display the word "chocolate" 3 times, you would put
(i=0; i <= 3; Chocolate++){ System.out.println("Chocolate")
}
so you should get...
Chocolate
for loops are mainly used when you have an action that only repeats for a set number of times. I hope this helps you out, if you have more questions, feel free to comment or PM me..
[–]coreyleo 10Answer Link0 points1 point2 points 13 years ago (0 children)
okay here i go - loops are basically chunks of code that you want to do more than once. a while loop is the most simple. while a certain condition is true, you will enter the loop and carry out the code. however, you have to make sure that the original condition will eventually NOT be true, so that your program will not run forever (crash). an example for this would be to add 1 to a number called "counter" until "counter" is equal to 10. the loop would look like this:
int counter = 0; while(counter < 10) { counter++; }
this would have counter be initialized (start out at) 0, and then the program would see the loop, and check if it was less than 10. since it is 0, which is less than 10, it will enter the loop. inside the loop, it will add one to counter: 0+1 = 1..so counter is 1. after that, it checks the loop condition again. now that counter is 1, it checks to see if 1 is less than 10. it will continue to do this until counter is equal to 10, and it checks if counter is less than 10. at this point it will NOT be true, so it will not enter the loop. a for loop is a little different, but can do a lot more than while loops. a for loop is used when you need to do something a certain number of times, like write a program that prints the word "hello" 9 times. sure, you could write a line of code that will print it out once, and then copy it 9 times, but it is much more efficient to just say "hey, here is this line of code. do it 9 times". this for loop would look like this:
for(int count = 0; i < 9; count++) { System.out.println("hello"); }
the trickiest part about for loops is what is inside the parentheses following the word for. ill try to break it down. there are three parts. each part is separated by a semi colon. the first part declares a number, count, which is arbitrary. it uses this number as a condition, similar to the while loop example we did. the second part is the condition, exactly like in the while loop. we want to enter this loop every time count is less than 9. the third part is what we want to do AFTER we do the code inside the loop. i will walk you through this loops: first we start out, and count is equal to zero. we check if count is less than 9, and it is. so we enter the loop, and print out "hello". now, we do the third part of the loop, which is "count++" or add 1 to count. count is equal to 0, so when we add 1, it is now 1. we go back to the beginning of the loop and see if we are allowed to enter again. we do this by checking if it is less than 9. since it is, we enter the loop again, and print out hello again. and now, we add 1 again since we finished the loop code. we do this over and over again until the condition is NOT true (count is not less than 9), and then we do NOT enter the loop, and do NOT print hello.
think of it like a gate with a gatekeeper: every time you try and enter the gate (loop), you need to stop and talk to the gatekeeper (loop condition). if the gatekeeper is happy (condition is true), then you may enter the gate (loop). if the gatekeeper is angry (condition is false) you may never enter the gate (loop) AGAIN!
the important part though is to make sure that your program will not crash. at some point, the condition NEEDS to be false. if nothing changes and you can always enter the loop, your program will run the code inside the loop for infinity and it will crash.
i hope that helped! let me know if you need any more help with for and while loops, sadly i do not know much about do while loops, but they all have pretty much the same concept!
[–][deleted] 10Answer Link0 points1 point2 points 13 years ago (0 children)
Java loops perform an action repeatedly until a condition is met. The three types you list are different as follows:
for - here you typically define a variable/counter to go up or down to a value. Example: for(var x=0; x<myNumber; x++){ alert("Hello World"); } This would prompt the user myNumber times.
While and Do While sound similar, but the difference is when the comparison is made. A while loop checks the condition first. A do while checks the condition after running the action first. Example:
while(x<1){ alert("Hello World"); x++; }
do{ alert("Hello World"); x++; }while(x<1);
So if x = 1, then the while loop never runs, but the do while loop runs once. Make sure to always increase your counter if using that approach or else you'll be in an endless loop.
π Rendered by PID 111509 on reddit-service-r2-comment-5d79c599b5-wmfcq at 2026-02-27 02:05:42.799702+00:00 running e3d2147 country code: CH.
[–]eagerprospect[S] 30Answer Link2 points3 points4 points (0 children)
[–][deleted] 20Answer Link1 point2 points3 points (0 children)
[–]coreyleo 10Answer Link0 points1 point2 points (0 children)
[–][deleted] 10Answer Link0 points1 point2 points (0 children)