Loops in C++ - Reading Assignment

  1. Let a function execute multiple times until the expression evaluates to false.

  2. True or non-zero

  3. If the expression always evaluates to true, the while loop will execute forever.

  4. Each time a loop executes.

  1. When you know how many times you need to repeat the loop.

  2. for (int count=0; count <= number; ++count){ }

  3. An error in a loop, that occurs when the loop iterates one too many or one too few times.

2 Likes

1. What can we do with the while statement?
The while statement is the simplest of the four loops that C++ provides.

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

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

4. What is an iteration?
Each time a loop executes its 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 know how many times we should iterate the loop.

2. How would you write a for-loop in code?
#include
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << i << “\n”;
}
return 0;
}

3. What is an off-by-one error?
An off-by-one error means that either there is one iteration too few to cover all elements, or one too many.

1 Like

1 Looping a piece of code as long as the condition is true.
2. The boolean true.
3. A while loop that will execute forever.
4. Each time a loop executes, it is called an iteration.

  1. When we know how many times we are intending to iterate
  2. for(int count {0}; count < 10; ++count){ cout<<count<<endl;}
  3. Off-by-one errors occur when the loop iterates one too many or one too few times
1 Like
  1. Using while loops, one can run a code multiple time for as long as a condition is true.
  2. True or false condition to be evaluated. For every loop, the condition to be kept evaluated as long as the value is true.
  3. Sometimes, the condition is never false, hence the loop keep running infinitely.
  4. Iteration means one turn or one loop.
  1. When we know exactly how many times we want to loop.
  2. for(int i = 0; i < 10 ; i++){code}
  3. New programmer often set the condition to evaluate in the loop incorrectly by using >, <, =. This causes error by looping one time too much or too less.
1 Like

What can we do with the while statement?

With a while statement we can execute a series of conditions on each iteration of a loop.

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

The value 1 or true needs to be evaluated in order for the loop to continue.

What is an infinite loop?

An infinite loop is a loop which always evaluates to true and thus will run to infinity.

What is an iteration?

When a loop starts it performs a condition check. if this check returns true it executes the following statements. It then returns back to condition test of the loop. This whole cycle is known as 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?

We would use a for loop instead of a while loop in cases where we need to initialize a variable, perform a condition check and change the variable all in one step. It is also useful when we know the number of iterations to make.

How would you write a for-loop in code?

// A typical for loop
for(int iii = { 0 }; iii <= 20; ++iii) {
    cout << iii << " ";
}

What is an off-by-one error?

An off by one error is caused by a badly formed condition test in a loop where the test returns one iteration more or less than what should really be returned and this can cause our programs to return an incorrect or not expected result.

1 Like

1. What can we do with the while statement?
We can use the while statement to loop.

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

3. What is an infinite loop?
When an expression or value will always be true so the loop will never break.

4. What is an iteration?
The amount of times the loop is called.

1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
Use a for loop when you know how many times the function needs to be iterated. Use the while loop whenever we’re not sure how many times it needs to be iterated.

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

3. What is an off-by-one error?
When you the loop is run 1 too many or too few times. This can be due to an unsigned integer with a misplace >=/ <= sign.

1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

1 Like

You can repeat a certain block as long as a condition is true.

True

A loop whose condition is always evaluated to be true.
It won’t be terminated automatically

It is the repeating of the code inside the loop.

1 Like

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

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
True(non-zero)

3. What is an infinite loop?
When the expression always evaluates to true.

4. What is an iteration?
Each time a loop executes, it’s 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 know how many times we need to iterate.

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

3. What is an off-by-one error?
When a loop iterates one too many or one too few times

1 Like

I think you need to add the JS course before C++ in the learning path.
Because you are saying as we said in JS and JS is not in the learning path

In my opinion its better to learn to learn a low level language before going higher because you will subconsciously know what is going on and will write better code in higher level languages as well.

1 Like

1. What can we do with the while statement?
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?
When the loop goes on forever

4. What is an iteration?
Each time a loop executes, it’s 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 know through how many iterations we’re going.

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.

1 Like

Think about the following questions while reading:

1. What can we do with the while statement?

We are able to automatically loop provided the argument(s) is/are true

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

the boolean true

3. What is an infinite loop?

this is a loop de loop de loop with no exit condition

4. What is an iteration?

to iterate is to execute the statements within the body of the function

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 is the loop of choice when the loop durations are precisely known

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

for (int-statement ; condition-expression; end - expression)
statement(s);

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

these are the most common errors made by developers in loops by not returning the expected outcome due to relational operator misusage.

2 Likes

1. What can we do with the while statement?
A while statement can be used to execute a piece of code until a condition is met. In other words loop trough code until the evaluated expression becomes false.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
As long the evaluated expression returns true the loop is executed.

3. What is an infinite loop?
This is a loop which is executed until a return break or exit statement is reached or the program is terminated.

4. What is an iteration?
Each time a loop is executed, this 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?
For loops are used when we exactly know how many times a loop needs to be iterated. Consequently while is used when we iterate a loop for a unknown number of times.

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

3. What is an off-by-one error?
This usually happens if the loop is iterated one time to few or one time to much. A common mistake is an error in the expression such as iii < X is used instead of iii <= X.

1 Like

Excellent answers sir, well documented! Please keep them like that :muscle:

Carlos Z.

2 Likes

Thank you for the compliment. Will try to keep it up.

Ralf

1 Like

1. What can we do with the while statement?
A while statement allows a block of code to be repeatedly executed while a certain condition is met.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
The statement in the brackets needs to evaluate to a boolean expression that returns either true or false.

3. What is an infinite loop?
An infinite loop is a loop that has no way to exit, such as no incrementing counter for example. If the conditional statement always evaluates to true, the code in the loop body will continue to execute indefinitely unless you provide a way to stop the conditional statement from being true (or break out of the loop - using a break statement for example).

4. What is an iteration?
An iteration is an occurrence of the body of the loop executing. If the conditional statement is true and the program reaches the end of the loop body, an iteration has occurred (and subsequently loops back to the start to see if the conditional statement still evaluates to true and continues looping if it is, in turn causing another iteration)

Section 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 a good idea if you know how many iterations of the loop you require. For example, if you wanted to print 10 lines of something or wanted to count up in 3’s exactly 50 times, etc. Personally, I usually consider a while loop as exactly that - a while statement (while ‘x’ is true do ‘y’), whereas I consider a for loop almost as an until statement (keep doing ‘y’ until ‘x’ is no longer true). This is not always the case, but most of the time it helps wrap my head around what I want to achieve and then define it accordingly.

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

     int treats = 5;
     for (int jellybeans = 1; jellybeans <= treats; jellybeans++){

         cout<<"You have not found all the jellybeans"<<"\n";
         cout<<"You must continue on your quest to find the remaining jellybeans"<<endl;
         cout<<"\n";

         if (jellybeans = treats)
         {
             cout<<"Well done, you have found all "<<jellybeans<<" jellybeans!!! .....mmmm, jellybeans"<<endl;
         }

      }

3. What is an off-by-one error?
An off by one error usually occurs during the < or <= part of a loop (or > or >= if decrementing the count). Quite often, new programmers (or distracted seasoned programmers) might end up using the wrong combination of start points and operators (for counting) for example they want to count 10 numbers and either start from 0 and use <= 10 and end up counting eleven numbers or they might start from 1 and just use the < operator before 10 and end up counting only 9 numbers.

1 Like

Only a true condition will continue the loop. :slight_smile: Based on your next answer you knew that, but just pointing it out.

The if in this statement is a bit unnecessary. You could fix the for loop to iterate to jellybeans < treats (making one less iteration! Yaaay performance). And just cout when the loop finishes :slight_smile:

2 Likes

Oh I misread that, yes I am aware that it needs to evaluate to true, thanks for clarifying.

I apologise as I was in a hurry earlier (I had to pick up my lad from school) - I shouldn’t have rushed it as I hadn’t quite finished what I really wanted to do. I wanted each iteration to provide a count of how many jellybeans I had found and how many were left. The way I had it just basically jumped through and only showed the last iteration.

I have 2 questions based on your response…

Would this show the number of jellybeans incorrectly in my cout code where I referred to how many jellybeans were found? Wouldn’t it say 4 instead of 5?

Wouldn’t this line show for each iteration though? I set the if statement so that the cout line saying congratulations, with the number of jellybeans found, would only show once 5 jellybeans had been found…



int treats = 5;
        for (int jellybeans = 1; jellybeans <= treats; jellybeans++)
        {
            if (jellybeans < treats)
            {
                cout<<"You have only found "<<jellybeans<<" jellybeans"<<"\n";
                cout<<"You must continue on your quest to find the remaining "<<treats - jellybeans<<" jellybeans"<<endl;
                cout<<"\n";
            }
            else
            {
                cout<<"Well done, you have found all "<<jellybeans<<" jellybeans!!! .....mmmm, jellybeans"<<endl;
            }

        }

I think you will find it makes more sense now...it shows each iteration with a counter of jellybeans and a remainder of how many left to find on the epic jellybean quest.

Thank you for your feedback and clarification. After reading your comments, I went back and fixed the code to correctly show what I originally intended to show. Playing with the position of the cout lines helped my understanding of why the other iterations weren’t showing up and then changing the if statement to an if & else pair that occurs earlier in the code got it working just right.

Thanks again.

1 Like

Sorry I was a bit careless with your code and didn’t check properly :slight_smile: Both of your examples are correct in fact I was just trying to explain you could get rid of your if statement and executed the last cout outside of the for loop. For that you would also have to declare jellybeans before the for loop like this:

    int treats = 5;
    int jellybeans;

    for (jellybeans = 1; jellybeans < treats; jellybeans++)
    {
        cout<<"You have only found "<<jellybeans<<" jellybeans"<<"\n";
        cout<<"You must continue on your quest to find the remaining "<<treats - jellybeans<<" jellybeans"<<endl;
        cout<<"\n";
    }
    cout<<"Well done, you have found all "<<jellybeans<<" jellybeans!!! .....mmmm, jellybeans"<<endl;

In this example the jellybeans count would be 5 since the for loop first increments the jellybeans and then checks the condition. So once outside for loop you would have 5 jellybeans! :slight_smile:

2 Likes