What will be the result?1. int i = 10;2. while(i++ <= 10){3. i++;4. }5. System.out.print(i);10111213
Question
What will be the result?1. int i = 10;2. while(i++ <= 10){3. i++;4. }5. System.out.print(i);10111213
Solution
The result will be 13. Here's the step by step explanation:
- An integer
iis initialized with a value of 10. - The
whileloop condition checks ifiincremented by 1 (i++) is less than or equal to 10. Sinceiis initially 10,i++in the condition will incrementito 11 but return the original value (10) for the comparison. So, the condition is true and the loop will execute. - Inside the loop,
iis incremented again by 1, makinginow 12. - The loop condition is checked again.
i++in the condition will incrementito 13 but return the original value (12) for the comparison. Since 12 is greater than 10, the condition is false and the loop will not execute again. - The value of
i(which is now 13) is printed.
Similar Questions
What will be the output of the following code? int i = 1;while(i<10){ if (i / 2 == 2) cout << i << “ ”;i++;}
What is the output of the following code snippet?int i = 0;while (i < 5) { if (i == 3) break; System.out.print(i + " "); i++;}
What will be the output of the program?public class Sample{ public static void main(String [] args) { int i = 10; do while ( i < 10 ) System.out.print("The value of i is " + i); while ( i > 10 ) ; }}Select one:a.The value of i is 10b.No output is produced.c.Compilation errord.The value of i is 10 The value of i is 10
What is the output of the programme below?public static void main(String[] args) { int x = 0; while (x < 10) { if (x & 2 == 0) { System.out.print(x + “ “); } x++; }}
What will be the output of the following program?while i < 20: print(i) i += 1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.