Loops in C++ - Reading Assignment

Part One

  1. We can use the while statement to evaluate an expression.
  2. In order for the loop to continue, the statement in the brackets needs to evaluate to true.
  3. An infinite loop is a loop in which the while statement executes forever because the expression always evaluates to be true.
  4. An iteration is the name for each time a loop executes.

Part Two

  1. It is a good idea to use a for statement to initialize, define, or change the value of loop variables after each iteration.
  2. You would write a for-loop in code as the following:
init-statement;
while (condition-expression)
{
statement;
end expression;
}
}
  1. An off-by-one error occurs when the loop iterates one too many or one too few times.

Thanks!

1 Like

I would use a for loop when I know how many iterations I want to make. :slight_smile:

this is a while loop :slight_smile: also it only takes a condition as a parameter. It can have an expression as a parameter though

1 Like
  1. We can execute particular block of code multiple times until certain condition is met or stops being met.

  2. True (1).

  3. A loop that never ends (in reality - it usually ends when it reaches some memory limit or security edge).

  4. Single execution of the looped code.

  5. When we know exactly how many iterations we gonna have. While loop is better for iterating until a condition is met. For example I know that I will become a millionaire in 2 years, so:

  6. for (month = 0; month < 24; month++) { grind; }
    cout << “You are millionaire!”;
    but since I don’t know when I’m gonna be a billionaire:
    while (me != billionaire) { grind; }
    cout << “You are billionaire!”;

  7. It happens when the developer miscalculates the number of iterations that his/her code will run and the loop goes one too many or one too few times.

1 Like

True in C++ is actually considered as any non zero value, not just 1 :slight_smile:

lol :joy: :rofl: :rofl:

1 Like

Oh, is it a bool comparison like JS then? 0 = no value = false /// 5 = value = true?

What can we do with the while statement?

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

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

Any true value

What is an infinite loop?

if the expression always evaluates to true, the while loop will execute forever

What is an iteration?

Each time a loop executes, it is called an iteration


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.

How would you write a for-loop in code?

// returns the value nBase ^ nExp
int pow(int base, int exponent)
{
int total{ 1 };

for (int count{ 0 }; count < exponent; ++count)
    total *= base;

return total;

}

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

Yes, 0 is false and anything else is true :slight_smile:

2 Likes
  1. We can loop until a certain condition is met. This is for situations when the number of iterations can vary between runs.

  2. True

  3. Infinite loops are loops that run indefinitely until manually stopped. They have no end or methods for breaking. They often result in program crash.

  4. An iteration is an individual loop cycle.


  1. For loops are used when the number of loop iterations are known.

  2. for(initializing statement; condition statement; end statement)

  3. An off-by-one error occurs when the results presented by a loop is off by 1, usually due to an error in the relational operator in the conditional statement.

1 Like

1. What can we do with the while statement? // Repeatedly run a block of code while specified conditions are true.

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

3. What is an infinite loop? // An expression that always evaluates true therefore runs continuously.

4. What is an iteration? // An iteration is a block of code that is run once and repeats this iteration a specified number of times 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? // A for loop is used when the number of times to run a loop is known.

2. How would you write a for-loop in code? // for (int counter = 0; counter < 10; counter++)
// for (init-statement; condition-expression; end-expression) {statements}

3. What is an off-by-one error?
// occur when the loop iterates one too many or one too few times, typically because of a mistake in conditional expressions (< instead of <=)…

1 Like

<<Article 1>>

  1. What can we do with the while statement?
    -> It enables to write conditional functions like if else.

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

  3. What is an infinite loop?
    -> It is a loop that never ends. The condition to finish the loop does not have an end.
    It is always true.

  4. What is an iteration?
    -> Go through the process over and over.
    Each time a loop executes, it is called an iteration .

<<Article 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 we know how many times we want to iterate.

  2. How would you write a for-loop in code?
    -> set a counter and use it to determine how many times you want to do the iteration and when to stop.
    for (int count{ 0 }; count < 10; ++count)
    cout << count << ’ ';

  3. What is an off-by-one error?
    -> The iteration does not happen as many times as intended due to the miss in the conditional statement. N times become N-1 times if you forgot to add = to the conditional.

1 Like

Part I

  1. With a while statement we can create loops.

  2. The statement within a while loop will continue to execute so long as the expression in parenthesis evaluates to true (non-zero).

  3. An infinite loop occurs when the expression in the while loop always evaluates to true. The statement therein executes continuously. Infinite loops can be terminated with a return, break, or exit statement.

  4. An iteration is one complete loop cycle. By declaring a counter variable outside the loop, we can keep track of the number of iterations we want our loop to make.

Part 2

  1. The key difference between a while loop and a for loop is that a while loop makes use of a persisting loop variable, that is, it leaves us a value after it performs that tells us something about the performance, such as how many iterations occurred. For loops are used typically when the # of iterations to be performed is known ahead of time, when we want the loop variable to be destroyed on termination.

  2. For(initial statement; conditional expression; end expression)
    {
    statement;
    }

  3. An off-by-one error happens when a loop’s conditional expression fails to evaluate the intended # of times and instead iterates one more or one less than intended. This happens because the programmer used the wrong symbol (I.e. ‘>’ vs. ‘=>’) in the conditional expression. Compilers will run the code and the program will give a different result.

