Loops in C++ - Reading Assignment

Part 1:

  1. A while statement can create a loop as long as it meets the statements parameters
  2. The Condition needs to evaluate to true to continue
  3. An Infinite loop is a loop that evaluates to true always
  4. Each time a loop executes is called an iteration.
    Part 2:
  5. For statements are preferred when we have an obvious loop variable.
  6. For (init-statement; condition; end-expression)
    statement
  7. An error when the loop iterates one too many times or one too few times than intended
1 Like

1. What can we do with the while statement?
if what is specified is true, then the following code block should be 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?
a loop that repeats itself without pause, which should be avoided

4. What is an iteration?
Iteration statements cause statements (or compound statements) to be executed zero or more times, subject to some loop-termination criteria. . C++ provides four iteration statements — while, do, for, and range-based for.

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 we know how often we have to iterate over the object

2. How would you write a for-loop in code?
for ( statement 1 ; statement 2 ; statement 3 ) {
// code block to be executed
}
https://www.w3schools.com/cpp/cpp_for_loop.asp (nice doc)

3. What is an off-by-one error?
the loop is executed once too much or once too little due to an error in the initial value or in the final value

1 Like
  1. What can we do with the while statement?

Repetitive tasks while a condition is true

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

It must evaluate to true

  1. What is an infinite loop?

A while true statement that never sets to false

  1. What is an iteration?

Each time you cycle through the while statement.

1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?

Make it more compact when we have an obvious loop variable.

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

for(int x{1}; x<=10;++x)
{
Do something cool.
}

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

It is an incorrect operator where you under or overcount the condition

1 Like

1. What can we do with the while statement?

  • We can write a piece of code that repeats itself as long as a certain condition results in 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 repeats itself forever because the expression always evaluates to true.

