Loops in C++ - Reading Assignment

  1. Using a while statement, we can execute a block of code to repeat as many times as we want.
  2. As long as the statement in brackets is true, the loop will continue.
  3. An infinite loop occurs when the while(condition) is always true and cannot be made false.
  4. Each execution of a loop is an iteration.

  1. When we want to set a precise number of iterations, it is better to use a for-loop instead of a while-loop.
  2. for (int count=0;count<=10;++count) {};
  3. When a loop executes one too many or one two few iterations.
1 Like
  1. What can we do with the while statement?
    With the while statement you can set a condition and a set of instructions to iterate until the condition is met. Does not use counter.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    Loop continues until the condition is met.

  3. What is an infinite loop?
    An infinite loop means the condition is never met. So the loop will continue

  4. What is an iteration?
    An iteration is one loop of execute instructions one time.

At the bottom of the article you’ll see a quiz - make sure to do the quiz!
// End of Chapter Exercise #1 Print even numbers from 0 to 20.

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

    cout<< i <<" ";

    cout<<endl;

// Exercise #2 Return sum of numbers
// The first set of instruction for the function must be applied before the main program and then it is call in the main program.

#include <iostream>

using namespace std;

// Total of sum of numbers function

int sumTotal(int max)
{
    int total{ 0 };
    for (int j{ 0 }; j <= max; ++j)
        total =total + j;

    return total;
}

int main()
{
cout<<sumTotal(20);

Read this article (http://www.learncpp.com/cpp-tutorial/57-for-statements/ ).

Think about the following questions while reading:

  1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    Use a if you dont have a fix number of iterations and use a for loop when you know how many iteration you want executed and then use a counter.

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

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

  1. What is an off-by-one error?
    This can happen if you are on or off by one iteration for several reasons like using <= . > =, instead of < or > or reverse. Or it can be by pre-designating the value of the counter by 1.
1 Like

PART I
1 & 2.) When a while statement is executed, the condition is evaluated. If the condition evaluates to true, the associated statement executes. Once the statement has finished executing, control returns to the top of the while statement and the process is repeated. This means a while statement will keep looping for as long as the condition evaluates to true.
3.) An infinite loop is when the expression always evaluates to true and it will execute forever.
4.) An iteration is each time a loop executes.

PART II
1.) For loops are preferred when the loop variable is know. This allows us to define, initialize, test and change the value of loop variables.
2.)

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

3.) Off by one errors happen when the loop iterates one too many or one too few times.

1 Like
  1. With the while statement we can create a continuous loop that always evaluated to true and continues on forever.

  2. The value in the statement needs to evaluate to True in order for the loop to continue.

  3. An infinite loop always evaluates to true and will execute forever.

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

  5. It is a good idea to use a for statement when you want something to repeat a specific number of times whereas a while loop will continue on forever.

  6. You would write a for loop in code by doing this :
    for (initializing of variable; condition statement; modification of variable if condition statement is true)
    for(count = 0; count >= 10, count++;). {
    cout << count \n;
    }
    First part count is initialized to the value of 0; second part if count is >= 10; third part increment variable by 1 and do whatever is detailed in brackets
    The first part of the loop initializes a variable and gives it a value, the second part is the condition statement where it evaluates whether or not the condition is true or false. The last part is the expression and modifies the loop variable if the condition statement evaluates to true and tells the loop to do what is written in the brackets.

  7. Off-by-one errors occur when the loop repeats one too many or one too few times to produce the desired result. The most common cause for these errors in using the wrong relational operator. An example of this is when we use <5 instead of <=5 or >1 instead of >=.

1 Like

Part 1

  1. When a while statement is executed, the condition is evaluated and If the condition is true, the statement executes.

  2. The while loop will continue if statement value in the brackets evaluates to true.

  3. Infinite is the loop that never terminate, and the program prints result forever becouse the expression always evaluates to true and the while loop executes forever.

  4. Each time a loop is executed it is called initeration.

