Instructions:: Greetings Everyone, 1. While - Loop
Instructions:: Greetings Everyone, 1. While - Loop
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!”
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) {
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.
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!");
}
}
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!");
}
}
class Elections {
public static void main (String[] arguments) {
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.
Reference:
Singh, C. (2017, September 10). While loop in Java with examples. beginnersbook.com.
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.
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