Help in loops in c++
Go to solution
Solved by LukeTim,
Hey People! So long time ago I missed a CS Class in which loops were taught. I worked around with the while and the for loops. But I didn't try to read DO WHILE LOOP, which costed me 4 points in my today's exam. So, I just wanna have a small discussion over the working of it.
A small example (a small c++ program ) would help!
Thanks!
The do while loop works the same as a while loop, only it is guaranteed to run at least once. In other words, the condition is checked after the loop has run, not before.
This is clear in the way it is written (in C/C++ at least):
i = 0;do { i++;} while(i < 5)
The statement(s) to be executed in the loop come before the conditional.
To give an example of how while and do while can differ:
i = 2while(i < 2) //will not enter loop{ i++;}//i == 2do { i++;} while(i < 2) //runs once, then terminates//i == 3

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now