Loops in C++ - Reading Assignment

#101
  1. What can we do with the while statement?
    Use for looping

  2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    The brackets evaluate is true then the loops continue.

  3. What is an infinite loop?
    The loop that never ends.

  4. What is an iteration?
    iteration is the process of every loop.

  5. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
    If there a fix times you want to loop is better to loop with for loop.

  6. How would you write a for-loop in code?
    the count of the number of times to loop and how it is incremented or deremented

  7. What is an off-by-one error?
    Generally is due to newbie who iterate once more or one less than expected due to the operator used in for loops.

0 Likes

#102

while statement

  1. What can we do with the while statement? It enables us to execute and loop the statement/s in it so long as an expression (a condition) is true.
  2. What value needs the statement in the brackets evaluate to in order for the loop to continue? It should be true (non-zero)
  3. What is an infinite loop? It is a loop wherein the expression always evaluates to true and so will continue executing forever.
  4. What is an iteration? An iteration is each execution of a loop.

for 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? It is good to use when we know exactly how many times we need to iterate. It is also more compact than while statements.
  2. How would you write a for-loop in code?
     for (init-statement (declaring a loop variable); condition-expression; end-expression)
       statement;

Inside the parentheses:

  • Define and initialize variable for first execution;
  • Expression where variable is evaluated against a condition;
  • Expression of how a variable is incremented or decremented at the end of the statement before going back to condition-expression
  1. What is an off-by-one error? It is an error made by a programmer in the conditional-expression that results to the loop iterating one too many or too few times.
0 Likes

#103
  1. What can we do with the while statement?
  • Loops
  1. What value needs the statement in the brackets evaluate to in order for the loop to continue?
  • “true”
  1. What is an infinite loop?
  • A loop whose statement is always “true” or “1”
  1. What is an iteration?
  • Going through the loop once
  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
  1. How would you write a for-loop in code?
  • for (init-statement; condition-expression; end-expression){
    //code
    };
  1. What is an off-by-one error?
  • Off-by-one errors are when a loop iterates one too many or one too few times
0 Likes

#104
  1. What can we do with the while statement?
    You can use a while statement to loop, with a definition very similair to an if statement.

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

  3. What is an infinite loop?
    When the expression always evaluates to true and thus never breaks out of the loop.

  4. What is an iteration?
    Each time a loop executes the block of code

  5. 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 exactly how many times you want to iterate.

  6. How would you write a for-loop in code?
    for (int count=0; count < 10; ++count)

  7. What is an off-by-one error?
    An error that often happens when the wrong relational operator is used in the conditional expression.

0 Likes

#105

What can we do with the while statement?

We can make a loop that will do something as long as the argument is true.

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

The statement need to be true.

What is an infinite loop?

An infinite loop ocurse when the argument can’t become false.

What is an iteration?

Each time a loop is done its an iteration, and it could be used to count each loop.

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 better to use a for loop, when you know how many times you will have to iterate.

How would you write a for-loop in code?

For(i =0; i < 0; i++)

What is an off-by-one error?

It’s a simple error where you forget to declare the right symbol in your code for example ”<” which will eliminate the last iteration number from your code or ”–” when really want to increment your code. It won’t show you any error.

0 Likes

#106

1. What can we do with the while statement?
We can set a segment of code to be executed repeatedly as long as a certain condition is met.

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

3. What is an infinite loop?
It occurs when the conditional statement is always true, hence the code inside the while braces executes indefinitely.

4. What is an iteration?
An iteration is a single full execution of the 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 how many times we want the iteration to occur.

2. How would you write a for-loop in code?
for(initial statement; conditional expression; end expression){
statement;
}

3. What is an off-by-one error?
It’s a type of error where the loop iterates one-too-many or one-too-few times than we expected it to. The error appears as a result of poor conditional expression definition.

0 Likes

#107

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.

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

What is an infinite loop?
A loop without an ending expression

What is an iteration?
Each time a loop executes, it is called 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?
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?
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.
When writing for loops, remember that the loop will execute as long as the conditional-expression is true. Generally it is a good idea to test your loops using known values to make sure that they work as expected. A good way to do this is to test your loop with known inputs that cause it to iterate 0, 1, and 2 times. If it works for those, it will likely work for any number of iterations.
Rule: Test your loops with known inputs that cause it to iterate 0, 1, and 2 times.

0 Likes

#108

