Loops 16.0
Realistically, as humans, we perform a lot of routine and repetitive tasks on daily basis with reference to our work places, homes, schools, etc. For example: every human has at least one (1) shower ever day of the week with respect to each month of the year.
In this regard, if these routine and/or repetitive tasks were to be automated; we can simply define a repetition mechanism such that, for every day of the week (daily basis), the expected task is performed as scheduled.
In programming, similar repetition constructs do exist; and these enable programmers to effectively execute a repetitive task with reference to a specified number of times or a given (repetitive) condition. Also, in programming, employing a repetition construct
with regard to implementing a repetitive block of code helps a programmer achieve the following goals, viz:
- It reduces the programming time - the amount of time spent in writing lines of code for a program.
- It reduces the chances of encountering Syntax (or Compile-time) errors during programming.
Furthermore, some of the common repetition constructs that exist in Java include, viz:
while {}
loop construct
for {}
loop construct
do-while {}
loop construct
6.1Operators' Precedence in Java Programming
Basically, when several operators are aggregated with respect to an expression in Java programming, then the following precedence (or Rule of Order) is employed towards determining which operator is processed or evaluated firstly. Thus, the table below showcases a list of common
operators usually employed with reference to Java programming; and operators bearing the same precedence-order number possess same level of ranking or precedence.
Precedence/Ranking |
Operator |
1 (Highest) |
Postfix ++, -- : varId++ , varId-- |
2 |
Prefix ++, -- : ++varId , --varId |
2 |
Bitwise NOT: ∽ |
2 |
Logical NOT: ! |
3 |
Multiplication: * |
3 |
Division: / |
3 |
Modulus: % |
4 |
Addition: + |
4 |
Subtraction: - |
5 |
Less Than: < |
5 |
Greater Than: > |
5 |
Less Than or Equal To: <= |
5 |
Greater Than or Equal To: >= |
6 |
Equal To: == |
6 |
Not Equal To: != |
7 |
Bitwise AND: & |
8 |
Bitwise Exclusive-OR: ^ |
9 |
Bitwise OR: | |
10 |
Logical AND: && |
11 |
Logical OR: || |
12 |
Basic-Assignment: = |
12 |
Add-Assignment: += |
12 |
Subtract-Assignment: -= |
12 |
Multiply-Assignment: *= |
12 |
Divide-Assignment: /= |
12 (Lowest) |
Modulus-Assignment: %= |
6.2while {}
loop construct
Realistically, the
while {}
loop construct can be likened to a car owner trying to replace a flat tire by using a hydraulic jack to lift up the area of the car housing the flat tire. In a bid to do so, notice that the car owner has to push down and push up
on the lever of the hydraulic jack, until the affected area of the car has sufficient clearance from the surface of the ground with reference to the flat tire.
Therefore, a
while {}
loop construct repeats a given block of code repetitively until a specified condition is reached or met. Thus, the implementation syntax of the
while {}
loop construct is as follows:
while (boolean condition == true) {
//action: lines of code
}
while (boolean condition == false) {
//action: lines of code
}
Pseudocode: while {}
loop construct
Additionally, it is important to mention that if a semicolon (
;
), which is used to end any line of code statement in Java, is appended after the condition segment of a
while {}
loop construct;
then this abruptly truncates the entire logic of the
while {}
loop construct. Hence, the code statement(s) within the curly parantheses,
{...}
, of the
while {}
loop construct gets executed irrespective of the condition segment.
while (boolean condition == true); //<-- Semicolon here truncates a "while {}" loop construct
{
//action: This gets executed irrespective of the above condition segment
}
while (boolean condition == false); //<-- Semicolon here truncates a "while {}" loop construct
{
//action: This gets executed irrespective of the above condition segment
}
Pseudocode: while {}
loop construct with truncated condition segment
Exercise 1: Consider the following code snippet with respect to the
while {}
loop construct. It is expected that we "dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
int count = 0, total = 0;
while (count < 100) {
total = total + count;
count = count + 10;
System.out.print("total is: " + total);
System.out.println("; count is: " + count);
}
}
}
//Dry-run: Hand tracing output (start)
/*
-------------------------------------------------------
| condition: count < 100
-------------------------------------------------------
| Iteration | total | count | OUTPUT
-------------------------------------------------------
| 0 (init.) | 0 | 0 |
| 1 | 0 | 10 | total is: 0; count is: 10
| 2 | 10 | 20 | total is: 10; count is: 20
| 3 | 30 | 30 | total is: 30; count is: 30
| 4 | 60 | 40 | total is: 60; count is: 40
| 5 | 100 | 50 | total is: 100; count is: 50
| 6 | 150 | 60 | total is: 150; count is: 60
| 7 | 210 | 70 | total is: 210; count is: 70
| 8 | 280 | 80 | total is: 280; count is: 80
| 9 | 360 | 90 | total is: 360; count is: 90
| 10 | 450 | 100 | total is: 450; count is: 100
-------------------------------------------------------
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 1
Exercise 2: Consider the following code snippet with respect to the
while {}
loop construct. It is expected that we "dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
int count = 0, total = 0;
while (total < 100) {
total = total + count;
count = count + 10;
System.out.print("total is: " + total);
System.out.println("; count is: " + count);
}
}
}
//Dry-run: Hand tracing output (start)
/*
-------------------------------------------------------
| condition: total < 100
-------------------------------------------------------
| Iteration | total | count | OUTPUT
-------------------------------------------------------
| 0 (init.) | 0 | 0 |
| 1 | 0 | 10 | total is: 0; count is: 10
| 2 | 10 | 20 | total is: 10; count is: 20
| 3 | 30 | 30 | total is: 30; count is: 30
| 4 | 60 | 40 | total is: 60; count is: 40
| 5 | 100 | 50 | total is: 100; count is: 50
-------------------------------------------------------
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 2
Exercise 3: Consider the following code snippet with respect to the
while {}
loop construct. It is expected that we "dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
int n = 1;
while (n <= 3) {
System.out.println(n + ", ");
n++;
}
}
}
//Dry-run: Hand tracing output (start)
/*
------------------------------------
| condition: n <= 3
------------------------------------
| Iteration | n | OUTPUT
------------------------------------
| 0 (init.) | 1 |
| 1 | 2 | 1,
| 2 | 3 | 2,
| 3 | 4 | 3,
------------------------------------
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 3
Class Work: Now, consider the following code snippet and "dry-run", via
hand tracing, each block of
while {}
loop-construct code within the code snippet. Also, specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
int n = 5;
while (n >= 0) {
n--;
System.out.print(n + ",");
}
int count = 0, total = 0;
while (total >= 100) {
total = total + count;
count = count + 10;
System.out.print("total is: " + total);
System.out.println("; count is: " + count);
}
}
}
Code Snippet: Class work tasks
6.3for {}
loop construct
A real-world event that can be likened to the operation of the
for {}
loop construct is the working principle of a conventional wall clock. Thus, it takes 12 hours for the hour-hand of a wall clock to revolve around its centre.
That is to say, at every hour, the hour hand makes some (clockwise) degrees rotation about its axis, untill it completes a 360-degree rotation in 12 hours.
In programming, a
for {}
loop construct essentially repeats a given block of code, every time, for a specified (or explicitly stated) number of times. Hence, the implementation syntax of a
for {}
loop construct is as follows:
for (variable initialization; boolean condition; variable update) {
//action: lines of code
}
Pseudocode: for {}
loop construct
Additionally, it is important to mention that if a semicolon (
;
) is appended after the "initialization; condition; update" segment of a
for {}
loop construct;
then this abruptly truncates the entire logic of the
for {}
loop construct. Hence, the lines of code within the curly parantheses,
{...}
, of the
for {}
loop construct get executed irrespective of the "initialization; condition; update" segment.
for (variable initialization; boolean condition; variable update); //<-- Semicolon here truncates a "for {}" loop construct
{
//action: This gets executed irrespective of the above "initialization; condition; update" segment
}
Pseudocode: for {}
loop construct with truncated "initialization; condition; update" segment
Also, it is important to state that the working principle of a
for {}
loop construct is governed by a workflow; and this workflow is as stated below:
- Firstly, a tracker-variable is declared/initialized to help keep track of the repetition counts.
- Secondly, a boolean condition is defined and used to manage the repetition as well as determine an exit state.
- Thirdly, the lines of code, which constitute the body of the
for {}
loop construct, are executed.
- Fourthly, the tracker-variable is updated by some value(s).
- Lastly, control flows back to step (2.) above where the boolean condition is checked to determine if a repetition should occur. Thus, this constitutes a loop execution from step (2.) to step (4.), and back to step (2.), until the repetition condition becomes invalid.
Caption: Workflow of the for {}
loop construct
Exercise 1: Given the following code snippet with respect to the
for {}
loop construct. "Dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
float cnt;
for (cnt = 1.0F; cnt < 2.5F; cnt = cnt + 0.2F) {
System.out.println("cnt is: " + cnt);
}
System.out.println("final value of cnt is: " + cnt);
}
}
//Dry-run: Hand tracing output (start)
/*
------------------------------------
| condition: cnt < 2.5F
------------------------------------
| Iteration | cnt | OUTPUT
------------------------------------
| 0 (init.) | 1.0 |
| 1 | 1.2 | cnt is: 1.0
| 2 | 1.4 | cnt is: 1.2
| 3 | 1.6 | cnt is: 1.4
| 4 | 1.8 | cnt is: 1.6
| 5 | 2.0 | cnt is: 1.8
| 6 | 2.2 | cnt is: 2.0
| 7 | 2.4 | cnt is: 2.2
| 8 | 2.6 | cnt is: 2.4
------------------------------------
final value of cnt is: 2.6
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 1
Exercise 2: Given the following code snippet with respect to the
for {}
loop construct. "Dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
for (int i = 0, j = 5; i <= j; i = i + 1, --j) {
System.out.println("i * j = " + i * j);
}
}
}
//Dry-run: Hand tracing output (start)
/*
------------------------------------
| condition: i <= j
------------------------------------
| Iteration | i | j | OUTPUT
------------------------------------
| 0 (init.) | 0 | 5 |
| 1 | 1 | 4 | i * j = 0
| 2 | 2 | 3 | i * j = 4
| 3 | 3 | 2 | i * j = 6
------------------------------------
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 2
Exercise 3: Consider the following code snippet with respect to the
for {}
loop construct. "Dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
for ( ; ; ) {
System.out.println("creates an infinite loop");
}
}
}
//Dry-run: Hand tracing output (start)
/*
This creates an infinite loop with respect to the screen output.
This infinite repetition occurs because there exist no "boolean
condition" defined to check for the exit state of the program.
Also, there exists neither a "declaration/initialization" nor
"update" for a tracker-variable.
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 3
Differences between the
while {}
and the
for {}
loop constructs:
while {} loop |
for {} loop |
Best suited for repetitions where the exit state/condition is strictly based on a boolean condition. |
Best suited for an explicitly stated and finite number of repetitions. |
Similarities between the
while {}
and the
for {}
loop constructs:
-
The
while {}
loop construct and the for {}
loop construct can almost be used interchangeably with respect to a repetitive task.
In other words, every while {}
loop construct can equally be implemented using a for {}
loop construct, and vice versa.
Class Work: With regard to the concept of
for {}
loop, consider the following code snippet and "dry-run" (via
hand tracing) each block of code in the code snippet. Also, specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
for (int i = 0; i > 0; i = i+1) {
System.out.println("i = " + i);
}
for (boolean j = true; j == true; j = false) {
System.out.println("j = " + j);
}
String trackVar;
for (trackVar = ""; !trackVar.equals("aaaaa"); trackVar = trackVar + "a") {
System.out.println("trackVar = " + trackVar);
}
}
}
Code Snippet: Class work tasks
6.4do-while {}
loop construct
In a real-world scenario, the working principle of the
do-while {}
loop construct can be likened to an already rotating carousel (or Merry-go-Round); such that when it becomes loaded with some weights (e.g. humans), then an explicit instruction is given to the carousel's rotor
so as to specify how many times it is going to rotate around and when to stop.
In Java programming, the
do-while {}
loop construct essentially consists of the following segments, namely:
- A
do {}
block which contains the lines of code to be executed.
- A
while ()
condition which explicitly encloses an instruction that determines how many times the do {}
block of code will be executed.
In other words, the
do-while {}
loop construct executes the
do {}
block at least once (1 time) irrespective of the
while ()
condition. Thereafter, subsequent repetitions of the
do {}
block will be strictly dependent on the intructions specified in the
while ()
condition.
Hence, the implementation syntax of the
do-while {}
loop construct is as follows:
do {
//action: lines of code
}
while (boolean condition == true);
do {
//action: lines of code
}
while (boolean condition == false);
Pseudocode: do-while {}
loop construct
Also, with reference to the
do-while {}
loop construct, another important point to note is that if a semicolon (
;
) is placed immediately after the
do {}
block of code;
then this triggers a Syntax Error with respect to the
do-while {}
loop construct. Hence, the entire
do-while {}
loop construct will not compile for execution.
do {
//action: lines of code
}; //<-- Semicolon here triggers a Syntax Error
while (boolean condition == true);
do {
//action: lines of code
}; //<-- Semicolon here triggers a Syntax Error
while (boolean condition == false);
Pseudocode: do-while {}
loop construct, with semicolon (;
) after the do {}
block, triggers a Syntax Error
Exercise 1: Given the following code snippet with respect to the
do-while {}
loop construct. "Dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
byte count = 1;
do {
System.out.println("count is: " + count);
++count;
}
while (count <= 5);
System.out.println("final value of count is: " + count);
}
}
//Dry-run: Hand tracing output (start)
/*
------------------------------------
| Iteration | count | OUTPUT
------------------------------------
| 0 (init.) | 1 |
| 1 | 2 | count is: 1
------------------------------------
| condition: count <= 5
------------------------------------
| 2 | 3 | count is: 2
| 3 | 4 | count is: 3
| 4 | 5 | count is: 4
| 5 | 6 | count is: 5
------------------------------------
final value of cnt is: 6
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 1
Exercise 2: Consider the following code snippet with respect to the
do-while {}
loop construct. It is expected that we "dry-run" the code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.Scanner;
public class Loops1 {
public static void main(String[] args) {
Scanner usrInput = new Scanner(System.in);
int inVal;
do {
System.out.print("Enter integer < 100: ");
inVal = usrInput.nextInt();
}
while (inVal >= 100);
System.out.println("Final input for inVal is: " + inVal);
usrInput.close();
}
}
//Dry-run: Hand tracing output (start)
/*
------------------------------------------------------
| Iteration | inVal = nextInt() | OUTPUT
------------------------------------------------------
| 0 (init.) | |
| 1 | 203 | Enter integer < 100:
------------------------------------------------------
| condition: inVal >= 100
------------------------------------------------------
| 2 | 507 | Enter integer < 100:
| 3 | 2 | Enter integer < 100:
------------------------------------------------------
Final input for inVal is: 2
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 2
Exercise 3: Consider the code snippet below which can be used for user-input validation. Thus, "dry-run" this code snippet, via
hand tracing, and determine as well as specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.Scanner;
public class Loops1 {
public static void main(String[] args) {
Scanner usrInput = new Scanner(System.in);
int inVal = 5;
do {
System.out.print("Enter value from 0 to 100: ");
inVal = usrInput.nextInt();
}
while (inVal < 0 || inVal > 100);
System.out.println("Final input for inVal is: " + inVal);
usrInput.close();
}
}
//Dry-run: Hand tracing output (start)
/*
------------------------------------------------------
| Iteration | inVal = nextInt() | OUTPUT
------------------------------------------------------
| 0 (init.) | 5 |
| 1 | 555 | Enter value from 0 to 100:
------------------------------------------------------
| condition: inVal < 0 || inVal > 100
------------------------------------------------------
| 2 | 932 | Enter value from 0 to 100:
| 3 | 210 | Enter value from 0 to 100:
| 4 | 402 | Enter value from 0 to 100:
| 5 | 10 | Enter value from 0 to 100:
------------------------------------------------------
Final input for inVal is: 10
*/
//Dry-run: Hand tracing output (stop)
Code Snippet: Dry-run task with reference to Exercise 3
Class Work: Based on the
do-while {}
loop construct, "dry-run" via
hand tracing each block of code within the code snippet below. Also, specify the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
String strVal = "";
int intVal = 1;
do {
if (intVal++ % 2 == 0) {
strVal = strVal + "*";
}
else {
strVal = strVal + "_";
}
System.out.println(strVal);
}
while (intVal < 20);
String sVal = "";
do {
sVal = sVal + "s";
System.out.println(sVal);
}
while (sVal.equalsIgnoreCase("ssssss") == false);
}
}
Code Snippet: Class work tasks
6.5Practice Exercise
-
Given the program below, kindly explain what this program does as well as its expected output(s)?
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.Scanner;
public class Loops1 {
public static void main(String[] args) {
Scanner usrInput = new Scanner(System.in);
int cnt = 0;
double tracker = 0.0, inNum = 1.0;
do {
System.out.print("Please enter a number, or enter 0 to exit: ");
if (usrInput.hasNextDouble()) {
inNum = usrInput.nextDouble();
if (cnt == 0) {
tracker = inNum;
}
else {
if (inNum > tracker) {
tracker = inNum;
}
}
cnt++;
}
else {
usrInput.nextLine(); //Garbage Collector
}
}
while (inNum != 0.0);
System.out.println("Thus far, the greatest number entered is: " + (cnt>0 ? tracker : "nil"));
usrInput.close();
}
}
-
Consider the code snippet below; and dry-run via hand tracing to determine the exact screen output(s).
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
int a, b;
for (a = 2, b = a * 3; a < b; a++);
{
System.out.println("{" + a + "," + b + "}");
}
}
}
-
With reference to the code snippet below, generate the
while {}
loop equivalent for this block of code.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.Scanner;
public class Loops1 {
public static void main(String[] args) {
Scanner usrInput = new Scanner(System.in);
int inputVal;
do {
System.out.print("Please enter an integer less than 100: ");
inputVal = usrInput.nextInt();
}
while (inputVal >= 100);
usrInput.close();
}
}
-
Consider the code snippet below and its inherent logic; thus, generate the
while {}
loop equivalent with reference to this block of code.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
import java.util.Scanner;
public class Loops1 {
public static void main(String[] args) {
Scanner usrInput = new Scanner(System.in);
for (int inputVal = 50; inputVal <= 50; ) {
System.out.print("Please enter an integer greater than 50: ");
inputVal = usrInput.nextInt();
}
usrInput.close();
}
}
-
Which of the following is not a valid
for {}
loop construct in Java?
for (int p = 5; ; ) {}
for ( ; ; ) {}
for (int p = 5, q = 10, r = p + q; ; ++p) {}
for (int p = 5) {}
- All of the above
- TRUE or FALSE: In Java programming, every
for {}
loop construct can be re-implemented as a while {}
loop construct?
- TRUE or FALSE: In Java programming, every
while {}
loop construct can be re-implemented as a for {}
loop construct?
-
Consider the code snippet below and its inherent logic; thus, generate the
for {}
loop equivalent with reference to this block of code.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Loops1 {
public static void main(String[] args) {
int p = 1;
double q = 1.0, r = 0.0;
do {
r = 1.0 * (p++ * 2);
q = q + r;
System.out.print("q = " + q);
System.out.println("; r = " + r);
}
while (r <= 20.0);
}
}
-
Given the code snippet below, which of the following options will be the most appropriate for completing the condition segment of the
while {}
loop construct?
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title Loops 1.
* @author Dr. Bonaventure Chidube Molokwu.
*/
Scanner usrInput = new Scanner(System.in);
float calc = 0.0F;
while ( ) {
float inputVal = usrInput.nextFloat();
calc = calc + inputVal;
}
System.out.println("'calc' is: " + calc);
usrInput.close();
usrInput.hasNextFloat() == false
!usrInput.hasNextDouble()
usrInput.hasNextDouble()
usrInput.hasNextFloat()
- None of the above