Part 2

  1. For loop is preferred when we know exactly how many times we need to iterate.

  2. for (init-statement; condition; end-expression) statement :
    for(int c{ 9 }; c>= 0; --c) { }

  3. Off-by-one errors is a problem that occur when the loop iterates one too many or one too few times. The most common cause for these errors is using the wrong relational operator (<,>, <=, >=, etc).

1 Like

Part 1
1. What can we do with the while statement?
We can use the while statement to create loops.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
The value needed to continue the loop is true.
3. What is an infinite loop?
An infinite loop is when a loop never terminates due to the expression always evaluating to true.
4. What is an iteration?
An iteration is every time a loop executes.
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 you know how many iterations you need, use a for loop.
2. How would you write a for-loop in code?
for (init-statement; condition; end-expression)
statement
3. What is an off-by-one error?
An off-by-one error is one that occurs when the loop iterates one too many or one too few times to produce the desired result.

1 Like

1. What can we do with the while statement?
It can repeat it’s loop after it’s done executing the statements in the while loop.

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

3. What is an infinite loop?
A loop that never stops because the value is always true.

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

1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
While loops are needed when something needs to be looped over and over again. If that is not needed then use a loop to loop for a specific amount.

2. How would you write a for-loop in code?
for (x int = 0; x< = 10; x++){
cout << x;
}

3. What is an off-by-one error?
When a loop doesn’t do the right amount of iterations.

1 Like
  1. with a while loop, we can continously execute code as long as a certain condition evaluates to true

  2. The statement must always evaluate to the specified condition to be true for the loop to continue

  3. an infinite loop well execute continuously because the statement is never false.

  4. an iteration is 1 execution cycle of a loop

  5. a for loop is preferred when we have an obvious loop variable because we can more easily
    define, initialize, test, and change the values

  6. for (int count{ 1 }; count <= 10; ++count);

  7. off-by-one errors occur in for loops when user uses the wrong relational operator, for example
    using < instead of <=

1 Like
  1. We can create a while loop. When a while statement is executed, the condition is evaluated. If the condition evaluates to true, the associated statement executes.

  2. True. While statement will keep looping for as long as the condition evaluates to TRUE.

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

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

  5. When we have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.

for(int i = 0; i<5; i++)
{
cout << i << endl;
}
7.
Rookie mistake. Off-by-one error - when the loop iterates one too many or one too few times to produce the desired result. It happens because of the wrong relational operator. For example ‘<’ is used when the program needs ‘<=’ and vice versa.

1 Like
  1. Create loops to complete a task over and over again.

  2. True

  3. It is a loop that will always evaluate to True

  4. It is one complete round of the program.

  5. When you have a counter variable that can be used.

  6. For (int Counter; counter (statement to eval); counter increment){ Function};

  7. When the loop is programmed so that the end condition is met either one counter too early or one too late.

1 Like

Technically the loop, but I guess a program might also be run in a loop :smile:

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

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue
    The statement in the brackets has to evaluate to ‘true’ for the loop to continue.

  3. What is an infinite loop?
    An infinite loop is one where there is no way to break the loop, in this case it will continue until it crashes the system.

  4. What is an iteration?
    It is a single execution of the loop out of however many it 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?
    We use a for loop when we know how many iterations are required as opposed to a while loop which is better when the number of iterations is less clear.

  6. How would you write a for-loop in code?
    {for (int count{1}; count <=100 ; ++count)
    statement;
    }

  7. What is an off-by-one error?
    An off-by-one error occurs when the loop iterates one too few or too many times. This is usually caused by an error in the break code by the programmer.

1 Like

1. What can we do with the while statement?
We can automatically repeat code as many times as we like.
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 never ends because the loop statement never evaluates to true.
4. What is an iteration?
One turn in 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?
2. How would you write a for-loop in code?

for (int i=0; i<10; i++)
    cout << i << endl;

3. What is an off-by-one error?
It’s a mistake that makes the loop one too many or one too few iterations.

