Loops in C++ - Reading Assignment

What can we do with the while statement?

We can make computer repeat the commands as long as a condition’s expression gets true value.

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

“True”

What is an infinite loop?

Loops that executes forever as long as the expressions get evaluated to true and while there’s no any update on the expression value.

What is an iteration?

A loop execution

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 need define, initialize, test, and change the value of loop variables easily.

How would you write a for-loop in code?

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

What is an off-by-one error?

Programmer misses the last iteration also must be counted by the times of condition value by using < or > operater instead of <= or >=, the loop iterates one too many or one too few times.

1 Like

Loops in C++ - Assignment.

A.

  1. We can use the while statement when we want to run the same block of code until a condition is met.

  2. The value that needs the statement in bracket in order for a loop to continue is the **while ( Bolean condition).**eg while ( n > 0 ).

  3. An infinite loop is when an expression is always evaluate to be true, the while loop will execute forever.

  4. An iteration is each time a loop executes a statement until some condition is met. It is introduced by keyword like while, do, and for.

B.

  1. It is a good idea to use a for statement (do a for loop) instead of a while loop when a condition is evaluated after the execution instead of before the execution. This will guarantee atleast one execution of statement, even if the condition is never fulfilled.

  2. A for loop can be written in code by using the following:

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

    # < include iostream>
    

{
int main ()

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

 std: : count << count ' ' ;

return 0 ;

  • init - statement is use to define variable definition and initialization

  • condition - it evaluates the condition. if condition is evaluated to be true, the condition will be executed.

  • end expression - is used to increase or decrease the loop variable in the init - statement.

  1. Off - by - One error occurs when a loop reiterates one too many, or one too few times to produce the desired result.
1 Like

1.) The while loop will continue to loop until the condition evaluates false.

2.) True

3.) An infinite loop is a loop that will always evaluate to true.

4.) An Iteration is each time a loop executes.


1.) A For Loop is good to use when we have an obvious loop variable or how many time to iterate because it lets us concisely define, initialize, test, and change the value of loop variables.

2.) for (int y =0; y>=10; ++y)
cout << y >> endl;

3.) An off by one error can occur when a loop iterates one too many or one too few times, due to a mistake in the conditional expression.

1 Like

Think about the following questions while reading:

  1. What can we do with the while statement?
    We can run a bunch of code in a loop until the while loop condition evaluates as 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 which condition always evaluates to true.

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

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?
    For loops are better when there is an obvious loop variable to be used.

  2. How would you write a for-loop in code?
    for (int i {0}; i < 10; ++i)
    // statement

  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.

2 Likes

1. What can we do with the while statement?
with a while statement we can let a specific program run until a condition is false is met that end the loop. Like this while n is lower than 6 print hello on the screen if n reaches or passes the number 6 don’t prin’t hello and quit the loop.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
true when the statement reaches false it quits the loop and goes to another part of the program

3. What is an infinite loop?
an infinite loop is when the condition of the loop is always true this means that the loop will loop infinite times because it only stops when the condition reaches false. As long as the condition is true it will just do the loop over and over and over again.

4. What is an iteration?
this is a value that sets the time or how many times the loop has to perform.

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?

when you know exactly how many iterationis you need.

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

for (int count{0}; count<=5; count++;)
{
       your function.
}

3. What is an off-by-one error?
this error will show up when a loop does a iteration 1 times too much or too few.

1 Like

Part 1

  1. The while loop will execute the same block of code until until the boolean condition is false.
  2. The boolean expression must be true.
  3. A loop that never breaks because the condition never becomes false.
  4. An iteration is a single completion of the code.
  5. When we know exactly how many times we want the loop to execute.
  6. for(int i=0; i<5; i++){
    // code goes here
    }
  7. When the number of code iterations is off by one, either over or under.
1 Like
  1. The while statement executes until a condition is found tobe false. As such one could eveluate an array fro conditions until one is found to be false.
  2. The value of “True” needs sto be evaluated in the statement in the barckets in order for the loop to continue?
  3. An infinite loop is a while statement that always evaluates to “true”, the loop will execute indefinitely.
  4. An iteration is a a loop execution.
    1. It a good idea to use a for statement (do a for loop) instead of the while loop when there is an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.
    1. We would write a for-loop in code as such for (init-statement; condition; end-expression) statement
    1. An off-by-one error happens when a loop iterates one too many (or one too few ) times to produce the desired result.
1 Like
  1. we can loop through a statement and it will continue looping as long as the condition evaluates to true
  2. true
  3. infinite loop happens when the expression only evaluates to true and this loop executes forever
  4. iteration = each time a loop executes

  1. for loops are used when we know how many times we need to iterate.
  2. for (int count{1}; count<=10;++count)
    {
    std::cout << count << ’ ’ ;
    }
  3. it is when the written loop executes one too many or one too few times
1 Like
  1. What can we do with the while statement?

It is a conditional statement, while the condition is true, the statement will be executed

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

The statement has to be true i.e. 1

  1. What is an infinite loop?

A condition that is always true

  1. What is an iteration?

A function that repeats itself under certain conditions

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 a clear 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?

Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result.

1 Like

First Reading

  1. A while statement acts like an “if” statement… while (condition){statement;}. These can be used when you don’t know the exact amount of iterations you want the loop to run, often times. when taking user input, etc.

  2. The values that the statements in the brackets need to amount to - in order to continue iterating - are less than or greater than the targeted value of times you specify the loop to run.

  3. An infinite loop is a loop that always runs true. Therefore, it never reads false and never comes to a completion.

  4. One iteration is the process of executing/running through a loop once.

