Computer Science While Loops
What is the sentinel value in this ? int num = 0, sum = 0; while(num != -1) { System.out.println("Enter a positive " +"integer (-1 to quit)"); num = scan.nextInt(); if(num != -1) sum += num; } System.out.println(sum);
-1
What is the minimum number of times that ANY while loop will repeat?
0
What will this code display? int i = 0; while (i < 5) { System.out.print (i + ", "); i ++; }
0, 1, 2, 3, 4,
What will this code display? int i = 0; do { System.out.print (i + ", "); } while (i < 5);
0, repeated forever
How many times does this run? int i = 9; do { System.out.print(i + " "); i++; } while(i<=5);
1
How many times will the following code print "Welcome to Java"?int count = 0;do { System.out.println("Welcome to Java"); count++;} while (count < 10);
10
How many times will the following code print "Welcome to Java"?int count = 0;while (count < 10) { System.out.println("Welcome to Java"); count++;}
10
What is the first number printed? Int value = 15; while (value < 28) { System.out.println(value); value++; }
15
What is the value equal to after the loop ends? Int value = 15; while (value < 28) { System.out.println(value); value++; }
28
What is the output of the following code?int x = 0;while (x < 4) { x = x + 1;}System.out.println("x is " + x);
4
What is wrong with the code below? int i = 0; While (i < 5) { System.out.println(i); i++; }
The While is incorrectly capitalized
What is wrong with the code below? int i = 0; while (i < 5) { System.out.println(i); } i++;
The closing curly bracket is in the wrong place , the counter value never increases, the loop never ends
What is a sentinel value?
a value to be typed in to stop a loop
what does ++ mean ?
adds one to a variable
where does the while statement go in a do-loop?
after and outside the do loop
Which loop will execute the body of loop even when condition controlling the loop is initially false?
do while loop
When a piece of code runs unintentionally forever it is known as
infinite loop
The difference between a while loop and a do while loop is
it is possible for a while loop to not run at all
what happens if the condition is initially false?
it never runs
Is this correct? while (condition);
no, has the semi colon
How many times do do-loops run at least
once
what does -- mean?
subtracts one from a variable
when does the while loop stop?
when the condition is false
What's the output int x = 0, y = 0; y = x++ + 1; System.out.println("x=" +x + ", y=" + y);
x = 1 y= 1
What's the output ? int x = 0, y = 0; y = ++x + 1; System.out.println("x=" +x + ", y=" + y);
x = 1 y=2
In a do-loop, do you need a semi colon after the while statement?
yes
does a while loop need curly braces?
yes
is this: count = count + 1 the same as count += 1 ?
yes