Loops in C++ - Reading Assignment

  1. While Statement? Use it to perform a looping function.
  2. Value needed for the loop to continue? The value of true.
  3. Infinite loop? A function that will not stop cycling on its own, requiring external input to stop it.
  4. Iteration? A count of the number of cycles of the looping function
  5. When for loop vs while loop? When have a known cycle count requirement.
  6. How would you write a for-loop in code?
    for (init-statement; condition-expression; end-expression)
    statement …OR… for(int i=0; i < length; i++) { /* do stuff here*/ }
  7. What is an off-by-one error? When the iteration count is off by one, a coding error.
1 Like

part1

  1. Allow you to loop an expression as long as it evaluates to true
  2. True
  3. A loop the always evaluates to true and never stops looping
  4. Every time a loop executes
    part2
  5. When we know how many times the loop needs to iterate
  6. for(int count{0}; count < exponent; ++count)
    cout<< count<<’ ';
    3.When the loop iterates one too many or one too few times
1 Like

Part 1

1. What can we do with the while statement?

Repeat a block of code until the condition is false.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?

The loop will continue while the condition is true.

3. What is an infinite loop?

A loop that never terminates- it’s condition never becomes false. The simplest one is:

while(true) {
// do stuff forever
}
4. What is an iteration?

Each execution of the loop body is one iteration.

Part 1 Quiz

1) In the above program, why is variable inner declared inside the while block instead of immediately following the declaration of outer?

Because the inner variable needs to be reset to 1 for every loop iteration to start the printing back at 1. If the inner var was declared outside the loop it would output the following:

1
2
3
4
5

instead of

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2, 3, 4)

I used for loops instead of while for these because I like them better.

#include <iostream>

using namespace std;

int sumTo(int value) {
    int sum = 0;
    for (int i=1; i <= value; i++) {
        sum += i;
    }
    return sum;
}

void printSumToResult(int value) {
    cout << "value: " << value << ", sumTo: " << sumTo(value) << "\n";
}

void printAtoZ() {
    int startChar = (int)'a';
    int endChar = (int)'z';
    for(int i = startChar; i <= endChar; i++) {
        cout << "Ascii value: " << i << ", char: " << (char)i << "\n";
    }
}

void upsideDownTriangle() {
    int baseLength = 5;
    for(int row=baseLength; row > 0; row--) {
        for(int col=row; col > 0; col--) {
            cout << col << " ";
        }
        cout << "\n";
    }
}

void invertedTriangle() {
    int baseLength = 5;
    for(int row=1; row <= baseLength; row++) {
        for(int col=baseLength; col > 0; col--) {
            if (col > row) {
                cout << "  ";
            } else {
                cout << col << " ";
            }
        }
        cout << "\n";
    }
}

int main()
{
    printSumToResult(0);
    printSumToResult(1);
    printSumToResult(3);

    cout << "\n";

    printAtoZ();

    cout << "\n";

    upsideDownTriangle();

    cout << "\n";

    invertedTriangle();

    return 0;
}

Part 2

1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?

When the number of iterations is known or can be calculated.

