Loops in C++ - Reading Assignment

1.1. What can we do with the while statement?
Loop a sequence of code until specific conditions are met.

1.2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
While true, keep going.

1.3. What is an infinite loop?
A loop gets executed for ever. A loop from which we never exit and the code execution is stuck in it.

1.4. What is an iteration?
One lap of the loop.

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 you know the exact number of iterations of a loop is required to execute a desired task.

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

2.3. What is an off-by-one error?
It’s when the loop iterates one too many or one too few times. Often a beginner mistake.

1 Like

Part 1:

What can we do with the while statement?

  • We can execute code while some condition is true.

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

  • True

What is an infinite loop?

  • A loop whose logic will never trigger a failure.

What is an iteration?

  • One pass through a loop of code.

Part 2:

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 good to use a for loop if the number of iterations is known. A while loop is good if a condition needs to be met in order to execute code.

How would you write a for-loop in code?

int i = 0;
	
for (i=0;i<=10;i++){
	cout << "Loop it!"<< endl;
}

What is an off-by-one error?

  • These are mistakes often made by new programmers which iterate through a loop an incorrect amount of times, namely off-by-one. Check the conditional operators (<,>,<=,>=,etc)
2 Likes

Questions: While Statements

  1. What can we do with the while statement? - loops can be created

  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? - this type of loop will always be true and therefore never terminate.

  4. What is an iteration? - teach time a loop executes is 1 iteration

Questions: 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 you know how many times the loop needs 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? - occurs when the loop iterates one to many or one few times.

1 Like

Part 1

  1. We can build the programs that verify if the expression inside the brackets is true. If yes, the part of code inside the while loop is executed repeatedly until the expression equals false.
  2. The value needs to evaluate to true.
  3. The infinite loop is the loop that executes forever, due to the fact that the expression always evaluates to true.
  4. An iteration is an event that occurs each time the loop executes.

Part 2

  1. The for statement should be used when we know exactly how many times to iterate through the loop.
 for (int i = 0; i < 10; i++) {
     court << i << endl;
    }
  1. Off-by-one error is a type of error that occurs when the loop iterates one too many or one too few times.
1 Like

What can we do with the while statement?
We can create a while loop.

What value needs the statement in the brackets evaluate to in order for the loop to continue?
it will need to be a true value.

What is an infinite loop?
It when the loop never break/quit

What is an iteration?
Its the amount of times a loop get executed.

When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previosly?
If I know the number of iterations I would use a for loop.

How would you write a for loop in code?

for (int i = 0; i < 5; i++)
  cout << "i is " << i << endl; 
return 0;

What is an off-by-one error?
it means you have one iteration to much or one iteration to few.

1 Like

First Article

  1. While loops allow to execute code until a certain condition is verified (meaning, until the condition within parentheses is true, or equal to 1).
  2. It has to be true, so “non-zero”.
  3. It is a while loop for which there is no change in the condition to verify (so it is and always will be true). The only way to exit an infinite loop is to insert some kind of clause to terminate, exit or break it.
  4. Each single time a loop is executed.

Second Article

  1. When the number of times the loop has to be repetead is known at creation. It allows to better manage the loop variables.
  2. for (initial statement; condition expression; ending expression)
    – code to be executed until the condition expression is valid;
  3. It is a quite common mistake which occurs when programming with loops. It depends on the fact that sometimes the programmer could make some mistake in the definition of conditions (such as the wrong use of signs like <,>,=) causing the code to execute one time more/less than needed.
1 Like

First section:

  1. With a while statement you can loop. That means you can execute a task/operation several times if a certain expression which is inside the () is evaluated as true.

  2. the statetments needs to be true

  3. An infinite loop is a loop where the statement is always true. That means the program will never stop working and always do whatever is defined in the block forever.

  4. Whenever a loop is executed (run through the whole block/body), it is called an iteration.