4. What is an iteration?

  • Each time a loop executes is called iteration.

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?

  • For loops are great when you need to iterate a certain number of times whereas while loops are good when you need to loop as long as a certain condition is met.

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

  • for(int count{1} ; count<=10 ; ++count){ //code block to be executed }

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

  • When we use wrong relational operator so that the loop iterates one too many or one too few times.
1 Like
  1. The while statement will loop through the subsequent commands until the condition is false. Its a type of loop.

  2. True

  3. A loop that will repeat forever unless broken out manually through code in the loop.

  4. And iteration is one pass through the loop.

=====================

  1. When we know the fixed amount of times we expect the loop to iterate.

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

  3. A loop either running 1 to many times, or short 1 iteration.

1 Like

1. What can we do with the while statement? DO UNITIL a condition is met in a loop
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? its a loop where its condition is set to TRUE (1)
4. What is an iteration? moving through data 1 dataset (row) at a time until eof. End of file…etc

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 data is first defined, initialized, and changed the value of a loop variable with every iteration.
2. How would you write a for-loop in code?
int i = 0;

for (i=0;i<=10;i++){
cout << “Loop it!”<< endl;
}
3. What is an off-by-one error?
is an error that occurs when the loop iterates one too many or one too few times

1 Like
  1. The while statement makes it possible to repeat an action until some pre-determined outcome results.

  2. So long as the while statement evaluates as true, it will repeat.

  3. An infinite loop is a loop which continues forever and doesn’t have an logical endpoint.

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

Next Section:

  1. We use a for loop when we have predetermined number of iterations we plan to perform, and use a while loop when we are unsure of the number of iterations which will be necessary.

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

  1. An off-by-one error is an error that occurs because a programmer miscalculated by the needed number of iterations by 1, often because they chose the wrong operator to compare with.
1 Like
  1. With a while loop you can continuously execute a statement as long as the condition is true. Once the condition becomes false the while loop breaks.
  2. The value needs to be true for the loop to continue.
  3. An infinite loop is a while loop that executes forever as the condition is always true.
  4. An iteration is every time a loop executes once.
    1.It is a good idea to use a for loop rather than a while loop when you are looking to use loop variables. With for loops you can easily define, initialize, and test these variables.
  5. for(init expression;condition;end expression)
    statement;
    3.An off by one error is an error common in new programmers that occurs when the wrong operator is used in the conditional part of a for loop resulting in a result that may be off by one value in either direction.
1 Like
  1. When a while statement is executed, the condition is evaluated. If the condition evaluates to true , the associated statement executes.
    2.TRUE
  2. if the expression always evaluates to true, the while loop will execute forever. This is called an infinite loop .
    4.each loop is an iteration

1.when you know the numbers of iterations
2.for (int i=0);
3.when the code iterates one to many or one to few times.

1 Like

This is only the starting condition, a for must also contain the step and end expression. :slight_smile:

1. What can we do with the while statement?
Repeat a function until a specified 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?
a loop where the condition always evaluates as true

4. What is an iteration?
a single pass through 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?
When it is known how many times the loop needs to be iterated

2. How would you write a for-loop in code?
for (int count = 0, count <=10, ++count){
cout << “Looping…” << endl;

3. What is an off-by-one error?
iterating through a loop one more or one less time than expected, usually a result of an incorrect comparison operator in the condition statement.

1 Like
  1. We can do a while loop when we want to count numbers in a large quantity.

  2. The value of the initialized count {} must be true in the while statement (like an if statement) in order to continue. For example in the while loop of:
    {
    int count {1};
    while(count <10)
    }
    The count of 1 must be lower than 10, once the count is 10 or 11 it will stop running.

  3. An infinite loop is when the loop is always true and the loop evaluates forever. There are also intentional infinite loop when you set the while loop to be true.

  4. An iteration is the number of times a loop executes.

  5. A for loop is for obvious loop variables.
    2.#include
    int main()
    {
    for(int count{1}; count<=10; ++count)
    std::cout<<count<< ’ ';
    return 0;
    }

  6. An off-by-one error is when your loop is when your loop iterates over one too many or one too few values.

1 Like

Can someone please help me. I do not understand why my C++ code will not print to the console 100 times, rather just one time.
Here is my code:
c++

My while loop does NOT work either.

Hmm, the code should work fine. Is this the entire code? You should define a main function, but without it the code should throw an error instead of printing once:

int main()
{
    for(int i = 0; i < 100; i++) {
        cout<<"Hello World";
    }

    return 0;
}

1.1. With a Wile loop we can repeat functions.
1.2. true.
1.3. a loop that never ends.
1.4. The statements that cause a set of statements to be executed repeatedly either for a specific number of times or until some condition is satisfied are known as iteration statements.

2.1. if we know exaaaactly how much we have to repeat the loop.
2.2. for(int i=0; i<100; i++){
cout << i << endl;
}
2.3. When the loop stops 1 to early or 1 to much.

1 Like
  1. What can we do with the while statement?
    It is the simplest of of the three loops C++ has. It will continue to execute the associated statement if the while statement remains true.

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

  3. What is an infinite loop?
    the loop that will never terminate if the statement is not incremented.

  4. What is an iteration?
    An iteration is where each time a loop executes.

  5. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    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.

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

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

1 Like

While statements:

  1. We can perform repetitive actions
  2. Counter value
  3. A loop that never evaluates to false
  4. A single execution of statements in a loop

For statements:

  1. For statements are better when we have a definite number of time we need to iterate
  2. for (init-statement; condition-expression; end-expression) statement
  3. Where the condition-expression loops one too few or too many times
1 Like

Its a condition that must evaluate to true, this is same with any loop. For loop has a condition expression that must again evaluate to true for the loop to continue. :slight_smile:

1 Like

Part I

  1. With the while statement we can execute a statement repeatedly, until the value of condition becomes false. The test takes place before each iteration.
  2. The statement in the brackets has to be equal to true in order for the loop to continue.
  3. We can get an infinite loop if the expression always evaluates to true. In this case the while loop will execute forever.
  4. Each time a loop executes, it is called an iteration.

Part II

  1. 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. for (int count{ 1 }; count <= 10; ++count)
    std::cout << count << ‘\n’;

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

1 Like