2. How would you write a for-loop in code?
for (int i=0; i < 10; i++) {
}
* It's also possible to omit any of the 3 sections in parenthesis by leaving them blank but keeping the semi colons
* You can also declare multiple loop variables and use them in each of the 3 sections by separating them with commas.
```c++
for(int i=0, j=0; i<10, j<10; i++, j++) {
// stuff
}
3. What is an off-by-one error?

When a loop iterates one too many times or 1 less time than it should. Usually caused by logic errors in loop counters or termination conditions. I.e:

  • Using array.length instead of array.length - 1
  • Starting at index 1 instead of zero
  • Using < instead of <= or vice versa

Part 2 Quiz

1) Write a for loop that prints every even number from 0 to 20
for(int i=0; i<=20; i+=2) {
cout << i << "\n";
}
2) Write a function named sumTo() that takes an integer parameter named value, and returns the sum of all the numbers from 1 to value
#include <iostream>

using namespace std;

int sumTo(int value) {
    int sum = 0;
    for (int i=1; i <= value; i++) {
        sum += i;
    }
    return sum;
}

void printResult(int value) {
    cout << "value: " << value << ", sumTo: " << sumTo(value) << "\n";
}

int main()
{
    printResult(0);
    printResult(1);
    printResult(3);

    return 0;
}
3) What's wrong with the following for loop?

This will result in an infinite loop because the count variable is an unsigned int. On the 10th iteration of the loop count will be zero. When --count is executed this will result in an underflow to the maximum int value instead of -1. This will continue forever as the only way to terminate the loop is for count to be negative, which is impossible for an unsigned int.

1 Like

:heart_eyes: Really sir, you got me amaze with the effort you made in every one of your answers! You truly are becoming an example for the Academy, please keep them like that! :muscle:

Always a pleasure to read your answers!

Carlos Z.

1 Like

What can we do with the while statement?
Loop a statement while the expression is true.
What value needs the statement in the brackets evaluate to in order for the loop to continue?
The statement loops aslong as the while expression is true.
What is an infinite loop?
A loop where the expression always evaluates to true.
What is an iteration?
Each time a loop executes.

When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
When we know exactly how many iterations we need to do.
How would you write a for-loop in code?
for(int count = 0; count < 10; ++count){
cout << count << endl;
}
What is an off-by-one error?
When the for loop iterates one too many or one too few times.

1 Like
  1. Repeat a section of code until predefined conditions are met.

  2. True

  3. It occurs when the expression is always true and the code continues to run forever.

  4. An individual turn through a loop.

  5. FOR loops are ideal for when you know how many times you want a loop to iterate.

  6. for (initial statement, condition statement, end-expression)
    statement

  7. When a loop starts or stops one position off.

1 Like

Article 1:

  1. White is one of the four looping commands that C++ offers.

  2. As long as the statement evaluates to true the while loop will continue. It will only cease looping once the expression evaluates to false.

  3. And infinite loop is one where there is never a possibility for the loop to cease looping. This ceasing can be done by a “return statement, a break statement, an exit statement, a goto statement, an exception being thrown, or the user killing the program”. Otherwise it will keep looping forever in theory, though in reality it will go until the program or your computer crashes haha

  4. An iteration is every instance of looping that happens inside of a loop as a whole.

Article 2:

  1. For loops are more compact then while loops, but they also allow for specifying exactly how many iterations of looping should be done unlike a while loop.

  2. for (int variable { value }; variable <, >, or = a number; additional parameter)

  3. An off-by-one error is “when the loop iterates one too many or one too few times”.

1 Like

1. What can we do with the while statement?

A while statements is like an if statement with loops. If a certain condition is satisfied in a while statement, it runs all the commands in the curly braces of the while statement, otherwise the loop breaks.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?

The value must be true in order to continue the loop.

3. What is an infinite loop?

Infinite loop has no break condition in order to break the loop.

4. What is an iteration?

A single loop.

1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?

Is a more compact form of a loop since the initial condition, end condition and increment of the counter variable is included together. It is better to use the for loop if we know the exact number of times we want to iterate, and we don’t want the counter variable to be used outside the for loop.

2. How would you write a for-loop in code?

for (int counter=0;counter<10;++counter){
/*
statements inside the for loop
*/
}

3. What is an off-by-one error?

When a loop makes an extra iteration than intended. This usually happens when initial value of the counter is set to 0 and the end condition is less than or equal to n. This results in n+1 iterations.

1 Like

Part 1

  1. What can we do with the while statement?
    We can loop through code repeating statements until a condition is met.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    As long as the condition is true, the loop will continue.

  3. What is an infinite loop?
    A loop where the trigger condition is never altered, so it never ends the while.

  4. What is an iteration?
    Each loop is considered an iteration.

Part 2

  1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    For loops are beneficial when you know the ending value for a loop. Additionally, while loops can be run at least once before satisfying a trigger condition.

  2. How would you write a for-loop in code?

`//for loop

