How many times is the text "Let's have fun with Java." printed when this code snippet is run? int i = 0; do { System.out.println("Let's have fun with Java."); i++; if (i % 2 == 0) { i = 10; } } while (i <= 10);
It is because the above code has one loop which executes three times. The loop executes for the value of i = 0,1 and 10.
when the value of i is 0 then "++i" will increase the value 1 and the text will be printed.
If the value of i=1, then the value of i is 2 in the second iteration and the again the text is printed, then the if condition gives the true result and the value of i will be 10.
Then the loop executes for the last time when the value of i is 10.Then the value will be 11 because of the increment operator and the text will be printed for the third time.
Then the while loop is not true for the 11 value of i and the loop will get terminated.