do loop

how can i make this do loop into a while or for loop?

1
2
3
4
5
6
do{
        cout << "How likely is the virus to spread to a neighbor (0-1) ? ";
        cin >>  probability;
        if(probability< 0 || probability > 1)
            cout << "Incorect probability entered" << endl;
    }while(probability < 0 || probability > 1);



i was thinking this but i wasn't sure

1
2
3
4
5
6
7
cout << "How likely is the virus to spread to a neighbor (0-1)? ";
cin >> probability;
while (probability < 0 || probability > 1)
{
    cout << "incorrect probability entered" << endl;
    cin >> probability;
}


if i did with a while loop would my code after that continue on the same way as if it would with a do loop. this is out of curiosity since i know the do loop works in my code. thanks for the help...
do..while loop will always execute at least once, because it test the condition after the loop is executed.
while loop test the condition before the block is executed, that's why it may not execute even once, if the condition is false.
Last edited on
but is my syntax correct for the while loop that i have above? i'm not on my machine with my compiler right now to check. also, if my while loop is in main and the condition is false, will it continue on with the main function the way i have it written?
it looks OK at first sight (actually you don't need to duplicate cin >> probability, however compiler should return a warning concerning unitialized variable)
Last edited on
Topic archived. No new replies allowed.