Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1. Give an example of while loop, then provide the equivalent do-while loop and for loop.

1.1 while loop

int count = 5;

while (count >0){

System.out.println("The rocket will be launched in: "+ count);

count --;

System.out.println("The rocket has been successfully launched!");

1.2 do-while loop

int count = 5;

do{

System.out.println("The rocket will be launched in: "+ count);

count --;

while (count >0);

System.out.println("The rocket has been successfully launched!");

1.3 for loop

for (int count = 5; count >0; count --){

System.out.println("The rocket will be launched in: "+ count);

System.out.println("The rocket has been successfully launched!");


2. An example of do-while loop along with the equivalent while loop and for loop.

2.1 do-while loop

int a = 5;

int b = -1;

do {

System.out.println(a+" is larger than "+b);

b ++;

while (b<a);

System.out.println("Both a and b has the same value.");

2.2 while loop

int a = 5;

int b = -1;

while (b<a){

System.out.println(a+" is larger than "+b);

b ++;

System.out.println("Both a and b has the same value.");

2.3 for loop

int a = 5;

for (int b = -1; b < a; b++){


System.out.println(a+" is larger than "+b);

System.out.println("Both a and b has the same value.");

3. An example of for loop along with the equivalent while loop and do-while loop.

3.1 for loop

for (int count = 10; count > 0; count --){

System.out.println(count);

System.out.println("The time is up!");

3.2 while loop

int count = 10;

while (count > 0){

System.out.println(count);

count --;

System.out.println("The time is up!");

3.3 do-while loop

int count = 10;


do{

System.out.println(count);

count --;

}while (count > 0);

System.out.println("The time is up!");

While loop is used in situations where we need to repeat the code for unknown number of

times. It will repeat as many times as its condition remains true. First, while loop will check the

Boolean expression and if its true, then it will execute the code within the loop and then go back

to the start. Only when the Boolean expression is true, it will exit the loop. While loop is easy to

read but it is slow and can cause infinite loop if not properly coded.

Do-while loop is used in situations where we want the code to execute at least once.

The flow of do-while loop is a little bit different from while loop. Unlike while loop, which

executes only after checking its condition, do-while loop will execute at first then check the

condition second. Therefore, the code is executed at least one time. Do-while loop is less

readable than while loop but it is the fastest one of the three loops.

For loop is used in situations where we want the code to execute for a limited number of

times. In the case of for loop, it is easier to read and less possible to accidently stuck in an

infinite loop. It is also the second fastest loop. While loop and for loop can end without any

execution where do-while loop executes at least once.

Reference:

Eck, D. J. (2019). Introduction to programming using Java, version 8.1. Hobart and William

Smith College. http://math.hws.edu/javanotes.

You might also like