Loops in C++ - Reading Assignment

A.

  1. While statement makes us able to execute loops as long as the expression is evaluated to true, because every time it executes the control goes back to the top of the while statement and then it executes again, and it will only halt once the expression is already evaluated to false, and that’s the time the loop will exit.

  2. The value needs to be true in order for the loop to continue.

  3. It is a loop that keeps executing forever because the expression that is given always evaluates to true which is any non-zero value.

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

B.

  1. It is ideal to use a for 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.

  2. for (init-statement; condition-expression; end-expression)
    statement
    example:
    for (int count{ 20 }; count >= 10; count -=3) std::cout << count << ' '; // -> 20 17 14 11

  3. Off-by-one errors occur when the loop iterates one too many or one too few times. This happens because of putting wrong relational operator in the conditional expression for example by using > instead of >=.

1 Like

Reading Assignment: Loops in C++ (Answers)

Pt.1
1.Loop expression in code.
2. true boolean
3. A loop that execute forever.
4. each time a loop executes.

Pt 2.

  1. When we know how many times to iterate a loop.
  2. for(init-statement; condition-expression; end-expression)
    statement.
  3. When a loop iterates on too many or one too few times.
    :space_invader:
1 Like
  1. What can we do with the while statement?
    Ans: We can execute a particular code while a certain expression holds true.

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

  3. What is an infinite loop?
    Ans: If the expression besides the while keyword in parentheses always evaluates to true, the while loop will execute forever. This is called an infinite loop.

  4. What is an iteration?
    Ans: 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?
    Ans: The for statement (also called a for loop) 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
    A good example is:
    for (int count{ 0 }; count < 10; ++count)
    std::cout << count << ’ ';

  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 >=). 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

Part 1

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

  2. Value needs to be non-zero and true

  3. Infinite loops are while statements when expression always evaluates to true

  4. Iteration is an each time when loop executes

Part 2

  1. The 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. for (init-statement; condition-expression; end-expression)
    statement

  3. 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 statements allow us to execute a particular statement/function while a certain condition is continuously being met (for example, while a counter is below a certain value, or while some boolean is true).
  2. The statement evaluates the condition within the parentheses following the word “while”.
  3. An infinite loop occurs when a condition inside the parenthses always evaluates to true. The simplest example would be to simply write while(true).
  4. Iteration refers to the repetition of a particular function/statement in a loop.
  5. For loops are a good idea when we know exactly how many times we’d like to loop through something, as well as when we know exactly what point to stop and end at. It’s also useful when we want to vary the incrementation of a counter in our loop.
  6. for (int x{0}; x <= 10; x++)
  7. Off by one errors often occur when a developer accidentally sets a counter condition to < instead of <= (or vice versa), leading the loop to run one extra or one too few times.
1 Like

1.1 We can loop under a condition. It’s easy to permaloop this way tough.
1.2. It needs to be a true statement.
1.3 A loop with an always true statement.
1.4 An iteration is what we call one loop cycle.

2.1 When we have sufficient data and we know how many times we want to loop.
2.2 for (int z {1}; z < 10; z++}
{ cout >> z;}
2.3 an off by one is some type of error that beginners seem to have where they have trouble with the condition expression. since counting in coding generally starts from 0, it is a common mistake to forget this and accidentally iterate a loop 1 more time than needed (or 1 less )

2 Likes

FIRST PART

  1. What can we do with the while statement? We can declare the easiest of the four kind of loop in C++. It is very similar to the IF condition, with the difference that at the end control returns to the begin to re-execute the operation as per loop declaration.
  2. What value needs the statement in the brackets evaluate to in order for the loop to continue? The statement in the brackets must be true. The loop will continue until the statement is true. If the statement is not true since the beginning the loop will not start at all.
  3. What is an infinite loop? An infinite loop will continue forever, and the cause is that the stated condition of the loop will never become false.
  4. What is an iteration? A single execution of the loop is called iteraction.

