0% found this document useful (0 votes)
6 views

Lab 5 2 Using the for Looping Repetition Structure

Lab 5.2 focuses on using the for looping structure in programming, specifically for creating count-controlled loops. The lab includes exercises for designing and implementing programs that calculate factorial values using both increment and decrement update conditions in C++. Students will also redesign a previous program to utilize a for loop and document their code execution results.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab 5 2 Using the for Looping Repetition Structure

Lab 5.2 focuses on using the for looping structure in programming, specifically for creating count-controlled loops. The lab includes exercises for designing and implementing programs that calculate factorial values using both increment and decrement update conditions in C++. Students will also redesign a previous program to utilize a for loop and document their code execution results.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 5.

2 Using the for Looping (Repetition) Structure

The for looping structure, sometimes called a counted or indexed for loop, is a
specialized form of the while loop that you use to simplify writing count-controlled loops.
To use the for looping structure, you write a for statement, in which for is followed by
the initial statement, loop condition, and update statement enclosed within parentheses.
The program executes a for loop in the following sequence:

1. The initial statement executes.


2. The loop condition is evaluated. If the loop condition is false, the body of the loop
never executes. If the loop condition is true, the body of the loop is executed.
3. The update statement executes after the body of the loop ends.
4. Steps 2 and 3 are repeated until the loop condition evaluates to false.

Objectives
In this lab, you create programs that use the for loop.

After completing this lab, you will be able to:


• Write a for loop with an increment update condition.
• Write a for loop with a decrement update condition.

Estimated completion time: 40–50 minutes


Lab 5.2 Steps: Using the for Looping (Repetition) Structure

In the following exercises, you design and write programs that use for loops.

1a. Design a program that prompts the user to enter a positive integer. Use a for loop to
calculate and display the factorial value of that number. Increment the counter in your
for loop. The factorial value of a number is a number multiplied by every factor
between 1 and the number, inclusive. For instance, 3 factorial is 3 * 2 * 1. If the user
enters a negative number, display a message indicating that the program calculates only
positive numbers. Otherwise, display the result of the calculation.

Write your design in the following space. Your design should be a list of C++ comments
without any code.
1b. Write a C++ program based on the design you created in Exercise 1a and name it
factorial1.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message.

1c. Enter, compile, link, and execute factorial1.cpp. Then copy the output and save it in a
block comment at the end of your program. Save factorial1.cpp in the Chap05 folder of
your Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.

Enter a positive integer to find the factorial value: 5


The factorial of 5 is 120
2a. Redesign the program you wrote in Exercise 1 to use a decrement counter in your for
loop. Write your design in the following space. Your design should be a list of C++
comments without any code.
2b. Write a C++ program based on the design you created in Exercise 2a and name it
factorial2.cpp. Step through the code by hand.

Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message.

2c. Enter, compile, link, and execute factorial2.cpp. Then copy the output and save it in a
block comment at the end of your program. Save factorial2.cpp in the Chap05 folder of
your Student Data Files.

The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.

Enter a positive integer to find the factorial value: 5


The factorial of 5 is 120
3a. Redesign the roseter.cpp program you wrote in Lab 5.1, Exercise 1 to use a for loop.
Write your design in the following space. Your design should be a list of C++ comments
without any code.

3b. Write a C++ program based on the design you created in Exercise 3a and name it
roster2.cpp. Step through the code by hand.

3c. Enter, compile, link, and execute roster2.cpp. Then copy the output and save it in a
block comment at the end of your program. Save roster2.cpp in the Chap05 folder of
your Student Data Files.

Your screen results should be the same for roster1.cpp and roster2.cpp when running
the same data.

You might also like