Loops in C++ - Reading Assignment

Part 1

  1. We can use the while statement to loop.

  2. The while statement is repeated if the value or expression is true.

  3. A loop that never stops running because the value or expression is always true.

  4. A loop basically.

Part 2

  1. A loop is used when it is known how many times its needed ro iterate. A while function is used when we have to loop during a undefined period.

  2. for(int x=0; x < length; x++) {/code block/}

  3. The error occurs when the loop iterates one too many or one to few times.

1 Like

PART 1

  1. What can we do with the while statement?
    Create loops that continue running until the condition returns false
  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?
    When a condition is always true it creates a loop that runs indefinitely
  4. What is an iteration?
    Every time a loop execute it is considered one 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?
    When there is an obvious loop variable
  2. How would you write a for-loop in code?
    for(int count{0}; count<10;++count)
  3. What is an off-by-one error?
    When a condition is read as false, either too soon or too late by one iteration
1 Like
  1. What can we do with the while statement?
    Create a loop that executes if the statement returns 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?
    It is a loop, which will never end because it will always return true.

  4. What is an iteration?
    It is a number, which shows how many times the loop was 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?
    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.

  6. How would you write a for-loop in code?
    for (int i = 0; i < 10; i++)

  7. What is an off-by-one error?
    It is an error, which is shown if for loop iterates one time more or less than it had to.

1 Like
  1. We can iterate through data based on certain criteria

  2. While something is true, the loop will be executed

  3. An infinite loop is when a statement is always true and the loop never stops

  4. Iteration is one time through a loop

===========================================================================

  1. When you already know the number of iterations that are needed

  2. for (int count { 1 }; count <= 100; ++count)

  3. It is when we use < operator instead of =< operator and the loop iterates one times less

1 Like

1.) We can repeat code until a condition is met
2.) it evaluates if the condition is true
3.) a loop without an end. No manual ending and condition is never true
4.) An execution of the loop

1.) When there is a counter variable
2.) for (int count {1} ; count <= 5; ++count)
{
statement;
}
3.) The loop executes once to many or few times.

1 Like
  1. We can do loops at non - compiled time .

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

  3. Infinite loop is a loop with always true conditions .

  4. Every single execution of the loop is called an iteration .

  1. The for loop is better when we have an obvious loop incoming. No user input , just an obvious loop. That way the loop is easier to modify.

  2. for (variable ; boolean ; end - expression){
    conditions to be executed …
    }

  3. Off - by - one errors occur when the programmer misses or uses the wrong operators and the result becomes off or up by one .

1 Like

Part 1

  1. A “while” statement allows you to conduct a set of instructions multiple times over (loop), within the constraint of a condition returning true
  2. The statement in the brackets (condition) must evaluate as “true” for the loop to continue
  3. An infinite loop is a loop statement that is impossible to break out of, forcing the system to repeat the set of instructions forever; the condition always evaluates to “true”
  4. An iteration is when a loop completes an execution; a loop can conduct N iterations

Part 2

  1. When there’s an easily apparent loop variable, it’s best to use a “for-loop” statement since it’s easier to define, initialize, test, & update the value of the the loop variables.
  2. for (init-statement; condition; end-expression)
    statement

    for (int i{0}; i <= 9; ++i)
    cout << i << ’ ';
  3. When a coder writes an error that iterates a loop once too little or too much
1 Like
  1. we can loop statements as long as the condition in the while loop evaluates to true

  2. true

  3. it’s a loop where the expression always evaluates to true

  4. it’s every time a loop executes

  5. when we have an obvious loop variable

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

  7. it’s an error when the loop executes one too many or one too few times

1 Like
  1. What can we do with the while statement?
    While a condition is true, loop through a set of instructions until it becomes false

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    It expects a boolean resulting in TRUE and exits when the result is FALSE.

  3. What is an infinite loop?
    A loop that where the condition always equals to true eg while(true){}

  4. What is an iteration?
    Each cycle of the loop is 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 you want to iterate over each element in an array or each character in a string for example.

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

for(int i = 0; i <= 10; i++){
    std::cout << i;
}

Result

012345678910
  1. What is an off-by-one error?
    When you are off by one in the amount of times you intend it to loop. Take the last example of a for loop. if I said i < 10 instead of i <= 10 it would only print out 0123456789 missing 10 which may be a result that was needed. Same would go if I initialized int i = 1 it would print out 12345678910 which in the case of iterating over an array would mean you miss the first element.
