Loops in C++ - Reading Assignment

  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.

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

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.

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.

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.

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”)

1 Like
  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.

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.

  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

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.

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 >=).

Part I

http://www.learncpp.com/cpp-tutorial/55-while-statements/

What can we do with the while statement?

Create loops.

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

The expression needs to evaluate as true in order for the loop to continue.

What is an infinite loop?

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

What is an iteration?

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

Part II

http://www.learncpp.com/cpp-tutorial/57-for-statements/

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.

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. This generally happens because the wrong relational operator is used in the conditional-expression (eg. > instead of >=).

Part I

  1. We can iterate a process until the condition change
  2. The condition needs to be true
  3. A loop where the condition is always true
  4. An iteration is a repetition

Part II

  1. When we know exactly the number of iterations
  2. for (int number = 0; number < 10; ++number) cout << number << endl;
  3. This is when you miss one value for instance starting at 1 instead of 0.

1- It is possible to do loops with it.
2- It needs to evaluate to true.
3- It’s a loop that runs forever, until it is manually exited.
4- Each time a loop executes it is called a iteration.

1- It is better for use when we know exactly how many times the loop needs to be iterated.
2- for (init-statement; condition-expression; end-expression) statement;
3- It is an error pretty common with beginners, when the loop executes one time more or one time less than expected, usually caused by misuse of the less than or greater than operators.

  1. looping something until the statement in the brackets is false
  2. true
  3. a loop that never stops, because the testing statement always returns true
  4. one execution of the code block that loops

QUIZZ:

  1. when you know exactly how many iterations you need
  2. for(int counter = 0; counter<10; counter++){
    //do Stuff
    }
  3. a coding error that leads in a loop that does 1 iteration too much or 1 too few.
  1. What can we do with the while statement? The while statement is the simplest of the four loops that C++ provides, and it has a definition very similar to that of an if statement : while (expression) 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. However, unlike an if statement , 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? True (boolean).

  3. What is an infinite loop? A loop that repeats forever, never stops. 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? The for statement looks pretty simple in abstract:

    for (init-statement; condition-expression; end-expression)
    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. 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.

Part 1

  1. Loop until the expression evaluates to true, the statement executes.
  2. True
  3. A loop that always evaluates to true, is an infinite loop.
  4. Each time a loop executes, it is called an iteration .

Part 2

  1. 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. for (int count=0; count < 10; ++count)
  3. Off-by-one errors occur when the loop iterates one too many or one too few times.

#letsgetthiscrypto

1. What can we do with the while statement?
loop until condition is met. (become false)
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
1=true
3. What is an infinite loop?
A loop that repeats itself (make new iterations repeatedly) as its expression always evaluates to true.
4. What is an iteration?
A single execution of the loop code block.

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 iterations required and when the executions statements/end expression(s) are relatively simple .
2. How would you write a for-loop in code?
for(int count =0; count<=12; count++){
e.g. cout << count << end1;
}
3. What is an off-by-one error?
coding error results in an unrequired number of iterations (too much or too little)