It would make it easier to have a separate loop control variable, yes.
For what values should your loop control variable start and stop?
It would make it easier to have a separate loop control variable, yes.
For what values should your loop control variable start and stop?
You really only need one loop.
I am not sure, can you show me?
Do you have a while loop that you have written before that uses a increasing or decreasing integer control variable?
I think the n++; is the control variable, but how do I get it to stop the loop?
n
is not a good choice for the control variable in this case, and the above loop doesnât work correctly.
Do you have a while loop that you have written before that uses a increasing or decreasing integer control variable and works?
Can you please just show me what to do? I am really stressed and this loop is due tomorrow.
We donât just give people answers here. I can help you build code, but just giving you the answer does not actually help anyone.
I really donât know what to do.
Do you have an example of a while loop that works in your notes somewhere that we can start with?
No, I donât have an example.
Iâm not certain how this is possible if you are in a class that expects you to write a while loop for a homework assignment.
Anyways,
I found this example by putting âJava while loopâ into a search engine:
Example
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
So, how could we modify this loop to print all numbers from -n
to n
?
I am still not sure.
Have you tried running this code? What values will this loop print?
[https://repl.it/repls/NimbleIndianredKernelmode#Main.java]
It prints out
0
1
2
3
4
Ok. So why does it start at 0? Why does it stop at 4? Can you make it stop at 5 instead?