We help IT Professionals succeed at work.

Java Loop

antmax00
antmax00 asked
on
Hi,

How do i setup a while loop to loop depending on user input?  

e.g. If a user inputs int 1, then it loops once. If the user inputs int 2, then it loops twice.


Thanks
Comment
Watch Question

Full-stack Java Developer
Commented:
int userInput;
int iterator = userInput;
while (iterator > 0) {

// do something you wanna do;
iterator--;
}
Big MontyWeb Ninja at large

Commented:
Radek has got the right answer if you're looking to do this in java, however you listed javascript as one of your topics, and that's completely different from java.

your code would look something like:

var counter = 0;
var numLoops = 5
while ( counter < numLoops ) {
     //-- execute your code
    counter++;
}

Open in new window

Author

Commented:
Thanks.
This works