1 Like

Part 1:
1 - While loops allow us to keep looping through the code that has been executed until the condition is deemed to be false .
2 - To keep looping the while value has to return true.
3 - An infinite loop is one where the while value is forever true.
4 - An iteration is the name given for each time a loop is executed.

Part 2:
1 - For loops are useful when we know the amount of times we want to iterate the code. While loops are useful for when we know that we need to iterate for an unspecified period of time.
2 -
int main()
{
for (int count{ 1 }; count <= 10; ++count)
cout << count << " ";

return 0;
}
3 - Off-by-one errors occur when the program loops one too few or one too many to produce result required. An example of when this can occur is if in the for loop the programmer writes:
for (int count{ 1 }; count < 10; ++count)
Comparing this loop to the one in answer above we can see that this loop writes; ;count < 10 , this loop will only iterate 9 times, as the code is required to iterate less than 10 times, as opposed to iterating less than or equal to 10 times, like in the example in the question above.

1 Like
  1. What can we do with the while statement?
    We can execute loops while a certain condition is met. When the condition is no longer met, the loop is exited.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    The value of the statement needs to be true. The loop is exited when the statement is false

  3. What is an infinite loop?
    An infinite loop is a scenario where the stament remains true and is never false. The loop is never exited because the value of the statement is always true.

  4. What is an iteration?
    Each time a loop executes we call it 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?
It is good to use it when a counter is needed. If you do not have a counter, a while statement is probably a better choice.

2. How would you write a for-loop in code?
many possibilities.
Here is one. This prints even numbers from 0 to 20

for (int count{ 0 }; count <= 20; ++count)
{
if (count%2==0)
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 to produce the desired result.

1 Like
  1. What can we do with the while statement?
    Create a loop to iterate code.

  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?
    Loop without a break or any other clear stop which makes loop go forever.

  4. What is an iteration?
    Repeating of a code.

  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 there is an obvious loop variable.

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

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

1 Like

part 1

  1. We can execute code as long as a condition is met and continue to do so until the condition is no longer true
  2. The value needs to evaluate to true
  3. When the condition in the loop always evaluates to true
  4. The number of times the loop has executed

part 2

  1. When you know how many times you want to loop and use a variable as a counter that increments each iteration
  2. for(initializing counter; condition for counter, increment or expression for counter
  3. When you increment or decrement too many or too few times
1 Like

What can we do with the while statement?
We can execute a piece of code repeatedly until some condition is met.

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

What is an infinite loop?
It is a loop that will execute forever, because the expression always evaluates to true.

What is an iteration?
Each time a loop executes is called 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 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.

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

What is an off-by-one error?
Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result, eg. When we use > operator instead of >=.

1 Like

While loop

  1. With a while statement we can execute code repeatedly while a condition is true.
  2. True.
  3. A loop that never stops executing because the condition never turns to false.
  4. An iteration is each time the code inside the loop executes.

For loop

  1. The for loop is better to use when you know the exact amount of iterations you need.
  2. {for (init-statement; condition; end-expression)
    statements to execute;
    }
  3. Off-by-one errors the ones when the loop doesn’t iterate the desired amount of times, often because of using the wrong operator in the condition.
1 Like
  1. A while statement allows us to create a loop in a program.

  2. A boolean statement is in the brackets and it is this statement that evaluates whether the loop will continue or end. A value that is true is needed for the loop to continue.

  3. An infinite loop is a loop that will continue to loop unless the programmer ends the loop manually with a break statement. e.g. while (true)

  4. An iteration is the value printed or executed at the end of each loop cycle.

  5. It is usually always a good idea to use a for loop because the code is easier to read as it is more concise and has all the information you need to understand what the loop does and also makes it easier to make any edits to the loop if need be

  6. for (init-statement, boolean expression (condition), increment or decrement expression)
    3.An off by one error is the most common type of error made by new programmers writing a loop. It is when the loop executes one too many times or one too few times.

1 Like