Loops in C++ - Reading Assignment

PART II

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?
Syntax:

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

Example:

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

3. What is an off-by-one error?
One of the biggest problems that new programmers have with for loops (and other kinds of loops) is off-by-one errors. 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?
    With the while statement we can repeat a block of code while certain condition is true.

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    The value that evaluate the while statement it has to be true to execute the block of code between the curly brackets , and it s going to repeat the process till the value of the condition is false.

  3. What is an infinite loop?
    An infinite loop occurs when the condition to evaluate for the while loop is always true. The program never stop executing the loop.

  4. What is an iteration?
    Each time the loop repeat the code inside the curly brackets is an iteration.

  5. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    Usually we use a for loop when we know the exactly times we want the code to be executed.

  6. How would you write a for-loop in code?
    for(int i =0 ; i < (some expression) ; i++) {
    code to be executed
    }

  7. What is an off-by-one error?
    The off-by-one error occurs because in programming we usually start counting in zero(0) , and it can be confuse when in the expression in the for loop we use < or <= , or similar and we have a difference by one more or one less if we made the error.

1 Like

PART ONE:

1. What can we do with the while statement?

We can execute statements.

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 an expression that always evaluates to true.

4. What is an iteration?

An iteration is when a loop executes each time.

PART TWO:

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 precisely how many times we need to iterate.

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

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

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

When the loop iterates one too few or many times.

1 Like
  1. We can setup a loop, that will run as long as some statement holds true
  2. It needs to evaluate to boolean value “true”
  3. It is a loop, that will run forever, because the statement will always be true, so nothing will stop the loop from running
  4. This is a process of running a specific loop once - executing all the code inside once
  5. When we exactly know how many times we want that loop to run
  6. for(initial value; statement; step)
  7. It is a mistake when we incorrectly run the loop either one time less, or one time more than we initially wanted - due to the misunderstanding of comparison logic operators (<=, =…)
1 Like

First part:

What can we do with the while statement?
Execute a piece of code while the condition defining the loop evaluates to true.

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

What is an infinite loop?

It is a loop where the condition required to execute the loops always evaluates to 1.

What is an iteration?
It is one of the instances or executions of a loop. For instance, in an infinite loop, there are an infinite number of iterations.

Second part

When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
When, beforehand, you know the number of iterations needed to accomplish the required task.

How would you write a for-loop in code?
for(initial statement; condition; final-statement)

What is an off-by-one error?
it is an error when the loop iterates one too many or one to few times

1 Like

Part 1

  1. Execute some block of code repeatedly while a given condition remains true.

  2. The value of the expression must evaluate to True or 1 for the code block to execute an iteration.

  3. An infinite loop executes its block of statements indefinitely.

  4. An iterations is a single executions of a loop’s code block.

Part 2

  1. Anytime that you know the number of iterations a given loop will take you could use a for loop.

for(int count = 0; count < 10; count++) {
    std::cout << "Something " << count << std::endl;
}
  1. Off-by-one is a logic error that arises from the exit condition of a for loop being declared off by one in its required iterations.
1 Like

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.

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

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.

Quiz

  1. Variable inner is declared inside the while block so that it is recreated and reinitialized to one.
    #include
    using namespace std;

int main()
{
char alphabet = ‘A’;
while (alphabet <= ‘Z’)
{
cout << alphabet << " " << static_cast(alphabet) << “\n”;
++alphabet;
}

return 0;

}

#include
using namespace std;

int main()
{
int outer_loop = 1;

while (outer_loop <= 5)
{
	int inner_loop = 5;

	while (inner_loop >= 1)
	{
		if (inner_loop <= outer_loop)
			cout << inner_loop << " ";
		else
			cout << "  ";

		inner_loop = inner_loop-1;
	}

	cout << "\n";

    // row completed start on next one
	outer_loop = outer_loop+1;
}

return 0;

}

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

2. How would you write a for-loop in code?
for (init-statement; condition-expression; end-expression)
statement
eg:
for (int test=1; test < 100; ++test)

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
  1. With while loop we can repeat a block of code until the break condition is met.

  2. For the loop to continue, the value of the statement in the brackets has to be true.

  3. Infinite loop is a loop with the statement in the brackets set to true. It may never exit if there is no return/break sentences.

  4. Iteration is one execution of the loop code.

  5. For loop is usually used when we know how many times we will have to iterate because it is simpler to handle.

  6. for(int i=0; i<10; i++) {…}

  7. Off-by-one error occurs when loop runs one time too much or one time to few.

1 Like

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