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.


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

#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

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