for (int myloop = 0; count >=10; count ++) {
cout << "looping " << count << " times." <<endl;
}`
  1. What is an off-by-one error?
    When the loop executes one extra iteration or one too few times.
1 Like
  1. What can we do with the while statement?
    Repeat a piece of code while a certain condition is met.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    The statement in parenthesis must evaluate to true.

  3. What is an infinite loop?
    A loop that executes forever.

  4. What is an iteration?
    A single execution of the code inside a loop.


  1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    When having to iterate over a known range of values.

  2. How would you write a for-loop in code?

for(int i = 0; i < 10; ++i) {
    cout << i << endl;
}
  1. What is an off-by-one error?
    An error that occurs when we iterate too many or too few times over the loop due to wrong relational operators used in the conditional expression.
1 Like

Part 1

1 - What can we do with the while statement?
We can set up a look with ‘while’

2 - What value needs the statement in the brackets evaluate to in order for the loop to continue.
The condition must evaluate to true for the loop to continue.

3 - What is an infinite loop?
An infinite loop is where the condition will never return false so the loop will carry on forever.

4 - What is an iteration?
One time through the loop is an interaction.

Part 2

1 - When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously)
For loops are tidier and are best when we know exactly how many loop iterations we want.

2 - How would you write a for loop in code?
for (int count{ 0 }; count < 10; ++count)
std::cout << count << ’ ';

3 - What is an off-by-one error.
A common error in for loops where the counter condition is not set correctly meaning that there are one too many or two few iterations of the loop.

2 Likes
  1. We can use it to for example initialize a number and make it count up to a decisive number before
    returning false.

  2. It needs to be less than stated in the “while” expression

  3. A loop that goes on forever and evaluates to true, uness a return statement or break statement is
    implemented.

  4. When a loop executes (using the while expression).

  5. When you know how many times you need to execute the code. It is a more effective and
    compact way of writing code

  6. The program will execute: " 0 2 4 6 8 "

for (int count {0}; count < 10; count+=2)
{
cout << count << " ";
}

  1. When you write the code wrong so the loop executes one too many or one too few times.
1 Like

While statements

  1. What can we do with the while statement?
    Repeat a process until a condition evaluates to false.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    true

  3. What is an infinite loop?
    A loop where the condition is always true.

  4. What is an iteration?
    Each time a loop executes is one iteration.

For statements

  1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    When the number of iterations is known.

  2. How would you write a for-loop in code?
    A for loop with 10 iterations
    for(int iter = 0; iter < 10: iter++)
    {
    // Do something
    }

  3. What is an off-by-one error?
    When the loop iterates one too many or one too few times.

1 Like

1. What can we do with the while statement?
Run a loop until the condition becomes false
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
true
3. What is an infinite loop?
A loop that is always true and executes for ever
4. What is an iteration?
Every time a loop executes is iteration

  1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    When the number of iterations is known.
  2. How would you write a for-loop in code?
    for (int count{ 0 }; count < 10; ++count)
    std::cout << count << ’ ';
  3. What is an off-by-one error?
    When the loop iterates one too many or one too few times.
1 Like

While Statements

  1. While statements allow us to repeat a block of code as long as a preset condition returns true.
  2. The value needs to return true, or not zero.
  3. An infinite loop has a conditional statement that will always return true, and therefore repeats forever.
  4. An iteration is a run through of the blocked code in a loop.

For loops

  1. For loops are more compact and do essentially the same thing.
  2. for(init; conditional statement; end expression){
    code to iterate
    }
  3. Off-by-one errors are problems where a loop will iterate once more or once less than intended, generally caused by misused operators (e.g. < instead of <= or vice versa).
1 Like

1. What can we do with the while statement?

The while is the simplest loop statement.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?

The loop will execute the expression in repeat until the false statemen which will exit the loop.

3. What is an infinite loop?

An infinite loop is a loop which never terminates.

4. What is an iteration?

It means - each time a loop executes.

1. When is it a good idea to use a for statement (do a for loop) instead of while loop we learned previously?

For loop is ideal when we know exactly how many times we need to repeat.

2. How would you write a for-loop in code?

for (it count{ 0 }; count , < 10; ++count)

cout << count << ’ ';

3. What is an off-by-one error?

occur when the loop repeats one too many or one too few times.

1 Like
  1. We can execute an operation multiple times
  2. The statement has to be “true”
    3 It’s a loop that is written the way that the statement in the brackets is always “true”
    4 Executing a loop once is called iteration.

1 “for” loop is better when you know the number of iterations

  1. for (int count{ 0 }; count < 10; ++count)
    std::cout << count << ’ ';

  2. it’s when your program is running but giving you a wrong result which makes it harder to figure out your mistakes.

1 Like

Part I

  1. We can create the simplest of the four C++ loops.
  2. The statement is repeated as long as the value is true.
  3. A loop that has no programmed ending and repeats to infinity, i.e. the checked value always remains true.
  4. Each specific execution of the loop.

Part II

  1. For loop is better when we know how many times exactly we want to iterate. We use a while loop when the number of iterations is unclear or not in specific time.

    for(int n=0; n<100; ++n) {
    cout << n << endl;
    }
  1. Off-by-one error is when a loop iterates one too many or one too few times. It happens when a programmer makes a mistake in forming the correct logical relations within the logic (eg. > instead of >=).
1 Like

1, The code that a while-statement comprises is executed repeatedly for as long as the while-argument is true.
2. The statement in the argument must have the logical value ‘true’.
3. An infinite loop is one that is repeated indefinitely (i.e., one for which the while-argument never becomes false).
4. An iteration is a single execution of the instructions that a (while-)loop comprises.
5. A for-loop can be used when the number of loop iterations is a fixed given value.
6. I would use the syntax for (int count{ n}; count < m; count++) (or count-- or count += k or count -= k)
7. In the case of an off-by-one-error the the number of loop iterations is off by positive or negative one.

1 Like

Part 1:

  1. The while function is a loop function. You can use it to loop a certain function.
  2. True
  3. An infinite loop is a loop that never achieves the conditions. This is becuase the value always result in true.
  4. An iteration is one completed loop. You can say that a loop did 5 iterations, and thereby meaning that it ran through the loop five times, with values based on previous iterations.

Part 2:

  1. For loop is used when the numbers of iterations is known beforehand.
  2. For(int i=0; i<5; ++i)]{
    Cout<<i<<endl;
    }
  3. A looping error that results in a loop that does one iteration too much or one interation too short.
1 Like