# 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 runA 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 times1 + 2 + 3 + 4 + 5 = 15

Question
5 views

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

check_circle

Step 1

Program:

#include <iostream>

using namespace std;

//definition of main method

int main()

{

//declare the required variables

int n, sum = 0;

//get the input from the user

cout << "Enter a positive integer: ";

cin >> n;

//display the number of iterations

cout << "You ran the for loop " << n << " times\n";

//iterate 1 to n numbers

for (int i = 1; i <= n; ++i)

{

sum += i;

//display the "i"

cout << i << " + ";

}

//remove the last "+" in the above statement

cout << '\b' << " " << '\b'<< '\b';

//display the sum

cout << " = " << sum;

//return statement

return 0;

}

Step 2

Screenshot:

...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

### Computer Science 