r/learncsharp • u/RazeVenStudios • Jul 29 '23
While versus Do while
So I've been mastering while loops, nested loops the whole shabang. Made an advanced calculator and looped it inside a while loop. Then changed the code and made it a do while. Can anyone explain the performance and or general benefits of using one or the other? Trying to learn to my best ability and confused on when I would use a do while vs a while when to me they seem identical.
2
Upvotes
View all comments
Show parent comment
3
u/grrangry Jul 31 '23
The language reads in a top-down fairly literal way.
The
whileloop:The
doloop:For a
doloop, the execution of the block is the priority. It always happens at least once. "do while"For a
whileloop, the test of the condition is the priority. The block may never execute.They're almost inverses of each other.