ClassEx_while_loop

ClassEx_while_loop - int count = 1; int sum = 0; int...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1. How many times will “hello world” be printed in the following program: int count = 0; while (count < 10) { System.out.println(“Hello World”); count++; } 2. How many times will “hello world” be printed in the following program: int count = 10; while (count < 1) { System.out.println(“Hello World”); count++; } 3. How many times will “I love Java Programming !” be printed in the following program segment? int count = 10; while (count >= 10) { System.out.println(“I love Java Programming !”); count++; } 4. How many times will “I love Java Programming !” be printed in the following program segment? int count = 0; while (count < 10) { System.out.println(“I love Java Programming !”); } 5. The code below is supposed to print the integers from 10 to 1 backwards. What is wrong with it? count = 10; while (count >= 0) { System.out.println(count); count = count + 1; } 6. Consider the following loop final int LIMIT = 16; sum nextVale count
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int count = 1; int sum = 0; int nextVal = 2; while (sum &lt; LIMIT) { sum = sum + nextVal; nextVal = nextVal + 2; count = count + 1; } Trace this loop, that is, in the table next to the code, show values for variables nextVal, sum and count at each iteration. 7. Trace the loop and explain what is the output for the following loops: Loop Output i = 0; sum = 0; while(sum &lt; 10) { i++; sum = sum + i; System.out.println(i + + sum); } Sum I 0 1 1 2 3 i = 0; sum = 0; while(sum &lt; 10) { i++; sum = sum - i; System.out.println(i + + sum); } Sum i 0 -1 1-3 2 i = 0; sum = 0; while(sum &lt; 0) { i++; sum = sum - i; System.out.println(i + + sum); } false i = 0; sum = 0; while(sum &gt;= 10) { i++; sum = sum + i; System.out.println(i + + sum); } false i = 0; sum = 0; while(sum &lt; 10) ; { i++; sum = sum + i; System.out.println(i + + sum); } Terminate condition...
View Full Document

Page1 / 2

ClassEx_while_loop - int count = 1; int sum = 0; int...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online