1 Like
  1. What can we do with the while statement?

    • 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.
  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.
      This is called an infinite loop.
  4. What is an iteration?

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

Part1:

  1. loop until a certain condition is met
  2. True
  3. A loop which is always true
  4. It’s calling the loop, each call is an iteration

Part2:

  1. We do a for loop when we know how many times we need to iterate
  2. for(int j=0; j<= 12; j++){
    //instructions}
  3. a coding error, that leads to the loop doing 1 more or less iteration
1 Like
  1. What can we do with the while statement?

with a while statement we can have something print out incrementally. if we wanted to print out 1 to 1000 we could type much less than having to do it with out loops.

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

True

  1. What is an infinite loop?

if the value is always true, then the loop can go on forever.

  1. What is an iteration?

an iteration is each time a loop 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?

a for loop should be used when the variable are obvious. that way we can test, initialize and change the value of loop variables.

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

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

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

these are errors that occur when the loop iterates one more time or one less time than it should.

1 Like
  1. What can we do with the while statement? we can execute code multiple times

  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 where the expression always evaluates as true

  4. What is an iteration? each time a loop 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? when we have an obvious loop variable

  6. How would you write a for-loop in code? for( init-expression, condition, end-expression){}

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

1 Like
  1. We can print out something as tedious as printing out all numbers from 1 to 100, as long as the condition is true.
  2. The statement in the brackets needs to evaluate to true.
  3. A loop where the expression always evaluates to true.
  4. Every time a loop executes is an iteration.
  5. It’s best to use a for loop when you have an obvious loop variable so you can easily define, initialize, test, and change the loop variable values.
  6. for (init-statement; condition; end-expression) statement
  7. An off-by-one error is when the loop iterates either one too many or not enough times to produce the desired result.
1 Like

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

2. What value needs the statement in the brackets to evaluate for the loop to continue?
For a loop to continue, an expression must evaluate to true.

3. What is an infinite loop?
A loop that iterates an infinite number of times.

4. What is an iteration?
It a sequence of instructions that are repeated continually.
For example- loops.

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 loop is preferred when we have an obvious loop variable. That is, we know how many times to iterate over the loop.

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

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

7. What is an off-by-one error?
It occurs when an iterative loop iterates one too many or too few times.

1 Like

1-It is a control flow construct that allow a piece of code to execute repeatedly until some condition is met.
2 - A condition.
3- A condition that is always met
4- every time a loop executes.

Part B
1- When we have an obvious loop variable because is esier to define, initialize, test and change the value of loop variables.
2- for (init-statement; condition; end-expression)
statement.
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

Article 1:

  1. What can we do with the while statement?
    A loop that is executed ONLY if a statement remains TRUE.

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

  3. What is an infinite loop?
    A loop that never breaks.

  4. What is an iteration?
    Each loop is called like that.

Article 2:

  1. When the loops are more specific, for example if I needed to change a value.

  2. for (Times it will be executed){
    The function contained by the loop
    }

  3. Iterating the wrong amount of times due to typing errors.

1 Like

1. What can we do with the while statement?

The while statement creates a loop.

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

The value must be true so that the loop continues.

3. What is an infinite loop?

A loop that never ends.

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?

for loops are used better for when you know how many iterations you will need it to make.

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

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

any code you need

}

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. Save time, write cleaner looking code on fewer lines

  2. The condition needs to evaluate to true in order for the loop to continue

  3. A line of code that does not have a break, and continually repeats itself crashing the programme

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

Part Two

  1. When we know the iteration we want to go up to

  2. For (counter = 0; counter <=100; counter ++)

So the above if I remember correctly, and reviewing my notes is how to do a for loop in JS, But, from reading this article, it is slightly different in C++ (below right?)

for (count { 0 }; counter <=10; ++count)
std::cout << count << endl;

  1. Occurs when the loop iterates one too many, or one too few too many times to produce the desired result.

struggled a bit with quiz question 2, anyone else?

1 Like

Mostly its the same, you still have to use the same variable for the condition (you used counter instead of count). You must also have the variable declared, if count is not already declared, you can do it in the for loop statement:

for (int count; count < 10; count++) {}

What was the question? :slight_smile: