Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Writing Exercise: (5 pts) The following are the exercises about the loop

ID: 672369 • Letter: P

Question

Part 1: Writing Exercise: (5 pts)

The following are the exercises about the loops in Java. Write the answers in a comment block

before the code of Part2.

a. What are the three required expressions of a for-loop? (1 pts)

b. Consider the following code. This code was written to generate the output as shown in

Output 2. However, there is an error in this code. Correct the code so that it is able to

generate the desired output. (2 pts)

int count = 0;

while (count < 10)

{

System.out.println("count:" + count);

}

Output 2

count:0

count:1

count:2

count:3

count:4

count:5

count:6

count:7

count:8

count:9

c. What is the output of the following code? (2 pts)

int i =0;

do

{

System.out.println("i is : " + i);

i++;

}while(i < 5);

Explanation / Answer

a. What are the three required expressions of a for-loop? (1 pts)

The three required expressions of a for-loop are initialization, terminatio and increment.

Here
    The initialization expression initializes the loop; it is executed once at the loop begining.
    The loop terminates , when the termination expression evaluates to false.
     After each iteration through the loop the increment expression is invoked. it is perfectly acceptable for this expression to increment or decrement a value.

If these 3 expression are null [for(;;)], then the loop executes infinitely.

See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for more information on java for-loop

b)   Error in the code is : the variable count is not incremented, so it always prints Count: 0 only and the loop
runs indefinitely as count never increments.

Corrected code

int count = 0;

while (count < 10)

{

System.out.println("count:" + count);
count++;

}

c) output of the code
i is : 0
i is : 1
i is : 2
i is : 3
i is : 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote