Answered step by step
Verified Expert Solution
Question
1 Approved Answer
zyBooks My library > PRG 4 2 0 : Java Programming I home > 3 . 2 : While loops Ahmed Mohamed 3 . 1
zyBooks
My library
PRG : Java Programming I home
: While loops
Ahmed Mohamed
Loops general
Students:
Section is a part of assignment:
Wka Loops due Day
Includes:
PA
CA
While loops
While loop: Basics
A while loop is a program construct that repeatedly executes a list of substatements 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 : 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
: While loop.
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 mainString args
Scanner scnr new ScannerSystemin;
int currPower;
char userChar;
currPower ;
userChar y;
while userChar y
System.out.printlncurrPower;
currPower currPower ;
userChar scnrnextcharAt;
System.out.printlnDone;
y y n
Input
Done
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 doubles currPower, and gets the next input.
Execution jumps back to the while part. userChar is ythe first input so userChar y is true, and the loop body executes again outputting
userChar is ythe second user input so userChar y is true, and the loop body executes a third time outputting
userChar is now n so userChar y is false. Thus, execution jumps to after the loop, which outputs "Done".
Feedback?
PARTICIPATION ACTIVITY
: 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 ;
while userNum
Do something
userNum Get input into userNum
Input:
Check
Show answer
Correct
The loop's expression will be encountered as follows:
First time: userNum was initialized with True, so loop body executes.
Second time: was gotten from input True, so loop body executes.
Third time: was gotten from input False, so loop body does NOT execute.
Thus, the loop body was executed twice.
Input:
Check
Show answer
Correct
The loop's expression will see:
: Body executes
: Body executes
: Body executes
: False, so body does not execute
So the body executes times.
Input:
Check
Show answer
Correct
: Loop body executes
: False, so body does not execute
The additional inputs are not relevant; the 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 : While loop example: Celsius to Fahrenheit.
import java.util.Scanner;
public class ConvertCtoF
public static void mainString args
Scanner scnr new ScannerSystemin;
double celsiusValue;
double fahrenheitValue;
char userChar;
celsiusValue ;
userChar y;
while userChar y
fahrenheitValue celsiusValue ;
System.out.printcelsiusValue C is ;
System.out.printlnfahrenheitValue F;
System.out.printType y to continue, any other to quit: ;
userChar scnrnextcharAt;
celsiusValue celsiusValue ;
System.out.println;
System.out.printlnGoodbye;
C is F
Type y to continue, any other to quit: y
C is F
Type y to continue, any other to quit: y
C is F
Type y to continue, any other to quit: y
C is F
Type y to continue, any other to quit: y
C is F
Type y to continue, any other to quit: y
C is F
Type y to continue, any other to quit: y
C is F
Type y to continue, any other to quit: y
C is F
Type y to continue, any other to quit: y
C is 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 scnrnextcharAt; next reads a string from the user input, then charAt gets the first character within that str
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started