Loops in C++ - Reading Assignment

  1. It keep looping something until the statement in the brackets is false
  2. True
  3. Its a loop that never stops, because the testing statement always returns true
  4. one execution of the code block that loops
  5. when you know exactly how many iterations you need
  6. for(int counter = 0; counter<10; counter++){
    execution
    }
  7. The coding error that leads in a loop that does 1 iteration many or few times
1 Like
  1. What can we do with the while statement?
    execute code repeatedly while the while condition is true

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    true (non-zero)

  3. What is an infinite loop?
    executes forever because the expression always evaluates to true

  4. What is an iteration?
    Each time a loop executes, it is called an iteration

  5. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    when you know exactly how many times you need to iterate

  6. How would you write a for-loop in code?
    for (init-statement; condition-expression; end-expression)
    statement

  7. What is an off-by-one error?
    Occur when the loop iterates one too many or one too few times

1 Like
  1. A while statement can run a program a set number of times as defined by the code. It allows for repetition.

  2. A counter variable goes in the brackets. That way the program can check it each time to see if it’s still true before it decides to run again.

  3. An infinite loop is where a loop runs forever because the condition of whether it runs or not is always true.

  4. An iteration is when the program runs one time. If the program counted to four then it had four iterations.


  1. A for loop can easily set a specific number of iterations to run.

  2. (for int i = 0, i < 5, i++) – that run five times.

  3. A off-by-one error is when a program runs one too many times or either it needed to run one more time. In other words, it missed its goal by one iteration.

1 Like

Part 1

1. What can we do with the while statement?

A while statement is a type of loop that checks a condition and as long as the condition is true it will repeat any code block declared with in it.

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

The value required for the loop to continue is true. True can be deduced from an expression, can be Boolean or even integer 1 is considered true in c++.

3. What is an infinite loop?

An infinite loop is where a loop is allowed to continuously loop round and round. This happens when the condition the loop checks for never reaches false.

4. What is an iteration?

Each cycle of the loop is called an iteration of the loop.

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?

A for loop is much easier to write than a while loop and is best suited to situations when we know how many iterations are required.

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

for (int iii = 0; iii <= 20; iii += 2){ cout << iii << endl; }

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

The off by one errors is caused by miss use of operators such as < or <= etc. For example the programmer may intend to count to 5 but use iii < 5 which of course will only count to 4

Quiz 1

  1. The variable is declared inside the inner loop because we want it to be destroyed and recreated when the inner loop completes and it is called again by the outer loop. Each time it is re-declared it is set back to 1, and this gives us the desired result for our programme.

Question 2, 3 & 4

#include

using namespace std;

int main()
{
//Question 2
int iii = 97;
while (iii < (97 + 26)){
char character = char(iii);
cout << character << " " << iii << endl;
++iii;
}
cout << endl;

//Question 3
int jjj = 5;
while (jjj > 0){
    int kkk = jjj;
    while (kkk > 0){
        cout << kkk << " ";
        --kkk;
    }
    cout << endl;
    --jjj;
}
cout << endl;

//Question 4
int outer = 1;
while (outer < 6){
    int inner = 5;
    while (inner > 0){
        if (outer >= inner){
        cout << " " << inner;
        }
        else {
            cout << "  ";
        }
        --inner;
    }
    cout << endl;
    ++outer;
}

return 0;

}

Quiz 2

Quiz questions 1 & 2

#include

using namespace std;

//Question 2 function
int sumTo (int value){
int sum = 0;
for (int jjj = 0; jjj < value; ++jjj){
sum += (value - jjj);
}
return sum;
}

int main()
{
//For loop with multiple declarations
for (int aaa=0, bbb=9; aaa < 10; ++aaa, --bbb){
cout << aaa << " " << bbb << ‘\n’;
}
cout << endl;

//Question 1
for (int iii = 0; iii <= 20; iii += 2){
    cout << iii << endl;
}

cout << endl;

//Question 2 function call
cout << sumTo(5);

cout << endl;

return 0;
}

Quiz question 3

The use of unsigned int is the error here. The loop needs to reach -1 to terminate yet an unsigned int can not be a negative number. An infinite loop will result.

1 Like

1- With the while statement, we can iterate a block of code a number of times
2- The while statement will execute as long as the statement in the brackets evaluates to true
3- An infinite loop is one where the statement inside the brackets always evaluates to true
4- An iteration is a specific run of the loop statement.

1- A for loop is a good idea when we know the number of iterations we need
2- for(int count; count<10; count++)
3- An off-by-one error is when we loop one too many, or one too few times.

