Loops in C++ - Reading Assignment


#1

Welcome to the discussion about the reading assignment about Loops in C++.

Leave your answers to the questions below in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please everything to the topic.

Read this article (http://www.learncpp.com/cpp-tutorial/55-while-statements/).

Think about the following questions while reading:

1. What can we do with the while statement?
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
3. What is an infinite loop?
4. What is an iteration?

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?
2. How would you write a for-loop in code?
3. What is an off-by-one error?


#2

1. What can we do with the while statement?
You can use a while to loop.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
While statement is repeated while the expression/value is true.

3. What is an infinite loop?
This is when the loop never break, because you expression/value is always true for example.

4. What is an iteration?
Each loop is call 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?
We use a for loop when we know how many times we need to iterate. We use a while when we have to loop during an undefined time.

2. How would you write a for-loop in code?
Many possibilities, but an example :
for(int i=0; i<10; ++i) {
cout << i << endl;
}

3. What is an off-by-one error?
It’s when you iterate one too many or one too few times. It can happens because you have a mistake in your conditionnal expression


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

  5. when you know exactly how many iterations you need

  6. for(int counter = 0; counter<10; counter++){
    //do Stuff
    }

  7. a coding error that leads in a loop that does 1 iteration too much or 1 too few.


#4

1. What can we do with the while statement?

  • loop until a condition is met or infinitely

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 whose expression always evaluates to true

4. What is an iteration?

  • a single pass through the loop execution

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 you know the increment and the number of iterations

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

 for(int i=0; i < length; i++) { /* do stuff here*/ }

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

  • iterate one too many or one too few times

#5
  1. What can we do with the while statement?
    Repeatedly execute a block of code while the provided statement is true

  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 with its statement fixed to 1 (True)

  4. What is an iteration?
    One time execution of the block of code defined within 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?
    Executing a loop a defined no. of times and in a predefined way, both known at the start of the loop, is preferably done with the for-loop
    e.g. iterating over an array one element at a time: because the size of the array is fixed and defined when the for loop starts and also the iteration step is predefined (+1)

  6. How would you write a for-loop in code?
    for (/declaring(optional)count variable and initialization/int count=0; /expresion based on count variable/ ; /count variable change/) {/code to be executed/}
    e.g. for (int count=0; count<length; count++) {/code block/}

  7. What is an off-by-one error?
    This errors occurs when the written loop code iterates one to many or one to few times.


#6

What can we do with the while statement?
Create an if/then like statement that loops back to itself if or while it’s conditions are true.

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

What is an infinite loop?
A loop where the value is always true.

What is an iteration?
The execution of a loop 0 or more times.

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 need to iterate the loop.

How would you write a for-loop in code?

string iwill [ ] = {"I", "will", "create", "a", "cool", "cryptocurrency","someday"};

for (int count=0; count < sizeof(iwill); ++count){
cout << iwill [count] <<’\n’;
}

What is an off-by-one error?
When you iterate at least once too many or too few times.


#7

1. What can we do with the while statement?
evaluate whether an expression is true or false, then a command is executed.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
Anything other than 0 which means expression is true.
3. What is an infinite loop?
An expression that always evaluates to true.
4. What is an iteration?
Each time the loop cycles a command

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 I need to define, initialize, and change the value of a loop variable with every iteration.
2. How would you write a for-loop in code?
for(int x =0; x>=12; ++x){
//cout << x << end1;
}
3. What is an off-by-one error?
when your loop initiates one to many or few times; usually occurs when defining the right expression or using an unsigned variable.


#8

Part 1:

What can we do with the while statement?
- We can execute code while some condition is true.

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

What is an infinite loop?
- A loop whose logic will never trigger a failure.

What is an iteration?
- One pass through a loop of code.

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?
- It is good to use a for loop if the number of iterations is known. A while loop is good if a condition needs to be met in order to execute code.

How would you write a for-loop in code?

int i = 0;
	
for (i=0;i<=10;i++){
	cout << "Loop it!"<< endl;
}

What is an off-by-one error?
- These are mistakes often made by new programmers which iterate through a loop an incorrect amount of times, namely off-by-one. Check the conditional operators (<,>,<=,>=,etc).


#9

First part:
1- We can do loops with while statement
2- While needs a true condition to continue looping
3- Infinite loop is a loop with its condition is always evaluated as true
4- Each time a loop is executed it’s called iteration

Second part:
1- When we know exactly how the loop have to be defined
2- for(int i=0; i<10; i++) { }
3- Off-by-one is a common mistake due to an error typing the condition expression. ie: > instead of >=


#10

While statements

1. Create a loop that executes until the expression evaluates to false.

2. true

3. A loop that will execute forever.

4. Each repetition of the statement inside the loop.

For statements

1. If we know exactly how many times we need to iterate.

2. for (int count=0; count < numberOfIterations; ++count) { // statement }

3. The error occures when the loop iterates one too many or one too few times.


#11
What can we do with the while statement?

A while statement can loop indefinitely until a condition is met.

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

The while statement works with boolean variables, so as long as the condition equates to true, the loop will continue to run (opposite for false, the loop will stop).

What is an infinite loop?

It is a while loop that has its condition always equating to true.

while (true) {
myBankBalance++;
}

What is an iteration?

an iteration is one pass through an entire loop.

When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?

This is perffect for when the programmer wants a numerical value to limit/determine the amount of loops that should run.

How would you write a for-loop in code?

for (int i = 1; i <= 100; i++) {
cout << i << endl;
}
cout << “we counted to 100!” << endl;

What is an off-by-one error?

An off-by-one error occurs when a loop falls short by 1 iteration or performs one too many iterations. Using my code from above to demonstrate, see if you can spot the error:

for (int i = 1; i < 100; i++) {
cout << i << endl;
}
cout << “we counted to 100!” << endl;
cout << “hang on, that was 99! whoops…” << endl;


#12
  1. create a loop.

  2. everything except zero/false.

  3. a loop that repeats an infinite amount of times.

  4. an iteration is one execution of a loop.

  5. a for loop is useful if we know the exact amount of repetitions for that loop.

  6. for(int index=start; index<end; index+=step) { do something; }

  7. it means that you have either iterated one step too often or one step too few.


#13
  1. What can we do with the while statement?
  2. A while loop is executed and will go back to the beginning of the code block and repeat the process until certain conditions are met.

    while (expression to evaluate) statement;
    

    or

    while (expression to evaluate) {statements}
    
  3. What value needs the statement in the brackets evaluate to in order for the loop to continue?
  4. A while loop is executed while the expression evaluated inside the parenthesis returns a true Boolean.

  5. What is an infinite loop?
  6. A loop that has a condition that always returns 1 or true will continue running indefinitly. Most of the times this overwhelms the computer running the code and the program crashes. But if controlled properly, infinite loops can provide us a tool at our disposal.

    while(1) {
              // Code block to be executed forever 
             // unless exited through a return statement, 
            // a break statement, an exit statement, a go to statement, 
           // an exception thrown, or the user killing the program 
    }
    
  7. What is an iteration?
  8. An iteration is the the number of loop executions performed. For example a loop that repeats 5 times before exiting has performed 5 iterations.

  1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
  2. A for loop is equivalent to a while loop that has an init-statement, codition-expression to evaluate, and an end-expression to perform.

    For example the following while loop and for loop in the next question are equivalent.

    init-statement;
       while (condition-expression)
       {
           statement;
           end-expression;
       }
    
  3. How would you write a for-loop in code?
  4. for (init-statement; condition-expression; end-expression)
      statement
    
  5. What is an off-by-one error?
  6. These are a type of errors when the loop iterates one too many or one too few times, usually due to using the wrong conditional-expression (> or >=)


#14

Completely Clueless Guyz:

#include <iostream>

using namespace std;

int main()
{
    int outer = 5;
    while (outer >= 1)
    {

      int inner = 1;
      while (inner <= 5){
        int num = outer - inner;
         cout << num;

        inner = inner + 1;
      }

        cout << "/n"
        --outer;

    }

    return 0;
}

This was supposed to be my answer to:

Invert the nested loops example so it prints the following:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

But am completely clueless cuz it won’t stop flagging my " --outer;" portion of my script. Idk why. The code isn’t even done yet. Honestly it shouldn’t work but it won’t even compile. Any ideas on why this is?


#15

1- we can repeat a statement in the braces of a while loop as long as the expression in the parentheses would evaluate to true.
2- As long as the expression in the parenthesis evaluates to true,the loop will continue.
3- If the expression always evaluates to true, the while loop will execute forever. This is called an infinite loop.
4- Each time a loop executes, it is called an iteration.

Quiz)
1- If we put the inner variable outside the outer while loop,the result would be like this :
1
2
3
4
5
so, we put it inside the outer loop and define it right above the inner loop.Every time the outer loop iterates the inner variable should be reset to 1 in order to get the true result.

2-
int x=97;
while(x<123)
{
cout<<char(x)<< " “<<x <<endl;
++x;
}
3-
int outer=1;
int count=0;
while(outer<=5)
{
int inner=5-count;
while(inner>=outer-count)
{
cout<<inner–<<” ";

    }
    cout<<"\n";
    ++outer;
    ++count;
}

4-
#include

using namespace std;

int main()
{
int outer=1;
while(outer<=5)
{
int inner=5;
while(inner>outer)
{
cout<<" “<<” “;
–inner;
}
int mid=outer;
while(mid>0)
{
cout<<mid–<<” ";
}

     cout<<"\n";
    ++outer;
}

return 0;

}

Part2 :
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- A for statement is evaluated in 3 parts:

  1. The init-statement is evaluated. Typically, the init-statement consists of variable definitions and initialization. This statement is only evaluated once, when the loop is first executed.

  2. The condition-expression is evaluated. If this evaluates to false, the loop terminates immediately. If this evaluates to true, the statement is executed.

3)After the statement is executed, the end-expression is evaluated. Typically, this expression is used to increment or decrement the variables declared in the init-statement. After the end-expression has been evaluated, the loop returns to step 2. For example:
for(count=0;count<10;++count)
{
The statement;
}

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

