+ 2

Can anyone give an example where do while needs to be used, and while cannot be used in its place?

4th Sep 2016, 4:33 PM
Bhavana
4 Respostas
+ 3
do while is used when you need the statements to b exectuted atleast once for sure even if the condition is false..since the conditiion is chckd at end..for example do { i=0; i++; System.out.println(+x); } while(x!=0); in this above case it prints d x value frst and then checks d condition...then if d condition is false it gets terminated...
4th Sep 2016, 6:55 PM
abi
+ 1
suppose you are reading a file line by line, then you can use while(fileReader.nextLine()), for iteration purposes it is better to use a for loop than while.
4th Sep 2016, 5:04 PM
Sameer Kulkarni
Sameer Kulkarni - avatar
+ 1
My question is why do while? Almost every case I know we can use a while instead of do while..
4th Sep 2016, 5:39 PM
Bhavana
0
In a while loop the compiler checks first for condition eventhough the condition is last whereas the do-while loop assigns before checking.
12th Oct 2016, 4:56 PM
THE.GEEK
THE.GEEK - avatar