Second Reading

  1. It’s a good idea to use a for statement when you know the specified number of times to run the loop iteration (aka you know the loop variable).

  2. a for loop written in code looks like:

    for( init-statement; condition; end expression)

  3. Off by one error is when the loop iterates the full amount of times that were specified, minus once, or plus once. Often times this is due to an operational error

(ie //run this loop

ten times

.....

int main() { for (int count = 0; count <=10; count++) {

std::cout<<count<<" ";
}
return 0;
}

//prints 0 1 2 3 4 5 6 7 8 9 10
//this loop ran 11 times

1 Like

If statement branches a code block, while loops a code block. :slight_smile:

While loop:
1. What can we do with the while statement?
= We can repeat a block of code until a certain condition is met.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
= true. Between the bracket in a while loop you will find a true or false condition, as long as the value is true the loop will continue.

3. What is an infinite loop?
= A loop that never stops. When you have set a conditions in your while loop so that the value by all loops will be true and jumps back to the beginning of the loop.

4. What is an iteration?
= A loop cycle.

For 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?
= When we know how many times we want the loop to iterate.

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?
= A off-by-one error is when the loop iterate one to many or one to few times than it is supposed to. It can happen when you use a wrong relational operator, or sometimes it you pre-increment or pre-decrement instead of post-increment or post-decrement or the other way around.

1 Like

Part 1:
1. What can we do with the while statement?
This is a statement that can be used to loop over code.

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

3. What is an infinite loop?
This is a loop that always evaluates to true. Most loops will stop once they have evaluated to false.

4. What is an iteration?
Each time the statement loops it is called 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?
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?

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

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.

1 Like

Part 1

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?
While statement will keep looping for as long as the conditions evaluates to true.

3. What is an infinite loop?
If the expression will always evaluates true, the while loop will execute forever.

4. What is an iteration?
When a loop execute, it is called 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?
The for statement (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?
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 to produce the desired result.

1 Like
  1. loop until the condition is false.

  2. TRUE.

  3. Loop that can’t terminate by itself, because the condition will always be true.

  4. Each “iteration” of the loop.

  5. When we know how many things we have to iterate through.

  6. for(int count{ 1 }; count <= 10; ++count

  7. Iterating one too many times.

1 Like

Reading Assignment: Loops in C++

Part 1

1. What can we do with the while statement?
The while statement can be used to evaluate a condition to see whether it is true, and if so, the associated statements may then execute.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
The value needs to be evaluated to “true” to then execute.

3. What is an infinite loop?
An infinite loop is where a condition is evaluated and remains true infinitely.

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

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?
Because they are a very compact ways to do loops with a counter, by containing all the necessary information about variables, loops conditions, and loop modifiers all at once, it is therefore easier for developers and helps reduce errors.

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

int main()

{

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

std::cout << count << ’ ';

return 0;

}

3. What is an off-by-one error?
When the loop iterates one too many or too few times to produce the desired result due to using the wrong relational operator.

1 Like
  1. you can execute a repeat statement until the condition evaluates to false

  2. true

  3. a piece of program that never stops executing because it continues to evaluate as true
    4, each loop cycle is referred to as an iteration

  4. for loops are used when you know how many times you want the loop to run

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

  6. off-by-one error - occurs when the loop iterates too many or too few times, usually caused by using the wrong relational operator

1 Like

1. What can we do with the while statement?

We execute a bunch of statements while a condition is true. An example of using the while loop can include printing numbers from x to 1000, where x = 1. x can be increments inside the while loop until the condition x < 1000 is no longer true.

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

The condition in the brackets needs to evaluate to true in order for the loop to continue.

3. What is an infinite loop?

A loop in which its condition always evaluates to true during runtime and never false.

4. What is an iteration?

It is each 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?

It is a good idea to use a for loop when you have a counter variable to track the number of iterations.

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

Example:

for(int i {0}; i < 10; ++i){
*some code to execute goes here*
}

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

It describes a human-error by the developer where their loop iterates one too many times or one too few times.

1 Like

PART 1

  1. With the while statement, we can evaluate a condition to see if it is true. If it is true, the associated statement executes.

  2. A while statement will continue to evaluate as long as the result is true.

  3. An infinite loop will always evaluate to true, causing it to loop indefinitely.

  4. Iteration refers to a single loop through the elements of a specific range using a set of operators.

PART 2

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

  2. To code a for loop we use:
    for(int count {1}; count <= 99; ++count);

  3. An off-by-one error is a common mistake made by new programmers. It is caused by accidentally pre or post incrementing/decrementing, giving an erroneous output that is one digit off from what was intended.

1 Like

Reading 1

  1. What can we do with the while statement?
    The while statement allows repeated execution of a block of code, where the number of repetitions is unknown when the program is compiled, until a prespecified condition happens.

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

  3. What is an infinite loop?
    An infinite loop happens where the condition for continuing the loop is always evaluated as true so that the loop will continue forever.

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

Reading 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?
    For loops are convenient when looping with a counter. The syntax of for loops make loop initiation, loop condition, and counter modification clear and easy to adjust.

  2. How would you write a for-loop in code?
    for (counter initiation; loop condition; counter modification){
    codes…
    }

  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.

1 Like