If the question says... "user can enter input up to five times" - does that mean you use a "for loop" since you know the # of times? or do you just use a "while loop" please help. thanks.

Recommended Answers

All 8 Replies

A loop's a loop. It doesn't matter how you do it. It's entirely equivalent. No matter which you choose all should produce the same five-time iteration that you want. I'd use for but a while would work too.

Use whichever makes more sense. In this case it's a counted loop, which is what the for loop syntax was designed to handle.

You can use any loop, be it for-loop, or while loop, or do-while loop. For the latter two, you just need to increment the counter within the loop and check for it at the condition place.

my problem has to do with - if you enter a number (10) it will output the word (ten) and so on... so I guess like you all said. any loop would work.

i think it means that you keep a counter to how many times a user has entered a number and if it exceeds 5 you stop.
So you can to it better with a do while loop because you can specify the exit conditions clearly.

do you know the synthax for a do loop???

It's do-while.

do {
 // code here
} while( condition );

There's no point in asking "which loop do I HAVE to use."

Every loop does the same thing. You can try it yourself: write something with for-loop, then try to write same thing with other two loops.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.