Loops in C++ - Reading Assignment

A to repeat a loop ubtil a condition is met
2 true
3 it does not end as a condition is never met
one loop.

1 Ideal when we know exactly how many times we need to iterate
2 for(int i=0; i < length; i++) { /* do stuff here*/ }
3 repeated one too many or one too few times.

1 Like

cannot break out of this loop, what am I doing wrong?
/*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.
  • */
    //#include
    #include

//using namespace std;

/*
*
*/
int main(){
int count = 1;
while (count <= 50);

{
if (count < 10)
std::cout << “0” << count << " ";

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

if (count %10 == 0)
    std::cout << "\n";

++count;

std::cout 
        << "Input a keyboard character: ";

char ch;{};
/*
std::cin
        >> ch
        ;
*/
std::cout
        << ch <<
        " has ASCII code "
        << static_cast<int>(ch) << count << " ";

}

return 0;

}

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. But if the expression evaluates to false, the statement stops to execute.
What value needs the statement in the brackets evaluate to in order for the loop to continue? The value is “while”.
What is an infinite loop? It is when an expression is always true, the loop will continue forever.
What is an iteration? Iteration is 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? 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.
How would you write a for-loop in code? A for statement is evaluated in 3 parts:

  1. The init-statement is evaluated.
  2. The condition-expression is evaluated. If this evaluates to false, the loop terminates immediately. If this evaluates to true, the statement is executed.
  3. After the statement is executed, the end-expression is evaluated
    for (int count{ 0 }; count < 10; ++count)
    std::cout << count << ’ ';
    First, we declare a loop variable named count, and assign it the value 0.
    Second, count < 10 is evaluated, and since count is 0, 0 < 10 evaluates to true. Consequently, the statement executes, which prints 0.
    Third, ++count is evaluated, which increments count to 1. Then the loop goes back to the second step.
    Now, 1 < 10 is evaluated to true, so the loop iterates again. The statement prints 1, and count is incremented to 2.
    2 < 10 evaluates to true, the statement prints 2, and count is incremented to 3. And so on.
    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 >=).
1 Like

Looks like you forgot to use the ++ in the function… so It dosent count the number…
Ivo

1 Like

I have better results with quiz but still am not getting proper ASCII static_cast

int main(){
int count = 1;
while (count < 50)

{
if (count < 10)
std::cout << “0” << count << " ";

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

if (count %10 == 0)
    std::cout << "\n";

count++;

/*std::cout 
        << "Input a keyboard character: ";
*/
char (ch);{};
/*
std::cin
        >> ch
        ;
*/
std::cout
        << ch <<
        "  ASCII code is "
        << static_cast<int>(ch) << " ";

}

return 0;

}

-----------------WHILE-statements---------------------------------------------

  1. What can we do with the while statement?
    Creating a while 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?
    An infinite loop is a loop, which will never stop to execute.

  4. What is an iteration?
    One execution of a loop = iteration

------------------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?
    When we know exactly, how many times we want to run the loop.

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

  3. What is an off-by-one error?
    Off-by-one error occur when the loop iterates one too many or one too few times. Happens mostly because wrong relational operator is used (e.g. > instead of >=)

1 Like

1. What can we do with the while statement?

Execute a block of code repeatedly until a 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 that never stops executing because the condition is never met (expression is always true).

4. What is an iteration?

An execution of the loop code block.

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?

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

statement

  1. The init-statement is evaluated. Typically, the init-statement consists of variable definitions and initialization. This statement is only evaluated once, when the loop is first executed.

  2. The condition-expression is evaluated. If this evaluates to false, the loop terminates immediately. If this evaluates to true, the statement is executed.

  3. After the statement is executed, the end-expression is evaluated. Typically, 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 step 2.

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 >=). These errors can be hard to track down because the compiler will not complain about them – the program will run fine, but it will produce the wrong result.

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

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue? In a while loop…TRUE.

  3. What is an infinite loop? A loop that will never trigger the exit requirement. In a while loop, the code will never produce a FALSE value.

  4. What is an iteration? To increment or increase/decrease a ‘counter’ variable in order to reach a specific value. Commonly used to trigger an exit statement.

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

    int count = 97;

    while (count<123){
        letter = count;
        cout << letter << "(" << static_cast <int>(letter) << ") ";
        ++count;
    };

1b. 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 exactly how many time we want to reiterate the code. They are more compact and can use less code.

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

for (iii = 0, jjj = 9; iii < 10; ++iii, --jjj)
    std::cout << iii << ' ' << jjj << '\n';

3b. What is an off-by-one error? A common beginners mistake where the counter or result is often off by one. Typically because a > or < intended target was misinterpreted.

1 Like

1. What can we do with the while statement?
we can create loops that can repeat indefinetily or until certain condition is met

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

3. What is an infinite loop?
a loop in which the statement inside the brackets is always true

4. What is an iteration?
it is a single execution of code inside 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?
depending on if we want to run the loop at least one time or a specific number of times

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

for (int i = 1; i <= 100; i++) {
//statements
}

3. What is an off-by-one error?
it’s when a loop fails to run the proper amount of times falling short of one or running one extra time

1 Like

While loops

  1. Repeatedly execute the same block of code while a certain statement evaluates to true.
  2. 1 (True)
  3. If the value in brackets always evaluates to true the loop will repeat forever.
  4. One execution of the code inside the loop.

