Loops in C++ - Reading Assignment

What can we do with the while statement? loops

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

true

What is an infinite loop?

A loop that will always be true and never ben broken

What is an iteration?

Everytime it goes through the loop and gets a true and then loop it again is a iteration ,

1. What can we do with the while statement?

  • we can build a program loop to iterate a piece of code

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

  • the statement has to be true

3. What is an infinite loop?

  • never ending iteration of the loop

4. What is an iteration?

  • a repetition of a piece of 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?

  • for loops are perfect when knowing the number of iterations

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

for (int counter = 0; counter < 10; ++counter){
cout << counter << endl;
}

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

  • Its an error in the number of iterations caused by wrong conditional expression

1. What can we do with the while statement?
It can help you to run a function a couple of times, while a specific value is 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?
A run of steps that never stops.

4. What is an iteration?
Execution of steps in a function that runs step by step a couple of times.

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 want more readable code.

2. How would you write a for-loop in code?
for (int iii=0; iii < numberOfIterations; ++iii) { // statement }

3. What is an off-by-one error?
When to code does take steps you not want. It sees no break out or it take to much steps or to less.

  1. What can we do with the while statement?

A: We can make infinate loops.

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

A: It is called a Loop Variables.

  1. What is an infinite loop?

A: It is when a while loop will execute forever. This is called an infinite loop.

  1. What is an iteration?

A: 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?

A: when ever you would want The statement to always executes at least once.

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

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

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

A: 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. What can we do with the while statement?

We can repeat statements when conditions are true.

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

It has to be true.

  1. What is an infinite loop?

Never ending loop, when there is no condition to end loop it will execute the code until the memory will be full or user will kill it.

  1. What is an iteration?

Iteration is name for every single loop execution.

Part II

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

  1. How would you write a for-loop in code?
for (int count = value; count > 0; --count) {
 // body
}
  1. 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. What can we do with the while statement?

Iterate an indefinite number of time - while the loop condition is true

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

The middle condition in the for loop needs to evaluate to true

3. What is an infinite loop?

The exit conditions on the loop are never reached

4. What is an iteration?

A single execution of the statements within a 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 the number of iterations needed

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

for (initialization value; condition; end) {code to execute}

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

This is where your loop runs once-too-many or once-too-few

  1. What can we do with the while statement?
    The while statement is the simplest of the four loops that C++ provides.

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

  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?
    “Off-by-one errors” occur when the loop iterates one too many or one too few times.

  1. What can we do with the while statement?
    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?
    1 (True)
  3. What is an infinite loop?
    A loop with its statement fixed to 1 (True)
  4. What is an iteration?
    One time execution of the block of code defined within 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?
    Executing a loop a defined no. of times and in a predefined way, both known at the start of the loop, is preferably done with the for-loop
    e.g. iterating over an array one element at a time: because the size of the array is fixed and defined when the for loop starts and also the iteration step is predefined (+1)
  6. How would you write a for-loop in code?
    for (/ declaring(optional)count variable and initialization /int count=0; / expresion based on count variable / ; / count variable change /) {/ code to be executed /}
    e.g. for (int count=0; count<length; count++) {/ code block /}
  7. What is an off-by-one error?
    This errors occurs when the written loop code iterates one to many or one to few times.

1. What can we do with the while statement?
While loops execute until the expression evaluates to false. They are useful for repeating some procedure over arrays, lists, etc…

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

3. What is an infinite loop?
The is a loop where the condition never evaluates to false. Generally discouraged in some programming languages but for some internet protocols constant refresh is necessary.

4. What is an iteration?
It is a single trip through a loop from beginning to end.

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 more compact and efficient and don’t have the problem of the iterator such as “count” potentially losing scope.

2. How would you write a for-loop in code?
A typical example…
for (int i=0: i <= 100; i++)
{
std::cout << i << ‘\n’;
… statements
}

3. What is an off-by-one error?
This is when a loop either executes one too many times or one two few. Usually caused by edge conditions in evaluator expressions involving ><=. Some languages such as Pascal will give you a range error if the compiler options is selected in Delphi compiler options. In C++ you will have to be more careful.

Part 1…

  1. What can we do with the while statement?
    The while statement tests an expression to allow us to loop through the code it encapsulates.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    The expression that a while statement tests must evaluate to true for the loop to continue.

  3. What is an infinite loop?
    An infinite loop is created when a while expression always evaluates to true. This could be implemented intentionally if, for instance, it was part of a listener service.

  4. What is an iteration?
    An iteration is a single loop of the while statement.

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?
    If we already know exactly how many times we need the loop to run then it is better to use a for loop as they are more compact and ideal for manipulating arrays and objects.

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

    string arrCars[5] = {“Volvo”, “BMW”, “Mercedes”, “Ford”, “Vauxhall”};
    int intMax = sizeof(arrCars)/sizeof(arrCars[0]);

    for(int count{0}; count < intMax; ++count)
    cout << arrCars[count] << endl;

  3. What is an off-by-one error?
    This error occurs when a loop iterates one too many or one too few times and is typically the result of using the wrong relational operator within the loop’s conditional expression.

  1. We can loop integers so we don`t have to write one for each account. While loops run the program then destroys it for each iteration, and start over until the criteria is met.

  2. The value has to be less or equal to the integer we defined or true.

  3. A loop that never reaches it end goal, for example in a loop where the number doesn`t increase for each iteration.

  4. Iteration is another word for a loop. Onr run through a program in a while or for loop is an iteration.

  5. A for loop is best to use when we know how many times we will run a loop. A while loop is best when we don`t know how many times we will loop.

  6. Example loop:

#include

using namespace std;

int main()
{

for (int count{ 0 }; count <= 20; ++count)
std::cout << count << ' ';


return 0;

}

  1. Off by one error is when a loop iterates one to many or one time less than programmed. Can occur when using an unsigned variable.

Part 1.

  1. Looping through some code while expression is true.
  2. A “true” value.
  3. If the expression always evaluates to true, the while loop will execute forever.
  4. Each execution of the code between the brackets of the while loop.

Part 2.

  1. When we know exactly how many times we need to iterate.
  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) What can we do with the while statement?
With the while statement, we can complete repetitive tasks using loops.
2) What value needs the statement in the brackets evaluate to in order for the loop to continue?
If the expression evaluates to true (non-zero), the statement executes.
3) What is an infinite loop?
It is a while loop that always evaluates to true (never stops).
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?
The 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?
Init-statement, condition-expression, end-statement. Then print it out.
For example:
for( int count {0}; count < 100; ++count)
std::cout << count << ’ ';
3) What is an off-by-one error?
An off-by-one error occurs 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 are difficult to find because the compiler does not notify you. The program runs but produces the wrong result.

image

  1. Loop something many times.
  2. The loop variable needs to be true.
  3. The loop variable is always true, so it never stops until some other action is taken like exiting out of the program or the program crashes.
  4. It’s every time the loop executes.
  5. It’s better to use a for loop when you know the exact number of times you want to iterate.
  6. (initial statement;condition expression; end expression)
    statement;
  7. It happens when the condition is set to something like >,<,>=, or ,<= so it iterates one too many/few times than desired.

loop

true

A loop that never ends. Happens if the statement for the loop always is true

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

The last Quiz was so hard, I couldn’t make it first. I saw the solution and understood it, but I would never come up with a solution like that by myself, it is just so complicated to set the loops up correctly for when they should activate and so on. Even after seeing the solution I tried to rebuild it without looking again and still coudln’t do it. After a pause I tried again and really concentrated and did it! For some reason it became really easy and I am ashamed of how stupid I am lol

when we know exactly how many times we need to iterate

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

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. Set a condition if true the statement executes, process repeated until condition returns false.

  2. Has to evaluate to true.

  3. Infinite loop - program that runs forever until shut down by user or crash of program :smiley:

  4. Each time loop executes called - Iteration.

  5. Good idea to use for loop when we know exactly how many times we need to iterate, because it lets us easily define, initialize and change value of loop variables after each iteration.

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

  7. Off by one error ocurs when the loop iterates one too many or one to few times, caused by wrong relational operator used in the conditional-expression.

1 Like

Part 1:

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

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

  3. What is an infinite loop?
    An infinite loop happens when the expression always evaluates to true.

  4. What is an iteration?
    It is the process of going through the block a specified number of times or until a condition is met.

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?
    It is a good idea to do a for loop when we know exactly how many times we want to iterate.

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

  3. What is an off-by-one error?
    This error occurs when we use “>” instead of “>=” or vice-versa, or “<” instead of “<=” or vice-versa. There will be 1 more or 1 less iteration in our loop and it is hard to track since the compiler will not detect it is an error.

1 Like

1 Evaluate whether an expression is true or false, then a command is executed.
2 True
3 When loop continue to run with out stopping.
4 One time execution of the block of code defined within the loop.
SECOND PART
1 When we know how many times we need to iterate the loop.
2 for(int i=0; i<10; ++i) {
cout << i << endl;
}
3 Off-by-one errors occur when the loop iterates one too many or one too few times.

1 Like