r/javahelp • u/ethraine • Oct 23 '19
Can you put a while loop inside a while loop?
This is probably a stupid question, but I'm in desperate need of help. So I'm taking an online Computer Science course, and am writing a program that is giving me a lot of trouble. Towards the beginning of the program, I have a scanner input, but when I run it with a while loop inside of a while loop, it keeps requesting input. Can I not do that?
18
u/desrtfx Out of Coffee error - System halted Oct 23 '19
Towards the beginning of the program, I have a scanner input, but when I run it with a while loop inside of a while loop, it keeps requesting input.
That means that your code, your idea, conception of the program is wrong. You need to step back and rethink your approach. Play your code through on paper and you should see where you went wrong.
You can nest any type of loop plus conditionals more or less as deep as you want. (There is a theoretical maximum, but trust me, you will never reach it.)
5
1
10
8
u/ChuvaChooChoo08 Oct 23 '19
Yes.
2
2
7
u/MHajoha Oct 23 '19
Sure you can. Try to have as few nested loops in a function as possible though, lest your code become more unreadable than my handwriting after walking in the cold without gloves on for an hour.
3
u/BertyLohan Oct 23 '19
It sounds like the program is looping correctly but you don't want it to. Show us the code you've written and what you want it to do.
1
1
u/msx Oct 24 '19
Yo dawg, i heard you like loops, so i put a loop in your loop so you can loop while you loop
1
1
u/ZeroGainZ Oct 24 '19
Computers do exactly what you tell them to do, so, if you ask to read input within a while loop, the computer will try to read the input repeatedly, until the loop ends.
For example:
int counter = 5;
while( counter > 0 ){
System.out.println( "Hello!" );
counter = counter - 1;
}The above would print "Hello" five times. So reading is similar, it will keep executing the body of the loop until the loop conditional is false.
Hope that helps.
1
u/elronnoco Oct 26 '19
Yes you can, they just control flow and they can be nested indefinitely - subject to language specific restrictions etc.
It’s important to remember that a while loop will not even perform one iteration if its condition is not met when it is first encountered.
If your program isn’t working you are best dry running it in your mind as though you are the machine running it. Evaluate each while condition as you encounter it and think “will this be true at this point in time”.
Shouldn’t be too long til you find the problem unless something quirky is going on.
0
0
u/JimmieDeep Oct 24 '19
Hi,
While statements execute the code while a condition is met.
Ex: I assume your first while satisfies the condition, he jumps to the next one, and it is a Scanner for user input. While the second condition is true it is always going to execute that block of code it holds. To make valid program, give it a certain if statement that checks out ( like the name "Oscar" ). Everytime user input some type value ( our case String ), it checks if it is "Oscar". If it is it jumps to the block of code under the if statement, this way you can validate it. Make output so you know it is working, also include output before scanner takes input for easier debugging ;)
To compare a string you can use the .equalsTo("") method.
Basic ex: String name; if (name.equalsTo("Oscar") {
// Code here
}
Always be aware as this method is case sensitive! If the user inputs "oscar" this stops working. Combining the logical operators || (or) this can be avoided.
if(name.equalsTo("Oscar") || name.equalsTo("oscar") {
// Code here
}
Hope it helps you anyhow. Always think of conditions when working with loops, you can use other commands like break; jump; continue; Useful sites to study: Zetcode, w3schools, hackerrank.
Best regards, Jim.
50
u/[deleted] Oct 23 '19
You can nest as many loops as you want.