Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1299 :

Posted: Tue Dec 12, 2017 3:00 pm
by Rinkesh
What will the following program print?

Code: Select all

class Test{
   public static void main(String args[]){
      int var = 20, i=0;
      do{
         while(true){
         if( i++ > var) break;
         }
      }while(i<var--);
      System.out.println(var);
   }
}
In do-while loop,The statement is executed first and then the condition is checked.So,shouldn't it check the outer while's condition every iteration and decrement the value of var by 1.Tell me what I am missing.

Re: About Question enthuware.ocajp.i.v8.2.1299 :

Posted: Tue Dec 12, 2017 10:30 pm
by admin
Not sure which statement are you talking about. Do you mean the do block? If so, yes.
It does check the condition i<var-- for every iteration of the do-while block.