Question: zyBooks My library > PRG 4 2 0 : Java Programming I home > 3 . 2 : While loops Ahmed Mohamed 3 . 1

zyBooks
My library
>
PRG 420: Java Programming I home
>
3.2: While loops
Ahmed Mohamed
3.1 Loops (general)
Students:
Section 3.2 is a part of 1 assignment:
Wk3a - Loops [due Day 7]
Includes:
PA
CA
3.2 While loops
While loop: Basics
A while loop is a program construct that repeatedly executes a list of sub-statements (known as the loop body) while the loop's expression evaluates to true. Each execution of the loop body is called an iteration. Once entering the loop body, execution continues to the body's end, even if the expression would become false midway through.
Construct 3.2.1: While loop.
while (expression){// Loop expression
// Loop body: Executes if expression evaluated to true
// After body, execution jumps back to the "while"
}
// Statements that execute after the expression evaluates to false
Feedback?
PARTICIPATION ACTIVITY
3.2.1: While loop.
1
2
3
4
5
userChar is now 'n', so userChar =='y' is false. Thus, execution jumps to after the loop, which outputs "Done".
import java.util.Scanner;
public class CountUp {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
int currPower;
char userChar;
currPower =2;
userChar ='y';
while (userChar =='y'){
System.out.println(currPower);
currPower = currPower *2;
userChar = scnr.next().charAt(0);
}
System.out.println("Done");
}
}
y y n
Input
Done
8
4
2
Output
Captions
When encountered, a while loop's expression is evaluated. If true, the loop's body is entered. Here, userChar was initialized with 'y', so userChar =='y' is true.
Thus, the loop body is executed, which outputs currPower's current value of 2, doubles currPower, and gets the next input.
Execution jumps back to the while part. userChar is 'y'(the first input), so userChar =='y' is true, and the loop body executes (again), outputting 4.
userChar is 'y'(the second user input), so userChar =='y' is true, and the loop body executes (a third time), outputting 8.
userChar is now 'n', so userChar =='y' is false. Thus, execution jumps to after the loop, which outputs "Done".
Feedback?
PARTICIPATION ACTIVITY
3.2.2: While loops: Number of iterations.
For the following code, indicate how many times the loop body will execute for the indicated input values.
int userNum =3;
while (userNum >0){
// Do something
userNum =// Get input into userNum
}
1)
Input: 5-1
.
Check
Show answer
Correct
.
2_The loop's expression will be encountered as follows:
First time: 3>0(userNum was initialized with 3). True, so loop body executes.
Second time: 5>0(5 was gotten from input). True, so loop body executes.
Third time: -1>0(-1 was gotten from input). False, so loop body does NOT execute.
Thus, the loop body was executed twice.
2)
Input: 210
3
Check
Show answer
Correct
3
The loop's expression will see:
3>0: Body executes
2>0: Body executes
1>0: Body executes
0>0: False, so body does not execute
So the body executes 3 times.
3)
Input: -154
1
Check
Show answer
Correct
1
3>0: Loop body executes
-1>0: False, so body does not execute
The additional inputs are not relevant; the -1 ends the looping.
Feedback?
Basic while loop example
The following Celsius to Fahrenheit example shows the common pattern of getting user input at the end of each loop iteration to determine whether to continue looping.
Figure 3.2.1: While loop example: Celsius to Fahrenheit.
import java.util.Scanner;
public class ConvertCtoF {
public static void main(String [] args){
Scanner scnr = new Scanner(System.in);
double celsiusValue;
double fahrenheitValue;
char userChar;
celsiusValue =0.0;
userChar ='y';
while (userChar =='y'){
fahrenheitValue =(celsiusValue *9.0/5.0)+32.0;
System.out.print(celsiusValue +" C is ");
System.out.println(fahrenheitValue +" F");
System.out.print("Type y to continue, any other to quit: ");
userChar = scnr.next().charAt(0);
celsiusValue = celsiusValue +5;
System.out.println("");
}
System.out.println("Goodbye.");
}
}
0.0 C is 32.0 F
Type y to continue, any other to quit: y
5.0 C is 41.0 F
Type y to continue, any other to quit: y
10.0 C is 50.0 F
Type y to continue, any other to quit: y
15.0 C is 59.0 F
Type y to continue, any other to quit: y
20.0 C is 68.0 F
Type y to continue, any other to quit: y
25.0 C is 77.0 F
Type y to continue, any other to quit: y
30.0 C is 86.0 F
Type y to continue, any other to quit: y
35.0 C is 95.0 F
Type y to continue, any other to quit: y
40.0 C is 104.0 F
Type y to continue, any other to quit: q
Goodbye.
Feedback?
The Scanner does not directly support reading a single character. The above program uses the following sequence: userChar = scnr.next().charAt(0);. next() reads a string from the user input, then charAt(0) gets the first character within that str

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!