CPS 121 Ch 4
The while loop has two important parts:
(1) a boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.
A count-controlled loop must possess three elements:
1. It must initialize a control variable to a starting value. 2. It must test the control variable by comparing it to a maximum value. When the control variable reaches its maximum value, the loop terminates. 3. It must update the control variable during each iteration. This is usually done by incrementing the variable.
Ch 4.2 How many times will "Hello World" be printed in the following program segment?int count=10;while(count<1){System.out.println("Hello World");count++;} It will never be printed out because count is initialized as greater than 1 and the loop will only execute when count is less than 1.
It will never be printed out because count is initialized as greater than 1 and the loop will only execute when count is less than 1.
Ch 4.2 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!");
It will print out an infinite amount of times because there is no increment to update the count.
Write a do-while loop that asks the user to enter a number. The number should be read in and multiplied by 10, and the result should be stored in the variable timesTen. The loop should iterate as long as timesTen contains a value less than 125.
Scanner kb = new Scanner(System.in);int timesTen;do{System.out.print("Enter a number: ");timesTen = kb.nextInt() * 10;}while (timesTen < 125);
onvert the do-while loop in the following code segment to a while loop:Scanner keyboard = new Scanner(System.in);String input;char sure;do{System.out.print("Are you sure you want to quit? ");input = keyboard.nextLine();sure = input.charAt(0);} while (sure !='Y' && sure !='N')
Scanner keyboard = new Scanner(System.in);String input;char sure;while (sure !='Y' && sure !='N'){System.out.print("Are you sure you want to quit? ");input = keyboard.nextLine();sure = input.charAt(0);}
Convert the while loop in the following code segment to a do-while loop:Scanner keyboard = new Scanner(System.in);int x = 1;while (x > 0){System.out.print("Enter a number: ");x = keyboard.nextInt();}
Scanner keyboard = new Scanner(System.in);int x = 1;do{System.out.print("Enter a number: ");x = keyboard.nextInt();}while (x > 0);
What will the following code segments display?a) for (int count = 0; count < 6; count++)System.out.println(count + count);b) for (int value = -5; value < 5; value++)System.out.println(value);c) for (int x = 5; x <= 15; x+=3)System.out.println(x);System.out.println("Next");
a) 0246810b) -5-4-3-2-101234c) 581114Next
4.1 What will the following program segments display? a) x = 2; y = x++; System.out.println(y);
a) 2
Each repetition of a loop is known as
an iteration.
4.1 What will the following program segments display? b) x = 2; System.out.println(x++);
b) 2
4.1 What will the following program segments display? c) x = 2; System.out.println(--x);
c) 1
A loop is a
control structure that causes a statement or group of statements to repeat.
4.1 What will the following program segments display? d) x = 2;y = x--;System.out.println(y)
d) 2
Write a for loop that displays all of the odd numbers from 1 through 54, inclusive.
for (int i = 54; i > 0; i = i -2){System.out.println(i);}
The first line of the for loop is known as the
loop header.
Java has three looping control structures:
the while loop, the do-while loop, and the for loop.
The while loop is known as a pretest loop,
which means it tests its expression before each iteration.