Loops in C++ - Reading Assignment

  1. We can use the loop to prints a series of number by example 1 to 10 the or 52 to 100.

  2. The while statement will keep to loop as long as the end result remain true.

  3. It is a loop who continue indefinitely because the expression is always true.

  4. When a loop is executed.

  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.

  6. for (int count{ 1 }; count <= 10; ++count)
    std::cout << count << ’ ';

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

  2. True

  3. A never-ending loop without failure

  4. One pass through a loop.


  1. When you may need to define, initialize, and change the value of a loop variable with every iteration.

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

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

1 Like

1a. We can use a while statement to repeatedly execute some code until a certain condition is met.
2a. For a while loop to continue, the conditional statement must evaluate to true;
3a. An infinite loop is a loop where the conditional statement will never evaluate to true. Such as when we exclude an increment/decrement statement. This can be desired such as a constantly updating webapp, or undesired such as accidentally forgetting to add an updater to the conditional statement and causing the program to crash the computer.
4a. An iteration is one execution of looped code. For example, for(int i=0;i<3;++i) will have 3 iterations.

1b. It is best to use a for loop whenever we have a clear variable that we wish to base the loop on, such as the number of elements in an array. When we do not have a such a clear variable to base our loop off of, it is best to use a while loop.
2b.
for(init statement;condition to continue;inc/decrement)
{code to execute each loop }
for example,
for (int i=0;i<10;++i){
std::cout<<"hello"<<' ';
}
3b. A off-by-one error is a common for looping error where we mistakenly miss our desired outcome by 1. This is usually caused by writing the conditional statement as < instead of <= or vice versa, or by setting the init statement to 1 instead of 0 or vice versa.

1 Like

1)create a statement that loops back if a condition is true
2) a loop that has a value that is always true
3) a single execution of instructions that are to be repeated

  1. it is good to use a for loop when iterations are known and a while loop is great if a condition is met
    2)int i = 0;
    for ( i=0; i<=10;i++ ){ cout << “Loop it!”<< endl; }
    3)iterate too many times
1 Like

Part1:

  1. Repeatedly execute statement(s) until a condition is met.
  2. true
  3. A loop that always evaluates to true and consequently repeats infinitely.
  4. Each time a loop is executed it is called an iteration.

Part 2:

  1. It is good to use a for a loop when we have an obvious loop variable(a counter)
  2. for(init statement; condition; end expression){statements}
  3. When a loop is executed one too many or one too few times, often as a result of using the wrong relational operator.
1 Like
  1. It’s smart shortcut way to produce a large set of integers.
  2. the condition needs to be evaluated as True
    3.expressions that always evaluate as true
  3. every time a loop executes

1.they are a very compact way to write a loop with a counter
2. for (init-statement; condition; end-expression)
statement
3. when a loop executes one too many or one too few times than desired.

1 Like
  1. With the while statement, we can loop as long as a certain condition is true.
  2. The value inside the parenthesis has to evaluate to true.
  3. An infinite loop would occur if the value always evaluates to true.
  4. Iteration in this case is each loop.

  1. A for statement is best when there is an obvious loop variable.

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

  1. An off-by-one error is when the user uses the wrong operator like < instead of <=
1 Like
  1. What can we do with the while statement?
    We can create a loop by evaluating the condition and while true, the associated statement executes.

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

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

1 Like
  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 { 1 } ; count <= 5; ++count){
    cout << i << endl;
    return 0;
    }

  3. What is an off-by-one error?
    Problems that occur when the loop iterates one too many or one too few times to produce the desired result.

1 Like
  1. While statement allows a piece of code to execute repeatedly as long as the condition is true.
  2. True
  3. Infinite loop will execute forever as its expression always evaluates to true.
  4. Each time a loop executes
  5. We use the for statement when we have an obvious loop variable.
  6. for (init-statement; condition; end-expression) {statement}
  7. Off-by-one error 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?
    You can use a while to loop.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    While statement is repeated while the expression/value is true.

  3. What is an infinite loop?
    This is when the loop never break, because you expression/value is always true for example.

  4. What is an iteration?
    Each loop is call 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?
    We use a for loop when we know how many times we need to iterate. We use a while when we have to loop during an undefined time.

  6. How would you write a for-loop in code?
    Many possibilities, but an example :
    for(int i=0; i<10; ++i) {
    cout << i << endl;
    }

  7. What is an off-by-one error?
    It’s when you iterate one too many or one too few times. It can happens because you have a mistake in your conditionnal expression

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. You can repeat these executions as many times as you need it to while certain conditions are met.

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