Second section:

  1. For loops can do loops in a very compact way. Another important thing is that the init statement only lives inside the for loop and is then “destroyed”, whereas the init statement of a while loop is defined outside the loop and is not destroyed. You should also use for loops when you know exactly how many times the loop should iterate.

  2. for(int i=0; i<=20; i++ { doSomethingFor20Times()
    }

  3. An off-by-one error occurs when a for loop (or a while loop) iterates one to many or one to fee times. Mostly the wrong expression inside the conditional expression (e. g. > insteand of >=) leads to this result. But also a wrong definition of the counter variable (e.g. int i=0 instead of i=1)

1 Like

I have a question regard the Little Game Project - Solution of @Ivan. In his solution he uses while{true} and a break statement to finish the while loop. Somebody told me that using break to interrupt or end an a while loop is not a good programming style. Is this correct? In my solution I used a bool variable called guessed_correct which is initialized with false and my while loop looks like: while (!guessed_correct){}. If the player guessed correctly the while loop ends because I changed the variable guessed_correct to true. Which solution is better and why? Maybe the person who told me that using break to end a loop is bad was wrong. Thank you for your answers and opinions.

1 Like

1. What can we do with the while statement?
We can loop over a set of commands several times until a specific condition has been met.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
This needs to evaluate to be true for the loop to cvontinue evaluating. When it evaluates to false the loop will be terminated.

3. What is an infinite loop?
This is a loop which will always evaluate to be true.

4. What is an iteration?
This is one pass through the list of commands within the 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?
In general for loops are better when the number of iterations is known. They are syntaxically more compact.

2. How would you write a for-loop in code?
for (int count{ initial value }; count conditional; count iteration)

3. What is an off-by-one error?
This occur typically where the for loop repeats the loop one extra time than intended. This may occur because the for loop is still evaluating true in the conditional statement.

1 Like

Answer:

    1. With while statement, we can create loops. It will allow us to execute a block of code (statement) until an expression of the loop returns falls. After that, the control will exit the while block.
    1. For the loop to continue, the value in the brackets has to evaluate to true.
    1. Infinite loop is a state, when the program runs without end. On a code level, the block of code executes again and again without stopping.
    1. Iteration happens each time the loop executes.
1 Like

Part I

  1. With a while statement you can loop an expression until it becomes true.

  2. The value needed in order for the loop to continue is true.

  3. An infinite loop is when a while loop executes forever because the expression is always true.

  4. An iteration is each time a loops executes.

Part II

  1. It is a good idea to use a for loop when you know exactly how many iterations you need and gives you the ability to change the values after each iteration.

  2. for (int count{ 0 }; count < 10; ++count)

  3. An off-by-one error is when the loop iterates one too many or one too few times, it usually happens when the wrong relational operator is used in the conditional expression.

1 Like

Answer:

    1. It is a good idea to use for loop when we know precisely how many iterations of the block we want inside the loop. Because it allows us easily define, initialize, and change the value of loop variables after each iteration.
    1. for ( initializing statement ; exit statement ; increment or decrement step) {
      body of the loop
      }
    1. Off-by-one error happens when the loop executes one too many times or one too few times. It happens because of the wrong relational-expressions ( > vs.>= ). To deal with this error, always test your loops with known values that will iterate 0,1 and 2 times.
1 Like

1)What can we do with the while statement?
Execute code in a loop

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 while loop that does not stop executing because the conditional
statement never becomes false

4)What is an iteration?
one 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 know how many times you want a loop to execute.

6)How would you write a for-loop in code?
for(int count{ 0 }; count < 10; count++){
code to execute;
}

7)What is an off-by-one error?
When a loop iterates one too many, or too few, times

1 Like

1. What can we do with the while statement?
While is used to execute something as long the statement is true.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
True value.
3. What is an infinite loop?
An infinite loop runs forever
4. What is an iteration?
Iteration takes place every time a loop is executes.
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’s ideal to use for loop when we know exactly how many times we need to iterate.
2. How would you write a for-loop in code?
for (initial statement; condition; end expression) code to be executed
3. What is an off-by-one error?
Off-by-one error occurs when the loop iterates too many or too few times.
Eg. Operator < is used instead of <=.

1 Like

Hello sir, not entirely true, that depends on the programming style of the programmer, ending a while loop with a break {true} is totally valid at some points. Why? because the while loop could continue even if the condition is met, so you can use a break to end the loop when you met the condition that you are looking for.

Hope this gives you a clear view of the subject, keep learning! :slight_smile:

Carlos Z.

  1. Gives us the ability to perform a task until a goal is met.

  2. True

  3. An infinite loop that will never stop running because it will never evaluate to stop running.

  4. An iteration is code that repeats for a certain number of times, those number of times are refer to as iterations.

  5. For loops are used for when you know how many iterations you need, while loops are used when that need may vary.

  6. for (counter {0}; counter < 10; ++counter) {
    cout << counter << ’ ';
    }

  7. When loops have near misses of being either one iteration too long or too short.

1 Like

1. What can we do with the while statement? While statements can be used to repeatedly execute a block of code while the provided statement is true.

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.

3. What is an infinite loop? An infinite loop is a piece of code that lacks a functional exit so that it repeats indefinitely.

4. What is an iteration? Each repetition of the statement inside a loop.

1 Like

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 (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?
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. This generally happens because the wrong relational operator is used in the conditional-expression (eg. > instead of >=).

1 Like
  1. We can do loops.
  2. True.
  3. When programme never ‘stops’ because the expression in the brackets is always true.
  4. One cycle of the loop.

  1. Good practice, but in general you use it if you need it in one go and when you know how many times/cycles you will have.
  2. for(int index{start}; index /operator i.e. ‘<’/ (end); /operator i.e. ‘++’/ steps)
  3. A step is missing or one too many (i.e. when you did not account for <=).

1. What can we do with the while statement?

Indeed sir, but could you please describe it a little bit more?

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.