Loops in C++ - Reading Assignment

PART 1

What can we do with the while statement?
we can loop a number of times and execute statements inside that loop
What value needs the statement in the brackets evaluate to in order for the loop to continue?
we need loop variables to know how many times we have to repeat the block of code inside the loop
What is an infinite loop?
that is a loop that never ends
What is an iteration?
Every time we repeat the block inside the loop it is called an iteration

PART 2

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 want to use a more simple looking format we use the for loop
How would you write a for-loop in code?
for (cnt=1 ; cnt < 5; cnt++)
What is an off-by-one error?
that is when the loop does 1 iteration too many or 1 iteration too little
1 Like

PART 1

  1. What can we do with the while statement?
    It’s possible execute infinite times the same code until an expression evaluated to false.

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

  3. What is an infinite loop?
    Infinite loop happen when the condition expression never will evaluate to false.

  4. What is an iteration?
    It’s a loop execute.

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?
    It’s 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; after-statement) { // code }}

  3. What is an off-by-one error?
    Occur when the loop iterates one too many or one too few times.

1 Like

1. What can we do with the while statement?
We can execute the code inside while statement with the specified number of times set by the while condition.
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 condition will always be true and will never break.
4. What is an iteration?
it is a single execution of 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?
When we might need the iteration variable set and changed by the for statement.
2. How would you write a for-loop in code?

for(int i = 0; i < 10; i++)
{
    //put the code here so it will execute 10 times
}

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

1 Like
  1. We can make loops with while statements

  2. True value needs to be in the brackets for a while loop

  3. infinite loop, is a loop that always has a true value

  4. An iteration is a pass through a loop cycle once

  5. It is a good idea to use a for statement when you know how many times you want to go through an iteration.

2.for loop in code: for (int count=0; count < 10; ++count){ code }

  1. An off by one error is a common mistake, when a loop is executed by 1 more or less then wanted.
1 Like

What can we do with the while statement?
execute “while” it stays true
What value needs the statement in the brackets evaluate to in order for the loop to continue?
1 == true
What is an infinite loop?
where the statement in the brackets never turns to 0 == false (i.e. while (Ivan == Swedish){})

What is an iteration?
one execution of the body 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 Ivan isn’t reading these answers, or when you know exactly how many times you need the loop to execute.
How would you write a for-loop in code?
for (i=0; i < 5; i++) {}
What is an off-by-one error?
when you loop one too few or one to many times. If I could take the hours spend debugging these, get paid for them, and put that money into Bitcoin in 2010 . . .

1 Like

A while loop allows you to execute a sequence of instructions multiple times as long as a condition evaluates to true. Before each iteration (execution of the instructions contained within the while loop), the while loop evaluates the condition to make sure that the condition is still true. If the condition always evaluates to true no matter how many iterations are performed, the loop will be an infinite loop (iterate forever until the program is stopped, typically through external means like a task manager or a reboot).

If your while loop can be rewritten to neatly conform to the init-statement, condition-expression, end-expression pattern of a for loop, rewrite the while loop as a for loop. Consider the example below:

int i=0; // init-statement
while(i < 5 /* condition-expression */){
     // loop body
    std::cout << i << std::endl;
    i++; //end-expression
}

for(int i=0; i<5; i++){
    std::cout << i << std::endl;
}

An off-by-one error happens when a loop executes one too many or one too few times.

1 Like
  1. Loop a set of statements as long as a condition is true.
  2. True
  3. Loop that does not end or keeps running.
  4. A single pass of the loop content.

Second part

  1. When number of iterations are well known.
for(initialise counter variable; looping condition; counter variable update after pass){
     //content
}
  1. When the loop runs 1 too many or less times than required usually caused by using >= in place of > or <= instead of < etc.
1 Like

Part 1

  1. A while loop will repetitively execute its body of work as long as its condition is True (non-zero).
  2. The value in the while statement needs to evaluate to 1 (True) so the body continues to execute.
  3. An infinite loop is a loop that always evaluates to 1, which will have the body code executing forever.
  4. An iteration is a cycle within a loop. Meaning that 1 iteration means the body of the loop has executed once.
1 Like

Part 2

  1. A for-loop is a better option, instead of a while-loop, when we know exactly how many times we want to iterate through a loop.
  2. for(int i=10; i>0; i--){
      cout << i <<endl;
    }
    
  3. Off-by-one error is when you program iterates one too many/few times. This is because of the confusion between the comparator operators < <= >= >
1 Like

PART ONE

  1. Execute a loop until a condition is broken
  2. A true value will make the loop continue, a false value will break the loop.
  3. A loop that will last forever since the condition will always be true, no matter what.
  4. Each execution of the loop.

PART TWO

  1. Both can be used with no issues, but for is recommendable when we know exactly how many iterations the loop will require.
  2. Similar as in JavaScript: for (counting_variable; condition_to_mantain_the_loop; change_in_counting) rest_of_the_code_here;
  3. Is when the number of iterations if off by one iteration, either more too much or too little.
1 Like

1. What can we do with the while statement?

A loop that executes continuously until a condition is set by a logical operator.

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

The value true or 1.

3. What is an infinite loop?

An infinite loop entails a loop in which the statement remains true without any restriction to make the logical statement false.

4. What is an iteration?

An iteration is the execution of a statement that fulfills the condition but is eligible for the program to be recycled since the condition has not approached the defined limit or the false 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?

A for loop is suitable when an initial is placed, a defined iteration, and at the frequency of the iteration.

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

A for loop is defined by the syntax:

for (int i = 0; i < 10; i++){
// insert program to evaluate;
}

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

A statement that normally has no compile errors but the intention of the program does not achieve the desired result by an additional or decrease iteration. Usually, to fix the mistake, a logical error was made in the condition.

1 Like

PART 1

1. 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, following the while keyword, 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. The process ends when the expression evaluates to false. It is possible that a while statement executes 0 times; this is the case when the expression evaluates immediately to false. On the other hand, if the expression always evaluates to true, the while loop will execute forever. This is called an infinite loop.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
The expression between brackets needs to evaluate to true (non-zero) for the loop to continue; An expression evaluation to false stops the execution of the while.

3. What is an infinite loop?
When the expression between brackets never evaluates to false, the statements within the while-loop will be executed infinitely. The only way to exit an infinite loop is through a return statement, a break statement, an exit statement, a goto statement, an exception being thrown, or the user killing the program.

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

1 Like

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