All programming languages have a loop capability. Looping is the program logic’s ability to repeat one or more lines of code either: A set number of times, a “for loop” (i.e. 10, 100, 500, 763 or more times) An “unknown” number of times (i.e. loop terminates if a tested condition becomes “true” or as long as the tested condition remains “false”), a “while” loop, a “do until” loop, a “do while” loop.   This Lab exercise demonstrates the use of the “For Loop”, a standard loop in all programming languages. C++ standard For Loop format looks like:                 For (n = 1; n <= 100; n++)             {                         Loop line 1 code;                         Loop line 2 code;                         Loop line 3 code;                         Loop line N code;             }    The loop code needs: A “loop counter variable” (n in the above example) to keep track of how many iterations this loop has run A starting value (i.e. 1 in the above example) A stop test (i.e. run as long as the loop counter variable is 100 or less in the above example)   The loop will start at 1 and increment by 1 until the counter exceeds 100. The loop will then stop at that point. We can substitute variables for the start and stop numeric constant values such as: for (n = 1; n <= NumberofTimes; n++) {             Lines of Loop code; }   This Lab Assignment will allow the user to: Enter a positive number (we will dispense with GIGO code) Run a For Loop from 1 to N times keeping a running count of the loop iteration values (i.e. 1 + 2 + 3 + 4 + 5 + 6 +…+ N)   Display the number of iterations and the total. You might also wish to display the iteration count as the loop progresses:   You ran the for loop 5 times 1 + 2 + 3 + 4 + 5 = 15

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter7: File Handling And Applications
Section: Chapter Questions
Problem 7PE
icon
Related questions
Question

All programming languages have a loop capability. Looping is the program logic’s ability to repeat one or more lines of code either:

  1. A set number of times, a “for loop” (i.e. 10, 100, 500, 763 or more times)
  2. An “unknown” number of times (i.e. loop terminates if a tested condition becomes “true” or as long as the tested condition remains “false”), a “while” loop, a “do until” loop, a “do while” loop.

 

This Lab exercise demonstrates the use of the “For Loop”, a standard loop in all programming languages. C++ standard For Loop format looks like:    

            For (n = 1; n <= 100; n++)

            {

                        Loop line 1 code;

                        Loop line 2 code;

                        Loop line 3 code;

                        Loop line N code;

            } 

 

The loop code needs:

  1. A “loop counter variable” (n in the above example) to keep track of how many iterations this loop has run
  2. A starting value (i.e. 1 in the above example)
  3. A stop test (i.e. run as long as the loop counter variable is 100 or less in the above example)

 

The loop will start at 1 and increment by 1 until the counter exceeds 100. The loop will then stop at that point. We can substitute variables for the start and stop numeric constant values such as:

for (n = 1; n <= NumberofTimes; n++)

{

            Lines of Loop code;

}

 

This Lab Assignment will allow the user to:

  1. Enter a positive number (we will dispense with GIGO code)
  2. Run a For Loop from 1 to N times keeping a running count of the loop iteration values (i.e. 1 + 2 + 3 + 4 + 5 + 6 +…+ N)

 

Display the number of iterations and the total. You might also wish to display the iteration count as the loop progresses:

 

You ran the for loop 5 times

1 + 2 + 3 + 4 + 5 = 15

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT