Term 1: Lesson 17 - While Loops
What is x equal to after the following code is executed? int x = 1; x--;
0
Consider the following code segment int k = 12; while (k > 0) { System.out.print((k % 4) + " "); if(k % 4 == 0 || k % 4 == 1) k -= 2; else k-=3; } What is printed as a result of executing the code segment?
0 2 3 0 2
Consider the following code: int n = 80; while (n > 40) { System.out.print(n % 10 + " " ); n -= 5; } What is output?
0 5 0 5 0 5 0 5
What is x equal to after the following code is executed? int x = 0; x++;
1
Consider the following code segment. int j = 1; int k = 0; while (j < 6 && k < 10) { k += j; j++; } System.out.println(k); What is printed as a result of executing the code segment?
10
Consider the following code segment. int x = 2; while (x < 10 || (x % 5) != 0) x += 3; System.out.println(x); What is printed as a result of executing the code segment?
20
Consider the following code segment. int num = 11; while (num < 21) { num += 2; System.out.println(num); } What is the last number output by the code segment?
21
Consider the following code segment. int a = 9; int b = 12; while (a != b) { while (a < b) a += 9; while (b < a) b += 12; } System.out.println(a); What is printed as a result of executing the code segment?
24
Consider the following code: int n = 4; while (n <= 15) { n += 2; System.out.print(n + " " ); } What is output?
6 8 10 12 14 16
Consider the following code: int n = 75; while (n > 0) { System.out.print(n + " " ); n /= 10; } What is output?
75 7
What is one potential problem with the following loop? while (n != -1) { System.out.println(n); }
Since n does not change the loop will not stop.
loops
The computational concept of running the same sequence multiple times.
What is one potential problem with the following loop? System.out.print("Enter integers. Enter -1 to exit."); System.out.println(" Count of integers entered will be returned."); int n=0; int c=0; while (n != -1) { n = scan.nextInt(); c++; } System.out.println(c);
The loop counts when the user enters -1 into the keyboard, so the count will be one too many.
scope of statement
When we declare a variable inside of a loop or an if statement, its scope is that loop or if statement, and it doesn't exist outside of that loop or if statement.
The difference between while loops and if loops is that
a while loop will keep executing the code over and over as long as the condition is true
Loops can be stopped using
counter variables or wait for a specific condition to come to pass
In order for a while loop to stop, the control variable must not change.
false
Java is an efficient programming language and it likes to save memory, so it does ____________ to get rid of things that are no longer needed.
garbage collection
When using while loops it's important to make sure that they will
stop eventually, otherwise we have what we call an infinite loop, which can cause a Stack Overflow Exception, and crash the program, if not the whole computer.
while loop only executes if
the condition is true to begin with
While loops have two components:
the condition, and the code to execute if the condition is true, just like if statements.
A while loop uses a loop to repeat commands.
true
If A is true, B is true and C is true, is the following compound statement true or false? (A && B) && (B || C)
true
The control variable must change in order for a while loop to stop.
true