Loops in C++ - Reading Assignment

What can we do with the while statement?
Make loops
What value needs the statement in the brackets evaluate to in order for the loop to continue?
Need to be true, but in c++ true = 1
What is an infinite loop?
A loop that runs forever. The value of the expression is always true (1)
What is an iteration?
Every cycle of the loop

Second part
When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
we know exactly how many times the iterations
How would you write a for-loop in code?
for (int i = 0; i <80; i++){// statements}
What is an off-by-one error?
An error normally when we use > or < instead of >= or <=. This iterated more or less than we need

1 Like

In C++ true is actually any non-zero value :slight_smile:

1 Like

yes thanks you are right. I am confusing a little with boolean variable. :+1: :+1:

  1. What can we do with the while statement? The while loop is used to repeat a section of code an unknown number of times until a specific condition is met.

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.

However, unlike an if statement , once the statement has finished executing, control returns to the top of the while statement and the process is repeated.

3. What is an infinite loop? An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an indefinite loop or an endless loop. It either produces a continuous output or no output.

4. What is an iteration? is the repetition of a process in order to generate an outcome. The sequence will approach some end point or end value. Each repetition of the process is a single iteration, and the outcome of each iteration is then the starting point of the next 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 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?

To write a for loop, first type the for keyword, and then in parentheses () provide three things:

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

3. What is an off-by-one error? An off-by-one error or off-by-one bug is a logic error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an iterative loop iterates one time too many or too few.

1 Like

Q1. What can we do with the while statement?

A1. Create a loop that executes a section of code whilst a defined expression evaluates to true

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

A2. True

Q3. What is an infinite loop?

A3. When a defined expression always evaluates to true. In such a case, the while loop will execute forever

Q4. What is an iteration?

A4. Each time a loop executes

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

A5. When we know exactly how many times we need to iterate through the loop

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

A6. Code Example:

// For Loop Syntax;

// for (<start value>; <whilst condition-expression>; <value increment>)

//      <statements>

// e.g.

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

    cout << count << ' ';

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

A7. Off-by-one errors occur when the loop iterates one too many or one too few times - i.e. coder error

1 Like

PART A -
1: “When a while statement is executed, the expression is evaluated. If the expression evaluates to true (non-zero), the statement executes” - https://www.learncpp.com/cpp-tutorial/55-while-statements
2: The statement needs to evaluate to true in order to continue the loop.
3: An infinite loop is a loop that keeps cycling over and over.
4. An iteration is an instance.

PART B -
1: It is good to use a for loop “when we know exactly how many times we need to iterate…” https://www.learncpp.com/cpp-tutorial/55-while-statements/
2: for (int count{ 0 }; count < 10; ++count)
3: Off-by-one errors occur when the loop iterates one too many or one too few times. - https://www.learncpp.com/cpp-tutorial/55-while-statements/

1 Like

Part 1:

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

  2. True or 1

  3. An infinite loop is a loop that keeps iterating because the expression is always true.

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

Part 2:

  1. It is a good idea to use a for loop instead of the while loop when we know the exact number of iterations needed for the loop

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

2 Likes
  1. loop until a condition is met or infinitely

  2. true

  3. a loop that ends in True

  4. a single pass through the loop

  5. when you know the increment and the number of iterations

  6. for(int i=0; i < length; i++) { cout << I };

3 iterate one too many or one too few times

1 Like
  1. What can we do with the while statement?
    It will loop the expression while its true.

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

  3. What is an infinite loop?
    a loop where the expression is always 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 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.

  6. How would you write a for-loop in code?
    for(int n = 1 ; n <= 5; ++n) {
    //code to be executed
    }

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

1 Like

True in c++ is actually any non-zero value. :slight_smile:

2 Likes
  1. What can we do with the while statement?

Loop something until a condition set in () is false.

  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?

a loop that never stops.

  1. What is an iteration?

an execution of the block that loops.

  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 the number of iterations its known its better to use for loop.
  1. How would you write a for-loop in code?

for (x=0; x<10, x++){

}
3. What is an off-by-one error?

You iterated on step too often or too little.

1 Like

1. What can we do with the while statement?
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
3. What is an infinite loop?
4. What is an iteration?

ANSWER:
1 when while statement is executed, the expression is evaluated causing it a loop
2 true statement
3 causes the execution to run in a loop forever
4 each loop executes, its 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?
2. How would you write a for-loop in code?
3. What is an off-by-one error?

ANSWER:
1 when you know the increment and number of iterations
2 for(int i=0; i < length; i++) {/* do stuff here*/}
3 iterate one too many or one too few times

1 Like

Q: What can we do with the while statement?
A: The while statement is used to do a loop.

Q: What value needs the statement in the brackets evaluate to in order for the loop to continue?
A: The value in the statement between the brackets needs to be true in order for the loop to continue.

Q: What is an infinite loop?
A: An infinite loop occurs when the statement between the brackets is always true.

Q: What is an iteration?
A: An iteration is one execution of the loop.

Q: When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
A: A “for loop” is best when we know exactly how many times we need to iterate.

Q: How would you write a for-loop in code?
for (int count{ 0 }; count < 15; ++count)
std::cout << count << ’ ';
You will get the following output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Q: What is an off-by-one error?
A: When you iterate one to many or one too few times.

1 Like

1. What can we do with the while statement?
it will always execute the code inside the statement until the expression that is evaluated is 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 the statement evaluation value is always true , so loop never ends.

4. What is an iteration?
To repeat the code or inside the brackets several 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 you know how many times the code inside the loop needs to be executed.
2. How would you write a for-loop in code?
for (type variable = 0; variable >= Y; variable++)
std::cout << expressionToPrint << ’ ';
3. What is an off-by-one error?
When the loop doesnt execute the expected number of times. Maybe more , maybe less

1 Like

A while statement will execute in a loop if it’s expression is true.

I think this question could’ve been worded better but I believe the answer is “true.”

An infinite loop occurs when the expression always evaluates to true. The while loop will execute forever.

Each time a loop executes is an iteration.

Part 2

It is better to use a for statement when we know exactly how many times we need to iterate a statement. This is because it lets us easily define, initialize, and change the value of loop variables after each iteration.

for (int count{ 0 }; count < 10; ++count)
std::cout << count << ’ ';

A off-by-one error is when the loop iterates one too many or one too few times.

1 Like

While statements

  1. A while statement is declared using the while keyword. When a while statement is executed, the expression is evaluated. If the expression evaluates to true (non-zero), the statement executes.
  2. true or non-zero value
  3. If the expression always evaluates to true, the while loop will execute forever, this is called an infinite loop.
  4. Each time a loop is executed, this is called an iteration.

For statements

  1. When we know exactly how many times we need to iterate.
  2. for (init-statement; condition-expression; end-expression){
    statement
    }
  3. When the loop iterates one too many or one too few times.
1 Like
  • With a while statement, we can create loops. when a statement has finished executing, control returns to the top of the statement and gets repeated.

  • If the statement evaluates to true, the loop will continue. When it evaluates to false, the loops will end.

  • An infinite loop is when an expression always evaluates to true resulting in the loop will execute forever.

  • Each execution of the loop is called an iteration.

  • It is good to use a for statement when we know how many times we need to iterate.
    For statements give us the ability to easilt define, initialize, and change variable values after each iteration.

  • for (init-statement; condition - expression; end-expression)
    statement
    for (int count = {0}; count < 5; ++count) {
    // do something here
    }
    std:: cout << count << ’ ’ ;

  • An off-by-one error occurs when the loop iterates one time too many, or one time less, than expected.

1 Like

PART 1

  1. With while statement, we can repeat the process from the top until the while statement evaluates to false.

  2. As long as the statement evaluates to true the loop will continue.

  3. An infinite loop is when a statement always evaluates to true and as a result while loop executes forever.

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

PART 2

  1. 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. First, we declare a loop variable named count, and initialize it with the value 0. Second, count < 10 is evaluated, and since count is 0, 0 < 10 evaluates to true. Consequently, the statement executes, which prints 0. Third, ++count is evaluated, which increments count to 1. Then the loop goes back to the second step.
    For example:
    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. 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. What can we do with the while statement?
    A while loop evaluates the condition. If the condition evaluates to true , the code inside the while loop is executed. The condition is evaluated again. … When the condition evaluates to false , the loop terminates.

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

  3. What is an infinite loop?
    A loop is said to be infinite when it executes repeatedly and never stops. … When you set the condition in for loop in such a way that it never return false, it becomes infinite loop.

  4. What is an iteration?
    Iteration statements cause statements (or compound statements) to be executed zero or more times, subject to some loop-termination criteria. … C++ provides four iteration statements — while, do, for, and range-based for.

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 will repeat the loop.

  1. How would you write a for-loop in code?
#include <iostream>
using namespace std;
int main(){
   for(int i=1; i<=6; i++){
      /* This statement would be executed
       * repeatedly until the condition
       * i<=6 returns false.
       */
      cout<<"Value of variable i is: "<<i<<endl;
   }
   return 0;
}
  1. What is an off-by-one error?
    An off-by-one error or off-by-one bug is a logic error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an iterative loop iterates one time too many or too few.
1 Like
  1. to loop pieco of code until statement is not true
  2. whether it is still true, checked by every iteration
  3. Loop that never stops
  4. single step in loop

2nd part

  1. if number ot iteration is fixed
  2. for(first to initialize counter; set the breaking point; and at last set the counter) {here will be code we need to execute}
  3. Most common error in for loop, we are one inetaration off the expected result
1 Like