The condition.

  1. What is an infinite loop?

A loop where the condition is never met.

  1. What is an iteration?

Each execution of 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?

The for statement is preferred when we have there is an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.

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

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

statement

  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 to produce the desired result.

1 Like
  1. What can we do with the while statement?
    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 or 1

3. What is an infinite loop?
a loop wherin the condition or statement in the brackets is continually evaluating to 1 or true

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

SET 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?
The for statement (also called a for loop) 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.

2. How would you write a for-loop in code?
The for statement looks pretty simple in abstract:

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

3. What is an off-by-one error?
A very common 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?
  • run a program continuously until the statement is proven false.
  1. What value needs the statement in the brackets evaluate to in order for the loop to continue?
  • needs to be true.
  1. What is an infinite loop?
  • never ending loop that is always true
  1. What is an iteration?
  • single exception of the code block 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?
  • if number of iterations is known its good to use a for loop
  1. How would you write a for-loop in code?
    for ( i = 0, i < 10, i++); {
    cout<< “HELLOOOOOO!” <<endl;
    }

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

  • an error that occurs from a typo
1 Like

1.What can we do with the while statement? A while statement creates a loop as long as statement is true.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue? a true value will loop continuously till it is false.
3.What is an infinite loop? An infinity loop will loop foreever because the statement is never false.
4. What is an iteration? An iteration is every time a loop is executed
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? A for-loop is when you have a defined number of iterations and a while loop can be undefined amount of times.

  2. How would you write a for-loop in code?
    #include
    int main()
    {
    for (int count{ 1 }; count <= 10; ++count)
    std::cout << count << ’ ';

    return 0;
    }

  3. What is an off-by-one error? an iterate that loops too many or too few times “error” in code

1 Like

First Article!

we can use a while loop. It’ll loop until the condition is false.

true.

a loop that will continue executing forever unless we set a break statement or a return statement.

Each time a loop executes.

Second Article!

With a “for loop”, we know how many times we need to iterate and with “while loop”, we have a loop for an undefined time until the condition is false.

for ( int i {1}; i < 5; ++i) {
cout << i << endl;
}

Is when programmers use a wrong relational operator and the loop iterates too many or too few times. ie: < instead of <=.

1 Like
  1. What can we do with the while statement?*** Repeat an operation over and over again until a defined endpoint is reached.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?*** an increment or decrement value such as count++

  3. What is an infinite loop?*** a loop that continues forever unless interrupted by manually breaking or stopping execution

  4. What is an iteration?*** an interation is one cycle or one execution of 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?*** a do loop is used when there is a definite counter variable - a while loop is used if there is no counter variable to define number of iterations

  6. How would you write a for-loop in code?*** for (init-statement; condition; increment)

  7. What is an off-by-one error?*** Off by one is when the loop is stopped on interation too soon or too late such as using > rather than >=.

1 Like

1. also called while loop is declared using the while keyword, and when its executed, the condition is evaluated, and if it 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, and will keep looping as long as the condition keeps evaluating to true

while (condition)
   statement;

2. true
3. when an expression always evaluates true, the while loop will execute forever in an infinite loop.
4. each time a loop executes, it is called an iteration.

1. a for loop is prefered when we have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.

2. this line of code should print a “count” from 0 to 10 “things”.

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

3. when a loop iterates one time too many or one too few times.
Common mistakes are made when using “<, >, =, <=, >=” less than, greater than, etc…

1 Like
  • While statements allow you to make conditional loops that loop while a set condition is equal to true.
  • The value of the condition statement must return true
  • An infinite loop is a while statement where the condition will always equal true
  • Each time a loop executes it is called an iteration.
  • The for statement (also called a for loop) 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.
  • for (init-statement; condition; end-expression)
    statement
  • Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result.
1 Like

Part 1

  1. With a while statement, it can loop a block of code until the condition becomes false.

  2. In order for a loop to continue, its condition must be true.

  3. Infinite loop is a loop that loops forever (until someone stops the program).

  4. When the block of code inside a loop is executed again, it is called an iteration.

Part 2

  1. It is a good idea to use a for statement instead of a while loop when you want to know how many times the loop iterated.

  2. How to write for-loop:

    for(int i=0; i<100; i=i+3){ }
  1. Off-by-one error is when the loop iterates one too many or one few times to product the desired result.
1 Like