1 Like
  1. While statement allow us to execute block of code repeatedly and stop if specified condition is evaluated to false.
  2. The loop will continue to execute as long as it’s conditional expression is evaluated to true.
  3. If a conditional expression is always evaluated to true, our loop can theoretically run forever and it is called the infinite loop.
  4. An iteration is a single execution of block code inside a loop.

Second part:

  1. A for loop might be better than a while loop when we know exactly how many times we wish to execute our code.
for (int i = 0; i <= 123; i++)
{
//do some crazy stuff
}
  1. An off-by-one error occurs when loop execute code one too many or one to few times. It is often hard to notice because it may not cause any error. It is usually cause by a mistake in conditional expressions.
1 Like

PART ONE

  1. What can we do with the while statement?
    You can create loops. This means that commands within the while statement are executed while the expression evaluates to true.

  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 is a loop where the expression always evaluates to true so the loop never ends, executing for ever.

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

PART TWO

  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 exactly how many times you need it to execute (iterate).

  2. How would you write a for-loop in code?
    In C++:

for (int i{ 0 }; i < 10; ++i) {/* do some stuff here */}

  1. What is an off-by-one error?
    These happen when the loop executes either one too many or one too few times.
    Most commonly caused by the wrong relational operator in the conditional expression, such as > instead of >=.
    Always test your loop with known values to make sure they work as expected. Test, test, test… usually with inputs that cause it to iterate 0,1 and 2 times.
1 Like
  1. With the while statements we can do loops

  2. The value needs to be true

  3. Infinite loop is when the expression in the brackets is always true.

  4. Iteration is the number of repetitions the loop will be exectued.

  5. When we know exactly how many times we need the loop to iteriate.

 for(initial-statement; condition expresion; end-expression)
     statement
  1. When the number of iterations os one to many or one to less
1 Like
  1. loop a piece of code while certain conditions are true
  2. the statement needs the value ‘true’
  3. a reccuring piece of code that will always output a suitable value ’ true’
  4. an iteration is one pass through the loop of some code
  5. when the number of iterations to be performed is known in advance
  6. for( int i=0; i<=10; i++){ statement }
  7. a common mistake, happens when iterating one to many or too few times, usually for not using correct conditional operators
1 Like

1 - When a while statement is executed, the expression is evaluated. If the expression evaluates to true (non-zero), the statement executes. once the statement has finished executing, control returns to the top of the while statement and the process is repeated
2 - If the expression evaluates to true (non-zero), the statement executes
3 - If the expression always evaluates to true, the while loop will execute forever.
4 - Each time a loop executes

1 - 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 - for (int count{ 0 }; count < 10; ++count)
std::cout << count << ’ ';
3- Off-by-one errors occur when the loop iterates one too many or one too few times.

1 Like

We can repeat code.

True.

It is a loop that does not have a counter variable. It loops forever, until it is stopped by some outside force.

An iteration is one execution of an entire loop. If it loops again it is a second iteration, and so forth.

When you know the amount of times you want the loop to run.

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

For instance:

for(int count{1}; count < 20; ++count){
// Code to loop;
}

It is when programmers use the wrong conditional expression. It can cause the code to run one time too many or too few.

1 Like
  1. as long as the while expression is true, the code in the statement is run again until not true. This creates a loop.
  2. the while expression
  3. a loop that is never stopped, because the while expression is always true
  4. One run of the code in a while loop

1 When it is exactly known how many time we want to loop.
2 e.g. for (int count{ 0 }; count < 10; ++count)
std::cout << count << ’ ';
3 if you code the loop in such a way that it either runs one time to many or one time to little.

1 Like

What can we do with the while statement?

A while loop evaluates the condition and if it’s true we can execute our code until the condition is false.

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

True.

What is an infinite loop?

It’s a loop which repeats indefinitely, the code’s function do not have a functional exit.

What is an iteration?

While, do-while and for iterates the loop until the expression evaluates to zero(false).

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

If the developer want to have a specific time the loop iterate for loop is the option, if the developer want to iterate the loop during a undefined time while loop is the the option.

How would you write a for-loop in code?

For(int = 0; i<10; i++){

Cout << i << endl;

}

What is an off-by-one error?

It is a logical error in the code and occurs when the condition is i.e: > instead of >=

1 Like

1 As long as (while) the conditions are met, repeat (iterate). This is what we call a loop.
2 The expression is a Boolean, to keep the loop going it must be true.
3 For some reason doesn’t the counter reach the end, the condition of the (while or for) statement is always true.
4 An iteration is when you repeat the same instructions again. Every loop is an iteration.

1 FOR is written in a more compact syntax, if you know the number of iterations the for loop is more convinient.
2 for (int count{ 0 }; count < 10; ++count)
// instruction to be iterated 10 times, as the dummy begin at 0, repeat until dummy = 9, add 1 to dummy.
3 When the loop iterates one time too many or one time too few.

1 Like