Loops in C++ - Reading Assignment

First part:

  1. Loop a code.

  2. We can make a code run as long as the expression inside the loop paranthesis is true.

  3. A loop that never can break because the expression never gets false. It gets stuck.

  4. An iteration is an execution of the loop. So each time the loop executes it is iterating.

Part two:

  1. When you know exactly how many times you want to iterate the loop.

Example:
for(int i = 0; i < 10; i++)
{
cout << "Printing number: " << i << endl;
}

  1. It is when a for loop is running one iteration too long or one iteration too short due to a miss in the expression in the loop. Maybe instead of a “>” it should have been a “>=”.

1. What can we do with the while statement?
When a while statement is executed, the expression is evaluated. If the expression evaluates to true (non-zero), the statement executes.
Once the statement has finished executing, control returns to the top of the while statement and the process is repeated.

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

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

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, 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?
A for statement can be defined in abstract as:

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

A sample for loop and will be the following:

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

Source: Learn C++

2 Likes

What can we do with the while statement?

  • set up the requirement to run the following codes
    What value needs the statement in the brackets evaluate to in order for the loop to continue?
  • a value that make the bracket statement true
    What is an infinite loop?
  • a loop never stop
    What is an iteration?
  • each loop is 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?

  • for loop: when we know exactly how many times we need to iterate
    How would you write a for-loop in code?
  • for(i=0; i<=10; 1++)
    {}
    What is an off-by-one error?
  • the loop iterates one too many or one too few times.
1 Like
  1. We can cycle through a block of code until the condition is met.
  2. While the condition is met
  3. An infinite loop is a loop without end.
  4. An iteration is one cycle of a loop.

  1. When you know exactly the number of an iteration needed
  2. for(int i=0; i < numOfIterations; i++){}’
  3. when your loop initiates one to many or few times;
1 Like

1. What can we do with the while statement?
we can create loops
***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?***it is a loop which never ends
4. What is an iteration? repeating the loop over and over again

At the bottom of the article you’ll see a quiz - make sure to do the quiz!