Quiz)
1-
for(int iii=0;iii<=20;++iii)
{
if(iii%2==0)
cout<<iii<<" ";
}

2-
int sumTo(int s)
{
int sum=0;
for(int iii=1;iii<=s;++iii)
{
sum+=iii;
}
return sum;
}

3-
// Print all numbers from 9 to 0
for (unsigned int count=9; count >= 0; --count)
std::cout << count << " ";
I think this loop will iterate forever,because the count variable is defined as unsigned, so it never
be a negative integer and the count>=0 always will evaluate to TRUE value.


#16

put ; after cout<< “/n” and it will compile.


#17

Part 1

  1. Repeat Actions
  2. All C++ loops include a statement that must be evaluated to true for the loop to continue.
  3. A loop that goes on until the program is quit or process is killed. If this never happens then it shall loop forever until it inevitably gets destroyed.
  4. One Loop cycle

Part 2

  1. For loops make it easier to set a much number of cycles

  2. for (int count = 9; count >= 0; count -= 2)
    std::cout << count << " ";

  3. When a For Loop runs one cycle too long or too short


#18

1- looping a statement
2- the statement must be true
3- happens when the statement in the brackets is always true ==> the loop never breaks
4- Each time a loop is executed

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


#19

While Statements
1. What can we do with the while statement?
We can make a block of code repeat itself continuously until a certain condition is met. This means we can also create infinite loops or ones that rely on user input.

2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
The loop will continue provided the statement within the brackets evaluates to true.

3. What is an infinite loop?
An infinite loop is one where the statement within the brackets always evaluates to true.

4. What is an iteration?
Iteration is a single execution of the block of code in a loop.

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?
For loops are used when you already know how many times you want the code to be repeated.

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

for (int counter = 0; counter < 10; counter++)

3. What is an off-by-one error?
This occurs when the loop iterates one too many or one too few times. Usually because < or > was used instead of <= or >=, or vice-versa.


#20

WHILE STATEMENTS

1. What can we do with the while statement?
Once a while statement has finished executing, control returns to the top of the whole statement. This allows us to repeat code until a certain condition is met which breaks us out of the while 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?
If the expression of a while loop always evaluates to true then it will never stop executing, resulting in an infinite loop.

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

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

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

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 usually happens as a result of of the wrong operator being used. For example > instead of >=