What situation would it be more appropriate to use a do...while statement than a while statement?
Do...while loop over whileDo...while loop
Page 1 of 1
3 Replies - 1164 Views - Last Post: 21 September 2009 - 06:47 PM
Replies To: Do...while loop over while
#2
Re: Do...while loop over while
Posted 21 September 2009 - 06:36 PM
when you want the loop to execute at least once regardless of the loop's condition
#3
Re: Do...while loop over while
Posted 21 September 2009 - 06:40 PM
yes but what kind of situation would call for that?
#4
Re: Do...while loop over while
Posted 21 September 2009 - 06:47 PM
for example if you want to keep taking input from the user until he inputs a negative number..here you want to execute the loop at least once
will break the loop once the user inputs a negative number..or for more sense maybe you would want to execute a block of statements in a loop until the users inputs N when the program asks him if he wants to continue or not
do{
int n = scanner.nextInt();
}while(n >= 0);
will break the loop once the user inputs a negative number..or for more sense maybe you would want to execute a block of statements in a loop until the users inputs N when the program asks him if he wants to continue or not
This post has been edited by mostyfriedman: 21 September 2009 - 06:50 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|