Loops in C++ - Reading Assignment

First article answers:

  1. Make a loop. when a while statement is executed, the condition is evaluated. If the condition evaluates to true , the associated statement executes.
  2. It need a true condition.
  3. A loop that will execute forever.
  4. Each time a loop executes.

Second article answers:

  1. A for loop is better when we know how many times we need to iterate, meanwhile the while loop is often used when we dont know how many times to iterate.
  2. for(int i=0; i<10; ++i) {
    cout << i << endl;
    }
  3. 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?
With a while statement a block of code can be continuously executed as many times until the boolean check condition evaluates to false.

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

3. What is an infinite loop?
An infinite loop is a loop that repeats forever. These loops are undesirable in almost all programs and in the context of the while loop it means the loop continuation condition is always true.

4. What is an iteration?
An iteration is one step in a repetitive process, which in this context is 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?

2. How would you write a for-loop in code?
A for loop is written as follows:
for(int i = 0; i < len; i++) {

}

Here ‘len’ is some variable to which i is compared on each iteration.

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

An off by one error is where the programmer forgets to adjust the relational operator in the loop check condition and thus the loop iterates one too many or one too few times.

1 Like

Questions:
. While statements allow for code to be executed as long as a criteria is fulfilled.
. The value true is required.
. Loops where the condition of a while loop is always true so the code does not stop running.
. Iterations occur through the execution of a loop.

. For loops are preferred when the number of iterations is clear.
. for (int count{0}; count<10; ++count)
cout << count << endl
. They occur when a loop iterates one too many or too few times.

1 Like

Part 1

  1. The while statement allows us to create loops. It will execute a specified piece of code repeatedly until a specified condition is met.

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

  3. An infinite loop is a loop that will never stop executing because its condition will never evaluate to false.

  4. Every execution of the loop is one iteration.

Part 2

  1. For loops are preferred over while loops when there is an obvious loop variable.

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

  3. Off-by-one errors occur when the loop iterates one too many or one too few times. They are commonly caused by using the wrong operator.

1 Like
  1. We can have the loop return to the initial condition of the instance to see if it is true and run the execution again and again until the while loop returns as false.
  2. It needs a true value in order for the loop to execute again.
  3. If the expression always evaluates to true.
  4. A number that defines a specific instance where the loop occurred or recurred.

Pt.2:

  1. When you are trying to reduce errors with one or multiple counters when executing a function.
  2. for(int {x}; x < y; x++)
  3. When a loop executes too many/few times and doesn’t give the desired outcome.
1 Like

Part One
1. What can we do with the while statement?
While statements can be used to loop 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?
A loop that will continue ad infinitum as the condition always evaluate to true.

4. What is an iteration?
A single loop is an iteration.

Part 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?
When more variables are known, as in the number of iterations needed, a for loop is prefered as it can be made more specific.

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

// for (initial expression; condition; end expression) 
for(int i = 0; i < 10; i++) {
    //code to execute as long as condition is true
    cout << i << endl;
}

3. What is an off-by-one error?
When a loop iterates once too many or too few times. Most often due to the incorrect operator being used in the conditional statement ie; < instead of <=

2 Likes

1. What can we do with the while statement? - You can use while a 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 a loop neve breaks, infinite go over.
4. What is an iteration? - Each loop 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 you exactly how many iterations you need
2. How would you write a for-loop in code? - 1. for(int counter = 0; counter<10; counter++){
//do Stuff
}
3. What is an off-by-one error? - a coding error that leads in a loop that does 1 iteration too much or 1 too few

2 Likes
  1. A while statement will perform a certain function over and over until the initialization condition is met.

  2. The counter needs the increment statement in the brackets for the loop to continue.

  3. An infinite loop is a loop with no way for the counter to reach it’s termination value.

  4. An iteration is the event of a loop performing statements inside once.

  5. It is a good idea to use a for-loop over a while-loop because the conditions of the loop are required in the instantiation of loop, and are changed within the loop.

  6. You would start a for loop, include a counter variable, end condition for that variable, and how that variable is changing.

  7. An off-by-one error is what happens when your start/end terms are slightly off, giving an error that can be easily fixed.

2 Likes

1. What can we do with the while statement?
Execute the associated statement over and over as long as the while 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?
A loop that always runs because the condition is always true

