Loops in C++ - Reading Assignment

1.What can we do with the while statement?

We can evaluate expressions and when it evaluates to true the statement gets executed

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?

That is a loop in which 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 .

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

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

for (int count{ 0 }; count < 10; ++count)
    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. 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

While loop:

  1. You can create a loop.
  2. As long as the statement is true, the loop will continue to run.
  3. Infinite while loop occurs, when statement is permanently true -> while (true) {}.
  4. Iteration is one complete cycle of the loop.
    For statement:
  5. When you know an exact number of iterations.
  6. for (variable declaration; condition; counter)
    for(int count{0}, count<10; count++).
  7. When for loop is performed too many times or too few times by one iteration due to error in for loop condition.
1 Like
  1. We can use while statements to continuously evaluate expressions repeatedly until whatever conditions are met. For good or for worse, this maybe an infinite loop! For example, servers may continuously make requests to third parties. However, we wouldn’t want a countdown from 10 to 0 continue through 0 and go into negative numbers.

  2. In order for a while loop to continue, the expression in the loop must evaluate as true.

  3. An infinite loop occurs when a while loop executes (evaluates as true) forever.

  4. An iteration is a single execution of a loop.

  5. You may want to use a for-loop over a while loop when you know exactly how many times you want to iterate an expression. In these cases, initialization, definitions and alterations are much easier to make.

  6. Here is a template for writing a for-loop:

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

Where we have:

init-statement: variable definitions and initialization of elements
condition-expression: execution is evaluation is true, termination is evaluation is false
end-expression: evaluated after statement execution in the condition-expression step. Usually this step involves the increment/decrement of step values declared in the init-statement. After evaluation, we return to condition-expression.

  1. A off-by-one error usually occurs through the improper use of inequalities (i.e. <=, >=, <, >) in the conditional-expression. They can be hard to track down, so it is recommended to test iterate your loops 0, 1 and/or 2 times. If it works with known inputs in these 3 cases, it should be safe for all natural numbers.
1 Like
  1. What can we do with the while statement?
    With a while statement you can create a loop that evaluates expressions repeatedly until a certain condition is met.

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

  3. What is an infinite loop?
    A loop where the value of the statement never evaluates to False.

  4. What is an iteration?
    One complete cycle 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?
    When you know exactly how many times you need to iterate.

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

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

1 Like
  1. Keep looping until a condition is met

  2. True

  3. while(true){}

  4. An iteration is when the loop recycles

  5. for loops are for when we know how many times we are going to iterate

  6. for (init; condition; increment){}

  7. When the for loop overshoots by one loop

1 Like
  1. A loop.
  2. A counter variable.
  3. When the loop does not stop.
  4. It is one cycle of the loop.

  1. When we know how many iteration we need.
  2. for ( int szam{0}; szam<=10; ++szam)
    cout << szam << " ";
  3. When the loop iterates one too many or one too few times.
1 Like

What can we do with the while statement?
in c++, the while statment implements such a repetition, the code keeps executing the statements while the condition is true.

What value needs the statement in the brackets evaluate to in order for the loop to continue?
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.

What is an infinite loop?
if the expression always evaluates to true, the while loop will execute forever. This is called an infinite loop.

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

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 to loop through a block of code, we use the for loop instead of a while loop.

How would you write a for-loop in code?
for (int i = 0; i < 5; i++) {
cout << i << “\n”;
}
What is an off-by-one error?
One of the biggest problems that new programmers have with for loops (and other kinds of loops) is off-by-one errors.
Off-by-one errors occur when the loop iterates one too many or one too few times

1 Like

1. What can we do with the while statement?
We can make a loop.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
As long as the statement is true, the loop will continue.
3. What is an infinite loop?
A loop where the condition statement will always be true.
If we type while(true){do something}, it will go on forever.
4. What is an iteration?
Each time the loop is executed 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 code is more compact. Also, the variables declared in for are not accessible outside which can be useful to avoid unexpected reference.
2. How would you write a for-loop in code?
for (init-expression; condition-expression; end-expression)
statement;

If there are several statements, we wrap them in { }.
If there are several variables, we separate them with comma.

for (int one = 0, int two = 10; one <= 10; ++ one, - - two)
{
anotherVariable += one;
yetAnotherVariable -= two;
cout << “some message” << endl;
}