For loops

  1. When you know exactly how many iterations the loop should have.
  2. for (int i {0}; i <= 10; ++i) { …}
  3. When mixing up the use of < and <= then for example, the loop could execute one too many times or one too few times.
1 Like

PART1:

  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.
  1. What value needs the statement in the brackets evaluate to in order for the loop to continue?
  • The value of the statement needs to be true
  1. What is an infinite loop?
  • An infinite loop is when a while loop will execute forever
  1. What is an iteration?
  • An iteration is each time a loop is executed

PART2:

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

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

Statement

}

  1. 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 >=).
2 Likes

What can we do with the while statement?
It can continue executing until the expression being evaluated is false.

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?
It is when the condition to execution the statement never ceases to be false meaning the loop never ends.

What is an iteration?
It is one complete execution of the loop.

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 before hand exactly the number of times you want the statement to execute.

How would you write a for-loop in code?
for (int count = 0; count < 10; ++count)
{
statement(s) to be executed;
}

What is an off-by-one error?
It is when the loop executes either one more or one less time than what is desired. This is usually caused by the incorrect use of a relational operator in the testing of the continuation expression.

1 Like

Answers

Part 1: while statements

  1. We can use while statement to create loops.

  2. In the while loop there is an expression (or condition) that it must be evaluate. As long as this condition is true the loop continue to perform the statement.

  3. When the expression in the brackets is always true, the loop will not stop this execution, so it creates an infinitive loop.

  4. Every time that a single loop executes, it’s called an iteration.

Part 2: for statements

  1. We can prefer do loops when we know exactly how many times we need to iterate.

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

  3. It’s a typical error of new programmers. It’s quite common to put a wrong condition into the brackets (eg > instead of >=).

1 Like

While statements

  1. The while statement loops through a block of code as long as a specified condition is true.
  2. true.
  3. If the expression always evaluates to true, and thus the loop does not exit, it is an infinite loop.
  4. It is the repetition of a process that generates an outcome. Each time a loop executes, it is called an iteration.
  1. When we know exactly how many times we need to iterate the expression.
  2. In abstract:
    for (init-statement; condition-expression; end-expression)
    statement

    For example:
for (int count{ 0 }; count < n; ++count)
    cout << count << ' ';
  1. An off-by-one error happens when the loop iterates one too many or one too few times. Typically, it happens when < is used instead of <=, or > instead of >=, or vice versa. It is hard to spot, because the compiler will not complain about it and the program work fine, but the result will be wrong.
1 Like
  1. We can write loops.

  2. True (non zero).

  3. A loop that always evaluates to true, and therefore executes forever.

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

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

  6. for (init-statement; condition-expression; end-expression)
    statement. For example, a for loop counting from 0 to 9:
    for (int count{ 0 }; count < 10; ++count)
    std::cout << count << ’ ';

  7. When the loop iterates one too many or one too few times. Usually caused by using the wrong relational operator (eg < instead of <= or vice versa).

1 Like

While statement

  1. Repeatedly execute a block of code while the provided statement is true

  2. True

  3. A loop that never stops, because the testing statement always returns true

  4. One time execution of the block of code defined within the loop

For statement

  1. When you know the increment and the number of iterations

  2. for(int i=0; i < length; i++)

  3. The error occurs when the loop iterates one too many or one too few times.

2 Likes

Part 1:

  1. What can we do with the while statement?
    To continue executing a bit of code while a set of conditions remain 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 where the conditions to run do not reach a logical conclusion.

  4. What is an iteration?
    Each execution of the code in a loop.

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 we know the number of iterations

  2. How would you write a for-loop in code?
    for (int iii {0}; iii < #; ++iii){}

  3. What is an off-by-one error?
    When the code runs one fewer or one more iteration than intended.

1 Like

WHILE LOOP

  1. What can we do with the while statement?

When executing while statement we evaluate the expression. If the expression is evaluated true, the statement executes. Once the statement has finished executing, control returns to the egining 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?

The statement executes If the expression evaluates to true (non-zero).

  1. What is an infinite loop?

Infinite loop will happen If the expression always evaluates to true. In that case the while loop will execute forever.

  1. What is an iteration?

Iteration is each time a loop executes.

FOR 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 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?
for (init-statement; condition-expression; end-expression)
statement
  1. What is an off-by-one error?

Off-by-one errors happens when the loop iterates one too many or one too few times. This happens because the wrong relational operator is used in the condition.

1 Like
  1. With while statement we can do 1 of the 4 loops .

  2. A true value is needed for the statement to be evaluated and the loop to continue.(not the number 0)

  3. A loop that cannot be stopped as it is never false , therefore it will crash your PC big time ahahah.

  4. One iteration is equal to the loop have gone full circle one time …and on and on…

  5. We could use the FOR LOOP when we know how many times we could execute the loop .

  6. for (int count=0 ; count <= 1000; ++count) {
    cout<<Buy me one Bitcoin<<endl;
    }

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

1 Like

1. What can we do with the while statement?
You can use a while to 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 the loop never break, because you expression/value is always true for example.

4. What is an iteration?
Each loop is call 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?
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. How would you write a for-loop in code?
Many possibilities, but an example :
for(int i=0; i<10; ++i) {
cout << i << endl;
}

3. What is an off-by-one error?
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