SECOND PART
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 iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.

  1. How would you write a for-loop in code? for (init-statement; condition-expression; end-expression) statement
  2. What is an off-by-one error? An off-by-one error occurs, as the word itself says, 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.
2 Likes

Ans 1:
Like an ‘if’ statement, the ‘while’ statement is able to create a loop in C++ code language.

Ans 2:
The value that needs to be returned must be ‘true’ for the loop to continue executing.

Ans 3:
An infinite loop is a condition that continues to prove true and thus, it will continue to execute without ending infinitely.

Ans 4:
An iteration is the next re-cycle of the call being executed. Thus a loop is operating. If the condition continues to prove true, for this next call/iteration, after its previous cycle(s) completion, the call will execute, otherwise it will exit the loop and complete the program runtime.

Ans 1:
When you wish to make the code writing simply more concise, easy to read and compact. And when the operation is still going to produce the same outcome.

Ans 2:
If we are counting from 1 up to 10, then a for loop can be written to be implemented. (eg.) composition of a basic for loop is: ‘for (initial statement; conditional expression; end expression) statement.’

  1. So first, using the for loop statement, we first declare a loop variable named ‘count’ and initialise it with the value o. This is the first parameter within the parenthesis.
  2. The second parameter within the for loop parenthesis, after the semi-colon is: ‘count < 10.’ Count was declared as 0 in the initial statement. So while this second conditional expression remains true, then this expression will continue to be executed.
  3. Then the third and final ‘end expression’ which is the final parameter within the parenthesis. This is the ‘++count’ end expression. This expression iterates numerically up by 1 upon each loop to the second conditional expression.
    NOTE: This second conditional expression will only continue to execute whilst it remains true, otherwise it exits and ends the programs ‘loop’ execution.

Ans 3:
The term ‘off-by-one-error’ is the term used when the program, that is written, completes its iterations one too many and/or too few times. This generally happens when the incorrect and/or incomplete ‘relational operator’ has been selected for the secondary ‘for’ loop conditional expression.

1 Like

Part 1.

  1. It can be used like an if statement where it runs if it meets the true/false requirements.
  2. The value has to be true or a non zero.
  3. Infinite loop is where the expression is always true.
  4. Iteration is how many times the loop is executed.

Part 2.

  1. When you know exactly how many times to iterate
  2. for (size_t i = 0; i < count; i++) { /* code */ }
  3. Off by one error is when the loop iterates one too many or too few times. Usually occurs when the relational operator is wrong.
1 Like

5.5
1. What can we do with the while statement? We can execute a statement a number of times. If the expression is true then execute but of the expression is false then we finish.
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?
It is a loop that never finishes.
4. What is an iteration? Each time a loop executes is an iteration.

5.7
1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously? If we know how many times we want to iterate.
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? It happens when the loop iterates one too many or one too few times. eg. Using > instead of >= etc

1 Like
  1. With a while statement you are able to have a piece of code continually run until the conditions that you specified are met. This is very valuable to help prevent code having to write the same line of code a bunch of times and helps things be cleaner in the case of fixing any bugs.

  2. The statement in the brackets needs to evaluate to true in order for the loop to continue. Once the loop evaluates to false, the loop will stop.

  3. An infinite loop is a loop that will constantly evaluate to true and/or does not have any conditions to make the code inside evaluate to false.

  4. An iteration is a single or specific instance of a loop running.

  1. A for loop is recommended to be used when you know how many iterations are needed for the loop to complete.

  2. You initialize the variable that is going to be iterated over. This will define the starting point for the loop. You then specify the parameter for which the loop will continue to iterate. You lastly specify how the loop will incrementally move up or down.
    for (int counter {1}, counter < 10, ++counter)

  3. An off-by-one error is a common error that can occur when the loop iterates one too many times or one too little times than intended. This can easily happen due to a small error in the relational operator in conditional expression. Ex. using < instead of <= when specifying the conditions that will continue the loop to run or finished.

1 Like

