0

How is the output 5

public class Program { public static void main(String[] args) { int x=1,y=1,z=0; do{ z=x+y; }while(++x<=1&&y++>=1); z=x+y+z; System.out.print(z); } }

19th Mar 2020, 8:11 AM
yash vardhan singh
yash vardhan singh - avatar
1 Antwort
0
Initially : x=1 , y =1 and z=0 During first iterations of loop : z = x + y = 2; after execution loop body , condition is checked. ++x <= 1 && y++>=1 This condition fails because first operand (++x<=1) of && evaluates to false. ++x is same as x = x +1; x is incremented to 2 (2<=1 is false) As the first operand of && is false it doesn't check for second operand due to short circuit evaluation. Now outside of loop : x = 2 ,y=1,z =2 z is assigned result of expression x+y+z that is 2+1+2 Hence the result 5. edit: This will help to understand short circuit evaluation :https://code.sololearn.com/cAKZM7vS0yYb/?ref=app
19th Mar 2020, 8:28 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar