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

A count-controlled loop iterates a specific number of times. Although you can wr

ID: 3700583 • Letter: A

Question

A count-controlled loop iterates a specific number of times. Although you can write this with a While or a Do-While loop as performed in Lab 5, most programming languages provide a loop known as the For loop. This loop is specifically designed as a count-controlled loop.

The process of the For loop is:

The loop keeps a count of the number of times that it iterates, and when the count reaches a specified amount, the loop stops.

A count-controlled loop uses a variable known as a counter variable to store the number of iterations that it has performed.

Using the counter, the following three actions take place (Initialization, Test, and Increment).

The pseudocode for a for statement looks as follows:

For counterVariable = startingValue to maxValue

Statement

Statement

Statement

Etc.

End For

This lab requires you to implement a count-controlled loop using a For statement.

Step 1: Examine the following code.

Constant Integer MAX_HOURS = 24

Declare Integer hours

For hours = 1 to MAX_HOURS

   Display "The hour is ", hours

End For

           

Step 2: Explain what you think will be displayed to the screen in Step 1. (Reference: For Statement, page 239):

______________________________________________________________

______________________________________________________________

______________________________________________________________

Step 3: Write a For loop that will print 60 minutes to the screen. Complete the missing lines of code.

            Constant Integer MAX_MINUTES = ______________________

     Declare Integer minutes

     For _____________ = 1 to ___________________

         Display ________________________________________

     End For

Step 4: Write a For loop that will print 60 seconds to the screen. Complete the missing lines of code.

            Constant Integer MAX_SECONDS = ______________________

     Declare Integer seconds

     For _____________ = 1 to ___________________

         Display ________________________________________

     End For

Step 5: For loops can also be used to increment by more than one. Examine the following code.

            Constant Integer MAX_VALUE = 10

     Declare Integer counter

     For counter = 0 to MAX_VALUE Step 2

         Display "The number is ", counter

     End For

Step 6: Explain what you think will be displayed to the screen in Step 5. (Reference: Incrementing by Values Other than 1, page 244):

______________________________________________________________

______________________________________________________________

______________________________________________________________

Step 7: Write a For loop that will display the numbers starting at 20, then 40, then 60, and continuing the sequence all the way to 200.

Constant Integer MAX_VALUE = _________________

     Declare Integer counter

     For counter = ____________ to MAX_VALUE Step ____________

         Display "The number is ", _____________________

     End For

Step 8: For loops can also be used when the user controls the number of iterations. Examine the following code:

Declare Integer numStudents

     Declare Integer counter

     Display "Enter the number of students in class"

     Input numStudents

     For counter = 1 to numStudents

        Display "Student #", counter

     End For

Step 9: Explain what you think will be displayed to the screen in Step 8. (Reference: Letting the User Control the Number of Iterations, page 248):

______________________________________________________________

______________________________________________________________

______________________________________________________________

Step 10: For loops are also commonly used to calculate a running total. Examine the following code.

     Declare Integer counter

     Declare Integer total = 0

     Declare Integer number

     For counter = 1 to 5

        Display "Enter a number: "

        Input number

        Set total = total + number

     End For

    

     Display "The total is: ", total

Step 11: Explain what you think will be displayed to the screen in Step 10. (Reference: Calculating a Running Total, page 255):

______________________________________________________________

______________________________________________________________

______________________________________________________________

Step 12: Write the missing lines for a program that will allow the user to enter how many ages they want to enter and then find the average.

Declare Integer counter

     Declare Integer totalAge = 0

     Declare Real averageAge = 0

     Declare Integer age

     Declare Integer number

     Display "How many ages do you want to enter: "

     Input ___________________________

     For counter = 1 to number

        Display "Enter age: "

        Input ___________________

        Set totalAge = _________________ + ________________

     End For

     averageAge = ___________________ / ___________________

    

     Display "The average age is ", _______________________

Explanation / Answer

Step2:

in the step1 code, we use a constant variable MAX_HOURS and we set it to 24 and we declare integer hours which is used in for loop iteration.

in the for loop we loop from value 1 to the value in MAX_HOURS i.e., 24 so the for loop runs for 24 times and in each iteration it displays the following thing i.e., the string The hour is and the value in hours variable because in each iteration the value of variable hours increment by 1 up to 24 and then for loop ends.

so code in step1 displays the following output:

The hour is 1
The hour is 2
The hour is 3
The hour is 4
The hour is 5
The hour is 6
The hour is 7
The hour is 8
The hour is 9
The hour is 10
The hour is 11
The hour is 12
The hour is 13
The hour is 14
The hour is 15
The hour is 16
The hour is 17
The hour is 18
The hour is 19
The hour is 20
The hour is 21
The hour is 22
The hour is 23
The hour is 24

Step 3:

Constant Integer MAX_MINUTES = 60

Declare Integer minutes

For minutes = 1 to MAX_MINUTES

Display "The minute is ", minutes

End For

Step 4:

Constant Integer MAX_SECONDS = 60

Declare Integer seconds

For seconds = 1 to MAX_SECONDS

Display "The second is ", seconds

End For

Step 6:

The counter will increment by two for each iteration so it will print the following

The number is 0

The number is 2

The number is 4

The number is 6

The number is 8

The number is 10

Step 7:

Constant Integer MAX_VALUE = 200

Declare Integer counter

For counter = 20 to MAX_VALUE Step 20

Display “The number is”, counter

End For

Step 9:

it reads the value from students for the number of students in the class and then it loops up to that value and displays the following

for example the student enter the value as 3

then it prints the following

Student # 1

Student # 2

Student # 3

Step 11:

in each iteration, it reads a value from the user and adds it to the total variable which is initially 0 and for loop runs for five times and reads five values from user and each time it adds the user entered the value to total and finally prints the total of five values to the user.

for example, assume user entered the following values

Enter a number:

1

Enter a number:

2

Enter a number:

3

Enter a number:

4

Enter a number:

5

The total is: 15

Step 12:

Declare Integer counter

Declare Integer totalAge = 0

Declare Real averageAge = 0

Declare Integer age

Declare Integer number

Display "How many ages do you want to enter: "

Input number

For counter = 1 to number

Display "Enter age: "

Input age

Set totalAge = totalAge + ag

End For

averageAge = totalAge / number

Display "The average age is ", averageAge

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