+ 2
Can anyone give an example where do while needs to be used, and while cannot be used in its place?
4 Respuestas
+ 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...
+ 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.
+ 1
My question is why do while? Almost every case I know we can use a while instead of do while..
0
In a while loop the compiler checks first for condition eventhough the condition is last whereas the do-while loop assigns before checking.