function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pavani Akella 9Pavani Akella 9 

Output of the code:

Integer i=1,counter=2;
do{
System.debug(i);
i++;
counter++;
}while(i==20&&counter==21);

What would be the output of the code?
Thanks in advance!
 
Best Answer chosen by Pavani Akella 9
Narender Singh(Nads)Narender Singh(Nads)
Hi Pavani,
The output of your code will be 1.
The loop will be executed for the first time(that's how do-while loops are designed to work) producing output 1. Then the condition gets checked and a false is returned. And the loop execution stops.
 

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi Pavani,
The output of your code will be 1.
The loop will be executed for the first time(that's how do-while loops are designed to work) producing output 1. Then the condition gets checked and a false is returned. And the loop execution stops.
 
This was selected as the best answer
Prashant Pandey07Prashant Pandey07
Hi Pavani,
The output for integer will be 1 and the counter will be 2.
+1 for Narender..
Do-while runs at least once even if the condition is false because the condition is evaluated, after the execution of the body of loop.
For example, in the above code while the condition is not being satisfied and the output is 1 and 2. if you try to modify the code as follow
Integer i=1,counter=2;
do{
   System.debug(i);
            i++;
   

counter++;
  
}
             while(i==2&&counter==3);
system.debug(+counter);

The above logic will return the integer output 2 and 3 and the counter will be 4.

--
Thanks,
Prashant