Jump to content

C# While Loop

Go to solution Solved by JesperEC,

The "do { CODE } while (CONDITION);" loop runs the contents of the loop once first, before checking the condition. To have it check the condition (after "while") the first time around too, use a "while (CONDITION) { CODE }" loop instead. 

Hey everyone. I've got a quick question about while loops, and can't figure out exactly what's happening. I'm programming in Visual Studio 2017, and need to program a while loop to have a user reenter a response if the original response is not the right data type. I've included a screenshot of what I've got so far, which from everything I've found so far says is correct. 

Now whenever I actually run the cmd program, it'll require me to enter the same thing twice for the program to take it. 

Any ideas?

image.png.2c75e3bb15c60aad8a98c0477a502a9a.png

image.png.1507f0d53f975f6270fe2d2ae6561284.png

Link to comment
https://linustechtips.com/topic/902169-c-while-loop/
Share on other sites

Link to post
Share on other sites

The "do { CODE } while (CONDITION);" loop runs the contents of the loop once first, before checking the condition. To have it check the condition (after "while") the first time around too, use a "while (CONDITION) { CODE }" loop instead. 

Link to comment
https://linustechtips.com/topic/902169-c-while-loop/#findComment-11102589
Share on other sites

Link to post
Share on other sites

7 minutes ago, JesperEC said:

The "do { CODE } while (CONDITION);" loop runs the contents of the loop once first, before checking the condition. To have it check the condition (after "while") the first time around too, use a "while (CONDITION) { CODE }" loop instead. 

+1 to this. There is a different between a "while" loop and a "do while" loop. The "do while" loop completes the code once before checking the condition, whereas the "while" doesn't.

I edit my posts a lot.

Link to comment
https://linustechtips.com/topic/902169-c-while-loop/#findComment-11102602
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

@JesperEC and @MrDrWho13 said it. And in addition to that the do while loop is exit control loop and the while loop is entry control loop. Exit control loop check the test condition after executing the body and at the end. Entry control loop checks the test condition at the beginning of the body before entering the body. 

 

Link to comment
https://linustechtips.com/topic/902169-c-while-loop/#findComment-11130911
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×