3. What is an off-by-one error?
If we don’t take in count the first or last iteration of a loop, we could get a wrong result. For example, if we initialize a variable to 0 when we meant 1, or break at 100 when we meant 99. Or, we use < > when we meant <= >=.
Loops should be tested separately before inserting them in a longer program.

1 Like

Both statements will loop through some block of code. For is more useful when you know how many iterations you want to make :slight_smile:

1 Like

Same applies to a while loop, both blocks of code have their own scope outside of which the variables declared inside are not accessable. For loop is more useful when you know how many iterations you want to make :slight_smile:

2 Likes

That makes sense, but what do they mean in the article by this?
The variables defined inside a for loop have a special kind of scope called loop scope . Variables with loop scope exist only within the loop, and are not accessible outside of it.

Its means variables defined inside the scope of the loop (either while or for, not sure why they mentioned for specifically) are only accessable within that function. The same applies to functions for variables defined inside the scope of the function are only visible inside that function.

2 Likes
  1. start a loop
  2. true
  3. a loop with an expression that’s always true
  4. one complete cycle of a loop

  1. when we know exactly how many times we want to run the loop
  2. for (init-statement; condition-expression; end-expression)
    statement
  3. when the loop iterates one too many or one too few times
1 Like

Section 1

  1. Set a segment of code that will be executed as long as the condition is true.
  2. True, therefore non-zero
  3. An infinite loop while run / execute indefinitely because the condition will always be true.
  4. An iteration is a single execution of the loop.

Section 2

  1. When you know, before you code the loop, how many time the code must execute.
  2. For(initial statement; conditional expression; end expression){
    statement;
    }
  3. It’s an error where the loop executes “one-too-many” or “one-too-few” times, this happens due to poor conditional expression definition/
1 Like

1)To repeat loops
2)The value is true then it will repeat.
3)The loop that will never end
4)Each time a loop executes is called as iteration

1)When we know how many times we need to loop(iterate).
2)for(int i=0; i<5; 1++){
cout << i << endl;
}
3)0ff-by-one errors occur when the loop iterates one too many or one too few times.

1 Like

Part I

  1. We can program a loop with the while statement.

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

  3. An infinite loop is when the expression always evaluates to true and the while loop executes forever.

  4. An iteration is each instance when a loop is executed.

Part II

  1. It is a good idea to use a for loop instead of a while loop 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. A for loop looks like this:

for (init-statement; condition-expression; end-expression)
   statement
  1. An off-by-one error occurs when the loop iterates one too many or one too few times.
1 Like

While statements:

  1. What can we do with the while statement?
    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. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    True (non-zero).
  3. What is an infinite loop?
    When an 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 .

For statements:

  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?
> for (init-statement; condition-expression; 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.
1 Like

***1. What can we do with the while statement? to make loops
2. What value needs the statement in the brackets evaluate to in order for the loop to continue? it has to be true
3. What is an infinite loop? when the condition statement is always true
4. What is an iteration? one run of the loop

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? when you know how many times you need to run the loop
2. How would you write a for-loop in code?

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

3. What is an off-by-one error? when condition < and > are mistakenly used rather than <= or >= or the opposite

1 Like

What can we do with the while statement?

A while statement may be used to execute the same lines of code over and over until a certain condition is met.

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 repeat perpetually until it is deliberately ended with a break, exit or goto statement. (or if the user kills the program)

What is an iteration?

An iteration is an instance of code block contained in the loop statement. The loop will have as many iterations as the times it executes.


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 have a requirement of an exact number of iterations for the loop to make.

How would you write a for-loop in code?

for (int i = 1; i <= 20; i++)
{
// code here will execute 20 times. stepper variable i will start as 1 and end the loop when it goes above 20, incrementing by one for each iteration.
}

What is an off-by-one error?

An off-by-one error is when a for-loop executes one too many or one too few times. It is typically caused by an incorrectly placed relational operator ( >= instead of > etc).

1 Like
  1. What can we do with the while statement?
    Run commands multiple times depending on condition defined in the while statement.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    Any value different than zero ().

  3. What is an infinite loop?
    A loop that never evaluates its condition as zero.

  4. What is an iteration?
    A single pass through the loop statement.

  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 the increment and the number of iterations are known.

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

  7. What is an off-by-one error?
    It is an error that occurs when the loop iterates one time more or one time less than it intended to. It generally happens due to wrong relational operator (e.g. using > instead of >=)

1 Like