Loops in C++ - Reading Assignment

What about when you are waiting for data from a source? I don’t think using a for loop would make this more readable :stuck_out_tongue:

// Not a real world example, but the point is you have to wait for readbuff to return data.
int buff = -1
do {
  buff = readbuff()
} while (buff < 0)
  1. What can we do with the while statement?

A while statement will execute code repeatedly as long as a given condition is met.

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

The value needs to verfiy the statement in order to loop another time. If the value is not verifying the statement (false) anymore, the programm will exit the while loop.

  1. What is an infinite loop?

An infinite loop will never invalidate the statement an therefore go on forever.

  1. What is an iteration?

An iteration is a repetition.

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 are f. e. dealing with an index of an array, means we already have a fixed number of iterations. The while loop will continue until a certain condition is met. For loop will repeat For a certain amount of iterations. It actually says is in the name. lol

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

for(
int i=0; /setting counter to 0/
i<100; /setting condition, as long as counter is smaller than 100 repeat/
i++; /at the end of each loop increase counter by 1/

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

off-by-one error refers to differences in indexing arrays or vectors. In C languages indexing starts with 0. In other languages indexing might start with 1. So if you put in your “condition-to-meet” in a loop, make sure to consider the indexing rules. if you for example have an array with 5 entries, your condition last entry will be indexed 4 in C! If not considered correctly the loop might iterate on time too much, or too less.

1 Like

While statement

  1. A while statement is used to loop for as long as a condition evaluates to true.

  2. The statement in the brackets need to evaluate to true.

  3. An infinite loop is one in which the the expression in the brackets always evaluates to true and as such the while loop will execute forever.

  4. Each time a loop executes is called an iteration.
    For Statement

  5. When you know how many times you want a loop to iterate for.

  6. for(init-statement; condition; end-expression)
    {
    statements to execute;
    }

  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

Part 1

  1. What can we do with the while statement?
    We can setup loop that will keep repeating until it meets his objective
  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    it needs to be true
  3. What is an infinite loop?
    This is a loop that dont have statement like return, break, exit, goto
  4. What is an iteration?
    Iteration is a one complete run through a loops body before it repeats
    Part 2
  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 “for loop” is more appropriate when we want to have more control on the number of iterations we want the loop to go through
  6. How would you write a for-loop in code?
    for(int i = 0; i < =10; i++ ){ cout << “monkey business” << endl; };
  7. What is an off-by-one error?
    It happens when we get too many or to few iterations that we wanted
1 Like

A
1. What can we do with the while statement?
Execute a loop when conditions are met or non-execution when conditions are not met.
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 evaluation of the While Statement is always true.
4. What is an iteration?
The run of a single execution of the code block.

1 Like
  1. What can we do with the while statement? —- loop a block of code while a condition 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? — loop condition always satisfied so code block loops forever

  4. What is an iteration? — one code block loop cycle

  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 code block should be executed at least once before any condition is checked

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

  7. What is an off-by-one error? — when loops iterate one too many or too few times

1 Like

B
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 need a counter, or when there is a obvious loop variabe.

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

int main()
{
    for (int count{ 1 }; count <= 10; ++count)
        std::cout << count << ' ';
 
    return 0;
}

3. What is an off-by-one error?
It occurs when the loop iterates one too many or one too few times to produce the desired result. By using the wrong relational operator, they can sometimes occur by using pre-increment or pre-decrement instead of post-increment or post-decrement, or vice-versa.

1 Like

1. What can we do with the while statement? We can keep executing a loop so long as a condition holds true. We stop when the condition 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 condition is always true, therefore the loop will loop for ever.

4. What is an iteration? Every 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? When we have an easily defined and obvious loop variable.

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

for (init-statement is 1; condition <10 ; end-expression (add the next element to the last count))
statement (prints the iteration output to screen and a space)

such as…

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

3. What is an off-by-one error? When the loops executes 1 too many (or 1 too few) times than is desired or planned.

1 Like
  1. What can we do with the while statement? loop the function until the condition results 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 always returns true it will keep looping

  4. What is an iteration? one pass through 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? when you know the limit of how many times you want to loop

  6. How would you write a for-loop in code? for(int i=0; i < length; i++) {cout << i << end1;}

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

1 Like
  1. We can loop stuff with a condition while something is true then do this over and over again.
  2. true
  3. A loop that never breaks.
  4. Each single loop is an iteration
  5. For is handy when we know how many times we want to iterate.
  6. for (int i = 0; i<3; i++) {
    //do something
    }
  7. If you iterate too far you can get this error.
1 Like
  1. While statement are used to loop through a statement of code until the condition is evaluated as false (It stops)

  2. true

  3. infinite loop is a loop whose condition never evaluates to false so it executed infinitely

  4. An iteration is the word used to describe when a loop executes. “The loop iterated 10 times before stopping”

  5. You would preferably use a for loop in the instance of knowing how many times the loop should run. You would use a while loop when a breaking condition is met.

  6. for(int count{0}; count<10; count++){code to be executed in here}

  7. An off by one error is a common error that trips up new programmers and is when you don’t get the desired result by one value. This can occur by not including the = with a < or > operator or using pre-(de/in)crement rather than post

1 Like
  1. A while statement will perform a continuous task as long as the statement remains true.

  2. The value of being true.

  3. An infinite loop is when a loop statement never reaches a false condition thus runs infinitely.

  4. An iteration is an instance of one loop in execution or each time the loop completes.

  5. It is good to use a for loop when the variable is obvious.

  6. for (int example{0}; example > 5; ++example){}

  7. This is a mistake is made by the coder that deals with the miscount of iterations performed in the code. For an example for the for loop above it will only execute five time but if the > was >= then it would be 6 times. This off by one error would be formed if you only need the loop five times instead of six.

1 Like

What can we do with the while statement?
We can repeatedly execute a block of code while the provided statement is true

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

What is an infinite loop?
when the expression always evaluates to true, never reaching false, it executes forever

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

How would you write a for-loop in code?

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

What is an off-by-one error?
this happens when the loop iterates one too many or one too few times to produce the desired result, most commonly caused by using the wrong relational operator ( like < vs <=)

1 Like

Part 1

  1. What can we do with the while statement?
    Repeat a true loop until the loop is evaluated as false. “While” always returns to the top of the while loop whereas an “if” condition moves onto the next statement.

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

  3. What is an infinite loop?
    A loop where the condition never evaluates to false.

  4. What is an iteration?
    Each loop repeat is an 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 obvious loop variable

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

  2. What is an off-by-one error?
    Where the loop iterates one too many or one too few times to produce the desired result. Usually arises from a new programmer’s inexperience.

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.

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

3. What is an infinite loop?
An expression that always evaluates to true will never stop evaluating and crash.

4. What is an iteration?
Every time a loop executes 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 statements are prefered when there is an obvious loop variable or when you have a counter variable.

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

3. What is an off-by-one error?
When the loop iterates one too many or one too few times. Usually an operator error, but can occur from pre-incrementing when you should have post-incremented or vice versa

1 Like

1.we can repeat a function until a condition is not met
2. true
3. a loop that is always true
4. each time a loop cycles

  1. When you know how many times you want to loop
  2. initial statement; condition; end exprssion. eg. for(int numb {0}; numb<=10;++numb)
  3. When one forgets to use the <= and just uses the <
1 Like

PART 1

  1. With the while statement we can iterate a chunk of code multiple times,
    till a certain condition is met.

  2. To continue the loop the value in the brackets must evaluate to true.

  3. An infinite loop is a while loop where the condition will always be
    true, no matter how many times we iterate. The loop will be executed forever.

  4. An iteration is the single execution of the statement present in the body of the while.
    The execution begins only if the while condition is evaluated to true.

PART 2

  1. It is a good idea to use a for statement(instead of a while statement) when there is an
    obvious loop variable.

  2. for (initialStatement;condition;endExpression) {
    // loopStatement
    }

  3. An off-by-one error happens when a for loop is executed one too many times (or one too few times)
    than those required to obtain the expected result.
    This is due to an error in coding the loop variable.

1 Like
  1. To loop, to run the program while its true

  2. While the value is true

  3. A loop with an expression that is always true

  4. A cycle of data that you loop

1 When you know how many loops you need

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

3 An error when it loops 1 to much or to little

1 Like
  1. Run code in loop while a statement is true

  2. 1 //which is boolean true.

  3. A loop that has a statement that always is 1.

  4. Each time a while loop runs it’s the next iteration.

1 . If you’re trying to write short code possibly to save on execution time.

2 . for (initialzing counter statement, boolean test , counter charge )
{
code to be executed if true ;
}

3 . When a loop programmer doesn’t take into account the first or last value.
maybe by < instead of <= because of discounting the Zero

1 Like

1. What can we do with the while statement?
Create a loop that executes a code block repeatedly
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, it will continue indefinitely (infinite)
4. What is an iteration?
Each time a loop executes 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?
When we have an obvious loop variable that can be easily defined, initialized and changed
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 the loop iterates one too many or one too few times.

1 Like