1. What can we do with the while statement?
Set a constant variable under one condition.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
At least 2 values, the variable to evaluate in the loop and a the condition to met.
3. What is an infinite loop?
A loop that never met the condition.
4. What is an iteration?
The number of cycle that u want to make a variable runs in 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?
while is one condition, a for statement use 2 conditions, the 1st condition is to set a variable to know where the iteration start, the 2nd one is to define until when the iteration will run.
for(int i=0; i <=2;++i) {} >> variable “i” is the 1st condition, “i<=2” is 2nd condition.
2. How would you write a for-loop in code?
for(int i=0; i <=2;++i) {cout<<“SMASH THE LIKE BUTTON TIMES:”<<i<<endl;}
3. What is an off-by-one error?
when the loop iterate many times more or less than 2nd condition, this error is commonly showed for a bad use of “< >” and “<= >=” comparators. (run the for statement over this question, change the “i<=2” for “i<2”)

0 Likes

#109
  1. We can evaluate if the expression is true and run until is no longer true.

  2. True

  3. A loop when the expression always evaluates to true, and as a results, the while loop will execute forever.

  4. Each time a loop is executed.

  5. When the number of iteration is known.

  6. int i = 0
    for (i=0; i<10; --i){
    cout <<" "<< endl;
    }

  7. When a for loop by mistake is run (iterated) one time less or one time too many.

0 Likes

#110
1. What can we do with the while statement?

The while statement allows you to do a loop. The control flow is such that as long as the defined expression is evaluated to true, the statement is executed and control flow comes back to the top of the statement and the process is repeated, which is different from an if statement.

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

It needs to value to be true, I.e non zero.

3. What is an infinite loop?

It is a loop that never ends its execution. The value is always true therefore the loop executes on and on. This needs to be killed by a return, break, exit or goto statement, having an exception thrown or the user killing the program.

4. What is an iteration?

An execution of the loop.

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

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

Curly brackets being unnecessary should the loop have only one statement.

0 Likes

#111
  1. A while loop allows the programmer to execute the code inside the loop when a certain condition is true

  2. The value within the brackets of the loop must be ‘true’ (boolean typpe) in order to continue

  3. An infinite loop is one that goes on indefinitely because the expression being evaluated within the brackets is always true.

  4. Each time that a loop is executed is known as an iteration


  1. A for loop would be used when it is known in advance how many times we need to iterate over the loop whereas a while loop would be used where we do not know in advance how many times we need to iterate over the loop

  2. This is an example of writing a for loop that prints out number to 10:

int i = 0;
	
for (i=0;i<=10;i++){
	cout << "Number is: "<< i << endl;
}
  1. A one off error occurs when a loop executes an incorrect number of times, i.e it executes one too many or too few times
0 Likes

#112

1. What can we do with the while statement?
You can execute logic in a loop while a logic test remains 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 which continues forever.
4. What is an iteration?
Each run through the loop is 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?
It is more compact and saves time. It is very useful if you know how many times you will need to iterate.
2. How would you write a for-loop in code?
for( int i = 0; i <=10; ++i){
code goes here
}
3. What is an off-by-one error?
If you use a less than sign instead of a less than or equal sign (same with greater than/equal) and are not careful your program may loop one too many, or one too few, times.

0 Likes

#113

1. What can we do with the while statement? The while statement is very useful to execute blocks of code within the limits of certain conditions being met. The code will exit from the while statement after the limit set by the while statement is met or exceeded until then, it runs the code within the statement in a continuous 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 that has a condition that never gets met (eternally true) this type of loop can be broken out of with a break statement. This is a useful loop for menus that query the user and will keep asking for correct answers until the right answer is given, at which time the loop is ‘broken out of’ and a menu or some other part of the program continues.
4. What is an iteration? An iteration is a loop cycle, isolated into its own. For example, one iteration of a for statment states what the program will do in one ‘go through’ for the for 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? It depends on if there is a variable needed. The advantage of the for loop in iterating through an array, for example, is that you can address the element in the array at the specific iteration that you are on ( array[x] ). Otherwise, the two methods of repeating code are identical.
2. How would you write a for-loop in code? for (var i = 0; i < array.length; i++) { // code to run goes here } where i is the variable that is used to iterate through the block of code, the middle section is the limit of the iterations, and the third section is the step amount that you want to do for each iteration (i++ stands for i += 1, but it could just as easily be i += 5 and that would increase i by 5 at the end of each iteration.
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 >=).

0 Likes