4. What is an iteration?
Each time the statement is executed

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 (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 for loop iterates one too few or one too many times than intended.

2 Likes
  1. Create loops.
  2. True
  3. A loop in which the execution condition always evaluates to true
  4. An iteration is the completion of a loop.
  1. When there is an obvious loop variable. It allows for the easy and concise defining, initializing, testing and changing the value of loop variables.
  2. for (int=x, x<10, ++x){do things here}
  3. Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result.
3 Likes
  1. What can we do with the while statement? While loops 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? True

  3. What is an infinite loop? An expression that always evaluates to true, 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? 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? For (int count { 1 }; count <= 10; ++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 to produce the desired result.

3 Likes

1. What can we do with the while statement?
we can make loops by evaluating the boolean expression that´s inside the parenthesis besides the while, and then execute everything that we want, until the end of the curly braces, and then the boolean expression will be evaluated again, and the process will repeat over and over again until the boolean expression returns false. It can also be used to make infinite loops, which can be exited by a return/break/exit/goto/or an exception being thrown out or the user killing the program.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
to true
3. What is an infinite loop?
it is when we use while(true) or another similar expression that evaluates to true, so that this part of the program runs indefinitely, until a return/break/exit/goto/ statement or an exception being thrown out or the user killing the program.
4. What is an iteration?
it is when you use a counter to evaluate the boolean expression, such as counter >= 10 and then inside the loop you can do ++counter and stuff like that

1. 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.
2. How would you write a for-loop in code?

for( int x{10}; x > 0; --x) {
    std::cout << x << " ,";
}

std::cout << " ... we have lift off!!" << endl;


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

it is when you miss the condition by one :stuck_out_tongue_closed_eyes: claaaassic.

3 Likes
  1. What can we do with the while statement? Make a loop based on a condition

  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 keeps iterating cause the condition is always true.
    while (true) {…}

  4. What is an iteration?
    Repetition of a bunch of instruction

  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 there is an obvious loop variable

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

for (signed int i {0}; i<10; ++i) { do something }
  1. What is an off-by-one error?
    It happens when you do a mistake with the condition in the for loop.
    Like
for (int i {0}; i<=10; ++i) {}

This will loop 11 times: did you mean 10?

3 Likes
  1. The while statement will loop while the condition is defined as TRUE

  2. it needs a TRUE value to loop

  3. An infinite loop is when the loop never breaks. For example in a WHILE loop where the condition never changes from TRUE

  4. an iteration is each time a loop is executed. so if it loops 4 times then there is 4 iterations

  5. a for loop would be preferable to a while loop if we know how many iterations we want, whereas a while loop is for when we dont know how many iterations there is going to be and we want the computer to keep runnign the program while the condition is true

  6. for(int x=0, x<=10, x++)

  7. its an error when the loop iterates one too many or too few times, often because of a simple error such as using < instead of <=

3 Likes

Part I:
1. What can we do with the while statement?
We can check if a specific condition in a loop becomes true and if this is the case, execute an associated statement with it.

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

3. What is an infinite loop?
A never ending loop.
This occurs when the loop’s expression evaluates always true.

4. What is an iteration?
A round where code executes a loop.

Part II:
1. When is it a good idea to use for statement (do a for loop) instead of the while loop we learned previously?
For statements are preferable if we have to deal with a counter variable.
If not, it is better to use a while statement.

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

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

3. What is an off-by-one error?
This kind of error occurs when the loop iterates one too many or one too few to produce the desired result.
eg. the code should print out 1 2 3 4 , but the result shows 12 3. The cause is the use of the wrong relation operator.

for ( int count{1}; count < 4; ++count) // instead of <4 we should use <=4

3 Likes

1…loops
2…whatever is put in there.
3…no reason for it to ever be evaluated as false
3…a full execution of the code in the loop just before perfoming it again

1…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.

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

3…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

3 Likes
  1. loop until condition becomes false

  2. true

  3. the loop never stops, that is, the condition is always true

  4. the execution of the code block within the loop’s curly braces

  5. when we want to iterate a fixed number of times. Convenient for array accessing at the same time

  6. for(initialization of variables; condition; changes of variables){ code block }

  7. when the iteration is one too many or too few times, usually this is due to the condition being not exactly as it should be

2 Likes
  1. The while statement can keep evaluating code in it, as long as the certain condition is met.
  2. The statement in the brackets must evaluate to true.
  3. An infinite loop is a loop where the statement always evaluates to true, and the loop runs forever.
  4. An iteration is each time a loop executes.
    1 . It is better to use a for statement when there is an obvious loop variable.
    2 . for (init-statement; condition; end-expression) statement
    3 . When the loop iterates too many or too little times to get the desired result.
3 Likes

FIRST PART

1. What can we do with the while statement?
With a While statement we can loop through a condition as long as it’s “True”, is this condition is met the “body” of the loop (statement) will be executed until the condition becomes “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?
An infinite loop is a loop that never terminates, unless theres some user input (Intentional infinite loop) or the program is forced to shut down.

4. What is an iteration?
Each time a loop executes, it’s called an iteration. If the loop executes N time, it has N iterations.

SECOND PART

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 loop can be used when we have a obvious loop variable and we know how many times we need to iterate. If we don’t know how many times we need the function to iterate, we should use the while loop.

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

3. What is an off-by-one error?
A off-by-one error happens when the loop executes one too many or one too few, and the loop doesn’t give the desired result. This happens when the conditional expression has an error.

2 Likes

1. What can we do with the while statement?
evaluate a condition until it is false

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

3. What is an infinite loop?
An expression that always evaluates true

4. What is an iteration?
Everytime a loop is executed

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 the number of iterations you’ll be using

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?
An error where the loop iterates one too many or one too few amount of times