a1. We can run a looping series of instructions while a certain specified condition returns true.
a2. true
a3. a loop whose specified condition has been coded in such a way that it will never return false, thus the loop keeps running (if intentional, there is typically an in-loop break condition that the infinite loop is waiting for).
a4. A single run through the loop’s instructions.

b1.the for loop integrates iteration and initialization of the counter into the first line of the expression; this is faster to read and code.
b2. for ( variable initialization; condition tested; operation on counter){statements, with maybe a break condition}
b3. where the last or first counter value is off by one from planned because of a coding error, leading to unpredictable and possibly undefined behaviour.

1 Like
  1. What can we do with the while statement?
    We use while statement to loop through statement of a code.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    The code will execute while the expression in brackets remains true

  3. What is an infinite loop?
    Infinite loop is a loop thats doesn’t end. Loop code can only be stop canceling the code execution.

  4. What is an iteration?
    Iterations is the number of time a loop is executed

  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 we know how many time we need to iterate.

  6. How would you write a for-loop in code?
    for(int count = 0; count<20; count++){
    cout << count << endl;
    }

  7. What is an off-by-one error?
    When you iterate at least once too many

2 Likes

You missed the questions about for loops. :slight_smile:

1 Like

FIRST PART

  1. Once the statement has finished executing, control returns to the top of the while statement and the process is repeated.

  2. True.

  3. If the expression always evaluates to true, the while loop will execute forever.

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

SECOND PART

  1. For statements is preferred when we have an obvious loop variable, because it lets us easily and concisely define, initialize, test and change the value of loop variables.

  2. #include
    int main () {
    for (int count = 0; count <=10; count ++){
    cout<<count<<endl;
    return 0;
    }

  3. An off-by-one error occurs when the loop iterates one to many times or one too few times to produce the desired result.

2 Likes
  1. With a while statement, we can loop over/execute a block of code multiple times, as long as a defined expression evaluates to true.
  2. True.
  3. When the conditional expression always evaluates to true i.e. the loop will never end and the code will be executed over and over until the program is forcefully exited.
  4. An iteration refers to each time a loop executes all the code in its block.
  5. When we know exactly how many times we need to iterate.
for (init-statement; condition-expression; end-expression)
   statement;
  1. When a loop iterates one too many or too few times.
1 Like
  1. A while statement lets you loop a set of instructions until a condition is met
  2. True
  3. An infinite loop is a loop that always executes over and over, never ending
  4. An iteration is one full execution of the loop
  5. We use for loops when we know how many times the loop needs to be executed
  6. for (init-statement; condition; end-expression) statement
    7)Off by one occurs when the loop iterates too many or too few times
1 Like

Loops in C++ :wink:

  1. Programmers use while statement to run loop.

  2. If the condition evaluates to true , the associated statement executes.

  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.

**When, How & What

  1. Best practice :pinching_hand:

Prefer for loops over while loops when there is an obvious loop variable.
Prefer while loops over for loops when there is no obvious loop variable.

  1. for (int i = 0; i < 5; i++) {
    cout << i << “\n”;
    }

  2. Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result. :woman_shrugging:t4:

1 Like
  1. While statements create loops.

  2. true.

  3. If the expression in the loop always evaluates to true this creates an infinite loop.

  4. An iteration is every time a loop executes.

  5. It is better to use for loop than while loop when you know how many times you want to iterate.

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

  7. Off by one errors occur when the loop iteratres one too many or one too few times. This can occur through the use of wrong operational operator within the condition.

1 Like

1 -> With the while statement we got the possibility to loop trough a statement until the condition is true
2 -> The intialized variable that is inside the condition, if you see the condition as a value it is the condition as a whole that has to be evaluate
3 -> A infinite loop is a loop whit out the possibility to breakout of the loop
4 -> Each time a loop executes, it is called an iteration

1 -> The for loop is preferred when we have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.
2 -> for (int count{ 1 }; count <= 10; ++count)
std::cout << count << ’ ';
3 -> Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result.

1 Like