0% found this document useful (0 votes)
535 views5 pages

Instructions:: Greetings Everyone, 1. While - Loop

The document provides examples of while, do-while, and for loops in Java. It gives one example of each loop type, along with the equivalent for and while/do-while loops. The while loop executes as long as a condition is true. The do-while loop guarantees running the code block at least once. The for loop is useful when the number of iterations is known. Advantages include the while loop not running if the condition is false initially, and do-while always running at least once.

Uploaded by

Gabriel Sobec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
535 views5 pages

Instructions:: Greetings Everyone, 1. While - Loop

The document provides examples of while, do-while, and for loops in Java. It gives one example of each loop type, along with the equivalent for and while/do-while loops. The while loop executes as long as a condition is true. The do-while loop guarantees running the code block at least once. The for loop is useful when the number of iterations is known. Advantages include the while loop not running if the condition is false initially, and do-while always running at least once.

Uploaded by

Gabriel Sobec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Instructions:

Give an example of a ​while​ loop, then provide the equivalent do-while loop and for loop. Then
give a different example of a ​do-while​ loop, along with the equivalent while loop and for loop.
Finally, give an example of a ​for​ loop, along with the equivalent while loop and do-while loop.
Use your examples to illustrate the advantages and disadvantages of each looping structure,
and describe those advantages and disadvantages.

Assignment:

Greetings everyone,

1. While - Loop

The ​while-loop​ is used in situations where we don't know how many times the block or
statement should be repeated, but we want it to continue repeating as long as a condition is
true. The expression must evaluate as a boolean result. Once we are inside the loop, the body
of the loop is repeated until the condition is not true, that is when the condition is evaluated as
false.

The advantage of the while loop over the other loops is that it never runs if the
expression test is false the first time the while expression is verified.​ The program will
begin executing the first statement after the while loop.

In the program below, the variable "countdown" initialized with the value 10. Then using while
loop the value of “countdown” is printed with a single decrement at each iteration. The Loops
run until the value of “countdown” becomes “0” the condition in while loop becomes false and it
comes out of the loop and print “Times is over!”

Example 1.1 - while-loop

class Countdown {
public static void main (String[] arguments) {
int count = 10;
while(count > 0){
System.out.println(count);
count = count - 1;
}
System.out.println("Time is over!");
}
}
Example 1.1 do-while

class Countdown {
public static void main (String[] arguments) {

int count = 10;


do {
System.out.println(count);
count = count - 1;
}
while(count > 0)
System.out.println("Time is over!");
}
}

Example 1.2 for-loop

class Countdown {
public static void main (String[] arguments) {
for(int count = 10; count > 0; count--) {
System.out.println(count);
}
System.out.println("Time is over!");
}
}

2. Do - While

The do-while loop is something similar to the while-loop, but it has an advantage that the
code inside the loop is guaranteed to run at least once.​ This because the expression is
evaluated after the code was run.

The second example is a code to print every election year in The United States since 1788,
using do-while.

Example 2.1 do-while

class Elections {
public static void main(String[] arguments) {
int year = 1788;

do {

System.out.println(year);
year = year + 4;
}
while(year < 2020);
System.out.println("Election Year!");
}
}

Example 2.2 while-loop

class Elections {
public static void main (String[] arguments) {
int year = 1788;
while(year < 2020) {
System.out.println(year);
year = year + 4;
}
System.out.println("Elections Year!");
}
}

Example 2.3 for-loop

class Elections {
public static void main (String[] arguments) {

for(int year = 1788; year < 2020; year += 4) {


System.out.println(year);
}

System.out.println("Elections Year!");
}
}

3. For - Loop
In the case of the for-loop, is a good choice when the number of repetitions is known or
can be supplied by the user.

In this example, we have a simple program to calculate how many 2 point scores you have to
make to get 116 points in total.

Example 3.1 for-loop

public class PlusTwo {


public static void main(String[] arguments) {
int points = 0;
for(points = 0 ; points < 116 ; points+=2) {

} System.out.println("You need to score " + points /


2 + " times to make 116 points.");

Example 3.2 while-loop

public class PlusTwo {


public static void main(String[] args) {
int points = 0;
while(points < 116) {
points = points + 2;
} System.out.println("You need to score " + points / 2 + " times to
make 116 points.");
}

Example 3.3 do-while

public class PlusTwo {


public static void main(String[] args) {
int points = 0;
do {
points = points + 2;
}
while(points < 116); {
System.out.println("You need to score " + points / 2 + " times to
make 116 points.");
}
}

Reference:

Singh, C. (2017, September 10). ​While loop in Java with examples​. beginnersbook.com.

Retrieved June 30, 2020, from

https://beginnersbook.com/2015/03/while-loop-in-java-with-examples/

Singh, C. (2017, September 10). ​Do-while loop in Java with example.​ beginnersbook.com.

Retrieved June 30, 2020, from

https://beginnersbook.com/2015/03/do-while-loop-in-java-with-example/

Singh, C. (2017, September 10). ​For loop in Java with example​. beginnersbook.com. Retrieved

June 30, 2020, from ​https://beginnersbook.com/2015/03/for-loop-in-java-with-example/

You might also like