1 Like
  1. What can we do with the while statement?
    Keep executing until the condition is 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?
    When the expression always evaluates to true, the while loop will execute forever.

  4. What is an iteration?
    Each time a loop executes, it is called an iteration

  5. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    Using the for loop instead of the while loop when we know exactly how many times we need to iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.

  6. How would you write a for-loop in code?
    for (int count=0; count < 10; ++count)
    cout << count << " ";

  7. What is an off-by-one error?
    Off-by-one errors occur when the loop iterates one too many or one too few times. This generally happens because the wrong relational operator is used in the conditional-expression (eg. > instead of >=).

1 Like
  1. The while statement is the simplest of the four loops that C++ provides… while true… run some code

  2. True

  3. if the expression always evaluates to true, the while loop will execute forever. This is called an infinite loop .

  4. Each time a loop executes, it is called an iteration .

  5. when we know exactly how many times we need to iterate

  6. for (init-statement; condition-expression; end-expression)
    statement

  7. Off-by-one errors occur when the loop iterates one too many or one too few times.

1 Like
  1. When a while statement is executed, the expression is evaluated. If the expression evaluates to true(not zero), the statement executes.

  2. A while statement is declared using the while keyword.

  3. An infinite loop has an expression that always evaluates to true. creating a never-ending loop.

  4. Every time a loop is executed, it is known as an iteration.

  5. The for statement is ideal for when we know exactly how many times we need to iterate, it lets us easily define, initialize and change the vaue of loop variables after each iteration.

  6. for (int count=0; count < 10; ++count)
    std::cout << count << " ";

  7. off-by-one errors occur when the loop iterates one to many or one too few times.

1 Like

Part 1

What can we do with the while statement?

  • We can repeatedly execute code until a statement evaluates to false.

What value needs the statement in the brackets evaluated to in order for the loop to continue?

  • true

What is an infinite loop?

  • When the expression in the while loop always evaluates to true the loop will execute forever.

What is an iteration?

  • Each time a loop is executed is called an iteration.

Part 2

When is it a good idea to use a for statement instead of a while loop we learned previously?

  • It is ideal when you know exactly how many times we need to iterate, because it lets us easily define, initialise, and change the value of loop variables after each iteration

How would you write a for-loop in code?

  • for(int count=0; count < 10; ++count) {
    • std::cout << count << “ “;
  • }

What is an off-by-one error?

  • Off-by-one errors occur when the loop iterates one too many or one too few times.
  • This generally happens because the wrong relation operator is used in the conditional-expression (eg. > instead of >=).
  • These errors can be hard to track down because the compiler will not complain about them — the program will run fine but will produce the wrong result.
1 Like

1. What can we do with the while statement?

Ans : while has a definition which is very similar to an if statement :
while (expression){
statement; }

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

Ans : When a while statement is executed, the expression is evaluated. If the expression evaluates to true (non-zero), the statement executes.

3. What is an infinite loop?

Ans : If the expression always evaluates to true, the while loop will execute forever. This is called an infinite loop.

4. What is an iteration?

Each time a loop executes, it is called an 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?

Ans : for statement is ideal when we know exactly how many times we need to iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.

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

Ans. for (init-statement; condition-expression; end-expression){
statement; }

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

Ans. Off-by-one errors occur when the loop iterates one too many or one too few times. This generally happens because the wrong relational operator is used in the conditional-expression (eg. > instead of >=).

1 Like
  1. while statement can be use to create a linear condition that keeps running in a loop as long as the conditions evaluate to a true consequence of the decreed conditions.
  2. The statement needs to continuously evaluate to a “true” statement, until it terminates at a “false” condition.
  3. Infinite loop: when the resultant consequence of an executed condition always results into “true”. The loop will always repeat itself, hence creating an infinite condition.
  4. The process of execution of a single loop is referred to as an iteration.

  1. when is it a good idea to use a ‘for’ statement: when we know exactly how many times we have to iterate because it lets us to easily define, initialize, and change the value of the loop variables after each iteration.
  2. for (int i=0; i < 10; ++i)
  3. An off-by-one error: This occurs when the loop iterates one too many or one too few times.
1 Like
  1. You can create a statement block that repeats itself as long as a conditional statement is true.

  2. The statement must be evaluated to true

  3. A loop that never evaluates to false

  4. Each time around a loop is called an iteration

  5. It is ideal when we know how many time we want to go through the loop

  6. for ( init statement; condition expression; end expression ){ loop block }

  7. It is when a loop is executed one too many times or one too few times.

1 Like