Read this article (http://www.learncpp.com/cpp-tutorial/57-for-statements/ ).

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 we know exacly how many times we need to iterare
2. How would you write a for-loop in code? for(int a = 0; a<10; a++){statement}
3. What is an off-by-one error?simple error where instead of < we use <= and vice versa, it occurs when loop iterates too many or too few times

1 Like

Part 1

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

  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 invite loop happen when the expression always evaluates to true.

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

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?
    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?
    int main()
    {
    for(int count=0; count<15; ++count){
    cout<< count<<endl;
    }
    return 0;
    }

  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, because the wrong relational operator is used in the conditional-expression (eg. > instead of >=)

1 Like

while statements
1.What can we do with the while statement?
We can execute a loop, that will keep repeating until the condition within the parenthesis is false.

2.What value needs the statement in the brackets evaluate to in order for the loop to continue?
The statement in the brackets need to be evaluated to the Boolean value ‘true’ for the loop to continue.

3.What is an infinite loop?
An infinite loop is when the statement in the brackets is always true, no matter how many iterations the loops undergoes, causing the loop to keep repeating till infinity.

4.What is an iteration?
An iteration is when the loop repeats once.

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?
We use a for statement when we know precisely how many times we want the loop to repeat, as compared to the while loop which fulfills certain conditions before stopping.

2.How would you write a for-loop in code?
We can write a for loop in the certain format

for(initialization; condition; update)

The first part initializes the loop variable, while the second part declares the condition which the loop will repeat upon if this condition is true. Lastly, the last part is how the loop variable changes every iteration.

3.What is an off-by-one error?
An off-by-one error happens when the for loops repeats one less or more times than the creator initially intended. This can be caused by the wrong kind of relational operator in the conditional-expression. For example, instead of using >, we used >=. This might result in an extra iteration occurring.

1 Like
  1. Use it for a while loop

  2. true

  3. A loop that goes on and on forever

  4. A complete cycle of a loop

Part 2

  1. 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. for(counter=0;counter < 10; counter++){
    std::cout << counter << endl;
    }

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

1 Like
  1. we can create looping structures.
  2. true
  3. no end conditions are reached, meaning the boolean condition is always true.
  4. one execution of a loop

1.when compactness is required
2. for ( start-condition of loop; boolean condition; next loop state) { code}
3. going one beyond the loop boundary.

1 Like

Loops Part 1

1. does the same as in JS. does a loop while a condition is true. 2.needs a condition to be false to continue to loop. 3.an infinite loop is one where the program executes a group of statements forever. 4.an iteration is each time a loop executes.

Loops Part 2

1. when you do multiple declarations although this affects readability although _for_ or _while_ can be structured to do the same thing. 2.for (int counter = 0; counter <10; count ++) {statement} 3. this is a common error where the counter is out by 1 to break the loop.
1 Like
  1. While statement allows us to run the code continuously as long as the contitions in the statement are met
  2. It needs to eveluate true
  3. It is loop which runs endlessly due to not having terminating conditions or conditions not being met.
  4. It is single execution of the loop

1, It is to be used when we know how many times the loops is to be executed.
2, for (int count = 0; count <= x; count++)
3, It is error when the loop iterates one more or less times then needed. The reason is in using wrong relational operator ie < instead of <=

1 Like

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

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

What is an infinite loop?
if the expression always evaluates to true

What is an iteration?
Each time a loop executes

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

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

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

Loops in C++

1 We can executed a code section (like a function) many times, depending on how we write it (it can be infinite until some trigger stops it).

2 The value has to be true.

3 When the code section in the while’s range:

while(//condition){//the code in this range}

is executed infinite amount of times.

4 When the loop executes. If the loop is infinite, there will be infinite iterations.

For statements

1 for loops are good when we know how many iterations will happen. A while loop is usually used when we don’t know when it should stop (for example, the iterations depend on the user input).

2

for(int x = 0; x < 10; x++){ cout << x << " ";}

3 When we do one too few or one too many iterations. It usually a problem of using wrong relational operators in the conditional-expression (using > instead of >=)

1 Like

WHILE LOOPS

What can we do with the while statement?
We can perform the same code while a certain condition is met.

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

What is an infinite loop?
This is a loop where no changes ever happen, and the condition will always be true, so the code will continue to run indefinitely.

What is an iteration?
An iteration is a repetition of the code, in this case, while the condition is true.

FOR LOOPS

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 iterations we want or need to do.

How would you write a for-loop in code?

for(int count=0; count <10; count++)
std::cout << count << " ";

What is an off-by-one error?
These are errors related with the wrong relational operator being used in the conditional-expression (eg. > instead of >=)

1 Like
  1. What can we do with the while statement?
    Loop a block of statements on the condition of a boolean

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

  3. What is an infinite loop?
    A while loop that never produces a false condition

  4. What is an iteration?
    1 run of a loop

QUIZ 1:
Inner is located inside of the outer-loop just like outer is located just outside of the outer loop. We do this because we want inner to run to completion every time outer runs once. It also grants us different results each outer iteration.

Quiz4; int length = 1;
while (length <= 5)
{
int width = 5 -length;

   while (width >= 1)

{
cout << “X” << width–<< "\b ";
}

int inner = length;
    while (inner >= 1)

{
cout << inner-- << " ";
}
cout << “\n”;
length++;
width–;
}

  1. When is it a good idea to use a for statement (do a for loop)instead of the while loop we learned previously?
    Used when we know how many loops we want
  2. How would you write a for-loop in code?

For (int counterVariable = 0, counterVariable < 8 (or an int); ++counterVariable) //runs a for loop statement below this 8 times
3. What is an off-by-one error?
When <=, >= are used instead of <, > and the results are off by one

1 Like
  1. Repeat a process (loop).

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

  3. When the expression in a while loop always evaluates to true.

  4. Each time a loop executes.


  1. When we know exactly how many times we need to iterate.

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

  3. When the loop iterates one too many or one too few times.

1 Like
  1. While loops allows to run a code multiple time for as long as a condition is true.
  2. The true or false condition is located between the braces. At each turn, the condition is re-evaluated and continue as long as the value is true.
  3. In case the condition is never false, the loop will continue until the program crashes.
  4. An iteration is one turn or one loop
  5. The do-while loop is exactly like the regular loop except the code it contains is at least executed once.
  6. for(int i=0 ; for i<10 ; i++){code}
    or
    for(int i=0 ; for i<10 ; i++) code
  7. Beginners often have a hard time evaluating how many times the loops will iterate and may iterate one time too much or too less. This can be fixed by manually testing.
1 Like
  1. What can we do with the while statement? We can loop through a block of code whilst a particular 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? Code executed over and over because the statement evaluated is always True
  4. What is an iteration? Each time the code in a loop is executed
1 Like
  1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    We prefer using the for loop instead of the 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. How would you write a for-loop in code?
    for (init-statement; condition-expression; end-expression)
    statement;

or

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

statement;
}

Where init-statement is initializes the variable whose will be tested in the condition-expression and modified in the end-expression. Condition-expression is the expression that is evaluated whereby if the condition-expression evaluates to true the statements in the for loop will execute and if the condition-expression evaluates to false the body of the loop will be skipped. When the condition-expression evaluates to true then after the for loop finishes an iteration the end-expression modifies the variable initialized in the init-statement (either incrementing or decrementing the variable by some amount).

Example of a for loop:

for (int cycle = 1; cycle < 300; ++cycle)
cout << “ The current iteration of cycle is: “ << cycle << endl;

  1. What is an off-by-one error?
    The computational error that occurs when the for loop iterates either one too many time or one too few times. This generally occurs when the coder uses the wrong relational operator (< vs. <= or > vs.>=) in the conditional expression.
  1. Write a for loop that prints every even number from 0 to 20.

for ( int counting = 0; counting <=20; counting += 2)
std::cout << counting << std::endl;

  1. Write a function named sumTo() that takes an integer parameter named value, and returns the sum of all the numbers from 1 to value.
    For example, sumTo(5) should return 15, which is 1 + 2 + 3 + 4 + 5
    int sumto(int value)
    {
    int total = 0;

    for (int counter = 1; counter <= value; ++counter)
    total += counter;

    return total;
    }

  2. What’s wrong with the following for loop?
    1
    2
    3 // Print all numbers from 9 to 0
    for (unsigned int count=9; count >= 0; --count)
    std::cout << count << "
    count is an unsigned integer which means it will always be greater than 0 so the condition count >= 0 will always be true and this code will cause an infinite loop

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

When a while statement is executed, the expression is evaluated with the expression to true(no-zero), the statement executes. But once the statement has finished executing, control returns to the to[ of the while statement and the process is repeated.

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

True statement, once its false, then the while loop will be done.

  1. What is an infinite loop?

When an expression always evaluates as true, this will make the loop execute forever.

  1. What is an iteration?

An iteration is when 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?

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

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

Quiz

  1. In the above program, why is variable inner declared inside the while block instead of immediately following the declaration of outer?

This because you want to start counting inward by starting from the smallest variable and then expanding the loop out.

  1. Write a program that prints out the letters a through z along with their ASCII codes. Hint: to print characters as integers, you have to use a static_cast.
    image
    image

  2. Invert the nested loops example so it prints the following:

5 4 3 2 1

4 3 2 1

3 2 1

2 1

1
image

  1. Now make the numbers print like this:

1

2 1

3 2 1

4 3 2 1

5 4 3 2 1

hint: Figure out how to make it print like this first:

X X X X 1

X X X 2 1

X X 3 2 1

X 4 3 2 1

5 4 3 2 1

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

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

Its like writing a loop similar to how we write a while loop, but there are additional criteria to consider which are broken down in three parts:

  • The init-statement is evaluated. In most cases, the init-statement consists of variable definitions and initialization. This statement is only evaluated once, when the loop is first executed.
  • The condition-expression is evaluated. If this evaluates to false, the loop terminates immediately. If this evaluates to true, the statement is executes.
  • After the statement is executed, the end-expression is evaluated. For the most part, this expression is used to increment or decrement the variables declared in the init-statement. After the end-expression has been evaluated, the loop returns to the second step mentioned with this answer.
  1. What is an off-by-one error?

Off-by-one errors occurs when the loop iterates one too many or one too few times. This happens because the wrong relational operator is used in the conditional-expression, like > instead of >=. The errors are hard to detect because the compiler will not error them out, but will continue with the program with the wrong results. The “KEY” here is to remember that loop will execute as long as the conditional-expression is “TRUE”. Plus, in reading the “off-by-one error, it’s a good idea to test your loops to make sure that you get the correct results.

Quiz

  1. Write a for loop that prints every even number from 0 to 20.
    image

  2. Write a function named sumTo() that takes an integer parameter named value, and returns the sum of all the numbers from 1 to value.

For example, sumTo(5) should return 15, which is 1 + 2 + 3 + 4 + 5.

Hint: Use a non-loop variable to accumulate the sum as you iterate from 1 to the input value, much like the pow() example above uses the total variable to accumulate the return value each iteration.
image

  1. What’s wrong with the following for loop?

1

2

3 // Print all numbers from 9 to 0

for (unsigned int count=9; count >= 0; --count)

std::cout << count << " ";

This loop will run forever because when the count>=0 it will run until the count is negative but since the count is unsigned will never go negative. The “KEY” here is to not loop on unsigned variables. Although I am still not certain when we will would use an unsigned loop.

1 Like