Part 1:

  1. What can we do with the while statement?

    While statements can be used like if then statements with the added feature that once evaluation is complete, control returns to the beginning of the while statement and a loop can be formed.

  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?

    Where the expression always evaluates to True

  4. What is an iteration?

    Each time a loop executes, it is called 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?

    The for statement is used when the exact number of iterations is known.

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

    for (init-statement; condition-expression; end-expression)
    statement

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

    Off-by-one errors occur 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?
It is the simplest type of loop provided by C++. A while statement is executed while the expression evaluates to true. With a while statement we can repeat a process a pre-defined amount of times, instead of having to rewrite code for every repetition.

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

3. What is an infinite loop?
An infinite loop is one that repites indefinitely because it always evaluates to true. It’s important to not create infinite loops by mistake as this will crash the program. However, infinite loops can be created with the purpose of continuing until a certain interaction from the user takes place.

4. What is an iteration?
An iteration is a single loop execution. Every time a loop takes place is one 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?
Whenever we know the exact amount of iterations needed.

2. How would you write a for-loop in code?
for (initial-statement ; condition-expression ; end-expression)
Example:
for (int counter = 0; counter <=10; ++ counter)

3. What is an off-by-one error?
An off-by-one error is whenever the number of iterations are off by one (either too short or long), usually because the condition expression is incorrectly stated. For example, writing
counter <10
instead of
counter <=10

1 Like

1. What can we do with the while statement?
allows us to create loops - conditional statement which allow the code to execute “while” a certain condition is met

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
While statement is repeated while the expression/value is true

3. What is an infinite loop?
because the condition of the while statement can never become false, the code continues to execute infinitely because the condition remains true

4. What is an iteration?
each time a loop restarts is a new 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?

It is ideal when we know exactly how many times we need to iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.

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?

Off-by-one errors occur when the loop iterates one too many or one too few times. This generally happens because the wrong relational operator is used in the conditional-expression (eg. > instead of >=). These errors can be hard to track down because the compiler will not complain about them – the program will run fine, but it will produce the wrong result.

1 Like
  1. What can we do with the while statement?

    • create a loop to repeat a code block until the conditions are met
  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?

    • the statement within the brackets evaluates to true
  3. What is an infinite loop?

    • the statement within the brackets always evaluates to true
  4. What is an iteration?

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

    • when you already now how many times you want to loop
  6. How would you write a for-loop in code?

    • for (int x = 0; x < 100; x++)
  7. What is an off-by-one error?

    • when the number of iterations is off by one, either one over or one under. Happens with the operators are wrong.
1 Like

Part 1

  1. With the while statement, we can loop something until the statements inside the bracket is false.
  2. The value or expression in while statements need to be true to continue.
  3. An infinite loop is when a loop continues until it crashes because there is no condition put in placed to stop it. The loop is always true.
  4. An iteration is each time a loop executes.

Part 2

  1. It’s a good idea to use a for loop instead of a while loop when we know how many times we need to iterate. Use a while when we have to loop during an undefined time.
  2. for (int count=0; count < 10; ++count){
    cout<<count<<" ";
    }
  3. An off-by-one error is when you iterate one too many or one too few times. It can happens because you have a mistake in your conditionnal expression.
1 Like

1. What can we do with the while statement?
create a loop that repeats as long as the expression evaluates to true.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
The value within the bracket must be true in order to continue the loop.
3. What is an infinite loop?
if the expression always evaluates to true, the while loop will execute forever.
4. What is an iteration?
Each time a loop executes, it is called an 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 we know exactly how many times we need to execute the loop.
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?
Off-by-one errors occur when the loop iterates one too many or one too few times. This generally happens because the wrong relational operator is used in the conditional-expression (eg. > instead of >=)

1 Like
  1. What can we do with the while statement?
    Create a loop that executes until the expression 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?
    If the expression always evaluates to true, the while loop will execute forever.

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

  5. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    For statement is ideal when we know exactly how many times we need to iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.

  6. How would you write a for-loop in code?
    for (int count=0; count < 10; ++count)

    std::cout << count <<; " ";

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

1 Like

Hello @ivan and dear Community,


  1. What can we do with the while statement?

We can iterate endless over the code-block of the body of the while-loop until a specific key was pressed or condition was meet. The times of iterating depends on A) user-input B) system-variable or C) a pre-defined constant.

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

True or 1;

  1. What is an infinite loop?

A loop that have in brackets True or 1.

  1. What is an iteration?

Every round looping through the code-block is called a “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?

For-loops are suited for scenarios in which the exact number of iterations on the loop is known previously.

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

for(int jjj = 0; j <= 10; ++j) {//TODO};

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

These are logical errors, no compile errors.Executing the loop to often or to less can lead to false results. Just make sure your algorithm will work properly.


Kind regards
Kevin