Loops in C++ - Reading Assignment

PART ONE

1.We can do easy loops until the condition is filled.
2.true
3.loop that condition is always fullfiled and never will be terminated. 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.Each time a loop executes, it is called an iteration.

PART TWO
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 (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

Part 1
1. What can we do with the while statement?
Used to create a loop in the program by evaluating an expression.

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?
The loop that never ends by itself. It keeps executing the loop code forever unless program is killed by user or a break, return or goto or exit statement is executed.

4. What is an iteration?
Each execution of loop block of code is called iteration.

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?
When we know upfront how many iterations are required.

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

3. What is an off-by-one error?
It is logical error while writting loop which will not be detected at compile time. It is usally caused by using incorrect relational operator in condition expression (> instead of >=).

  1. What can we do with the while statement? A loop

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

  3. What is an infinite loop? a never break loop

  4. What is an iteration? a loop itself iterates

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

  6. How would you write a for-loop in code?
    while (count < 10)
    {
    std::cout << count << " ";
    ++count;
    }

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

  1. We can execute code repeatedly while the while condition is true. “while (this) is true, do (this)”.

  2. The condition.

  3. An infinite loop is a block of code that will run indefinitely until manually killed. The condition is always true.

  4. An interaction is considered one round of loop execution.

  5. For loops are great for pieces of code which we know exactly how many times we want it to execute.

  6. for (init-statement; condition-expression; end-expression) statement

  7. This is when the loop executes one to little or too many times.

PART 1

1. What can we do with the while statement? A while statement allows us to run the same piece of code over and over again.

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

3. What is an infinite loop? An infinite loop occurs when the loop’s condition is always true

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

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? The for loop is ideal when we know the number of times we need to iterate.

2. How would you write a for-loop in code? A simple for-loop can be expressed as follows:

for (int count=0; count < 10; ++count) {
    std::cout << count << " ";
}

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. Repeatedly execute a block of code while the provided statement is true.

  2. true (non-zero)

  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.


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

for (init-statement; condition-expression; end-expression)
   statement
  1. Off-by-one errors occur when the loop iterates one too many or one too few times.

1. What can we do with the while statement?
We can repeat a set of statements to achieve complex tasks.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
It needs to be a true evaluation.
3. What is an infinite loop?
It is a loop whose never ends because its evaluation statement is always true.
4. What is an iteration?
It is the execution of all the statements of the block. The block is entered and exited during each iteration.
Quiz

  1. Because we have to reinitialize the control loop variable _inner_ each time for each control loop variable outer .
   #include <iostream>

   using namespace std;

   main()
   {
        chat letter('a');
        while(letter != 'z'){
	     cout << static_cast<int>(letter)  << "\n";
	     letter++;
        }
	return 0;
    }
   #include <iostream>

   using namespace std;

   main()
   {
        int outer = 5;
        while(outer >= 1){
             int inner = 5;
             while(inner >= 1)
                  if(inner <= outer)
                     cout << inner-- << " ";
                  else
                     inner --;
    
              cout << "\n";
              --outer;
        }

        return 0;
    }
  #include <iostream>

  using namespace std;

  main()
  {
         int outer = 1;
         while(outer <= 5){
              int inner = 5;
              while(inner >= 1)
                   if (inner <= outer)
                       cout << inner-- << " ";
                   else {
                       cout << "x ";
                       inner--;
                   }  
    
               cout << "\n";
               ++outer;
          }

          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?
When it is known exactly how many iterations are needed. For example when arrays are involved.
2. How would you write a for-loop in code?
for(int i = 0; i < N; i++){}
3. What is an off-by-one error?
When the loop iterates one too many or on too few times.

Quiz
1.

   for(int i = 0; i <= 20; i++)
       std::cout << i << " ";

2

   int sumTo (int x)
   {
       for(int i = 0; i < x; i++)
	   x = x + i;
       return x;
   }

3 The for is a inifite-loop

1. What can we do with the while statement?
We can loop through a block of code until a condition is met.
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 that continues on forever because the expression never evaluates to false.
4. What is an iteration?
Each passthrough of a loop is an iteration.

I. 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 great for when we know how many times a loop should be executed.
II. How would you write a for-loop in code?

   for(int i = 0; i<x; i++){
   	cout << i <<endl;
    }

III. What is an off-by-one error?
This occurs when a loop iterates one too many or one too few times.

What can we do with the while statement?
We can use a while statement to repeat commands enclosed until the condition is false.

What value needs the statement in the brackets evaluate to in order for the loop to continue?
The value needs to be true or non-zero for the case of C++

What is an infinite loop?
It is a loop that iterates endlessly and does not stop.

What is an iteration?
A single pass for the group of commands enclosed a 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 the start end and incrment are known or well defined.

How would you write a for-loop in code?

What is an off-by-one error?
When you iterate by + / - 1 times more than nescesary.

Loops in C++

    1. We can repeat some statements while some conditional expression is true.
      Lke IF....THEN LOOP.

    2. the value of the statements needs to be TRUE.

    3. The while loop repeats itself forever while the value of that expression is ALWAYS TRUE.

    4. Each time a loop executes is an iteration. (x repeat)

    1. When we know exactly how many times you want to iterate some code.

    2. for(init statement; condition exprssion; end expression) { statements;}

    3. If you are off 1 time, by one to many or two too few. mostly by forgetting equal operator in <= or >=

  • 1 Like

    1. What can we do with the while statement?
    It is used for loops.
    2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
    It needs a true value.
    3. What is an infinite loop?
    Loop that never reaches true in evaluation statement
    4. What is an iteration?
    It is a next repetition of 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?
    When we know how many times we will repeat the loop.
    2. How would you write a for-loop in code?
    for (int i = 0; i <= 5; i++)
    cout << i << " " << endl;
    3. What is an off-by-one error?
    When a loop is repeated to many or to few times.

    1 Like

    Part 1:

    1. What can we do with the while statement?

    A while statement can be used to generate a loop.

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

    The statement must evaluate to true in order for the loop to continue.

    1. What is an infinite loop?

    An infinite loop is a loop for which the conditional statement is always true.

    4.What is an iteration?

    An iteration is one run of the expression in the loop.

    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?

    When we know exactly how many iterations of the loop are necessary a for loop is a better idea than a while loop.

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

    for(int count = 0; count < N; ++count) {
    expression;
    }

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

    An off-by-one error relates to an improper number of iterations of the for loop due to a misuse of the conditional statement.

    1. While statement evaluates an expression. Once the statement has finished executing, control returns to the top of the while statement and the process is repeated.
    2. The statement needs to evaluate to true (non-zero), so the loop will continue executing.
    3. The infinitive loop executes forever because the expression always evaluates to true.
    4. An iteration is one execution of the loop. Any variables declared inside the loop body are created and then destroyed with each iteration.
    1. We use a for a statement when we know exactly how many time we need to iterate.
    2. For-loop in code:
      for (int count=0; count < 10; ++count)
      std::cout << count << " ";
    3. Off-by-one errors occur when the loop iterates one too many or one too few times, usually because the wrong relational operator is used. Try to test your loops with known inputs that cause it to iterate 0, 1, and 2 times.
    1 Like

    Part 1

    1. A while statement allows us to loop a block until a certain condition is met.
    2. The statement in the brackets needs to be true for the while loop to continue.
    3. An infinite loop occurs when the statement in the brackets always evaluates to true. The block never stops executing.
    4. An iteration is one execution of the block within the loop.

    Part 2

    1. When we know exactly how many times we need to iterate, the for loop is useful.
    2. for(init-statement; condition-expression; end-expression){//code goes here}
    3. An off-by-one error occurs when the for loop stops either one iteration too much, or too low, due to the programmer incorrectly setting the condition expression;
    1 Like

    1. What can we do with the while statement?
    Loop as long as a condition is met.

    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 that has no programmatic break- thus it runs indefinitely (or until system resources are depleted and a crash occurs).

    4. What is an iteration?
    A single pass through a block of loop code.

    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.

    2. How would you write a for-loop in code?
    int count = 0;
    while (count < 10)
    {
    std::cout << count << " ";
    ++count;
    }

    3. What is an off-by-one error?
    Being 1 iteration off on the desired number of iterations on your loop, usually caused by using the wrong conditional expression (> or >=).

    1 Like
    1. A while statement is a loop which executes when the expression inside the round brackets is true

    2. Condition of the while loop must be true for the statement to continue

    3. A loop that goes on forever

    4. Iteration is, the execution of the code inside the block or the loop

    5. A for loop is used when the caller knows the number of times the code needs to iterate

    6. For loop is divided into three parts
      a: initialization ( variable is defined )
      b:condition is evaluated
      c:updation

    7. The off-by-one error occurs when the block of codes runs one less or one more times the number of execution that was expected

    1 Like

    What can we do with the while statement?
    In While statement 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?
    true
    What is an infinite loop?
    the while loop will execute forever
    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
    How would you write a for-loop in code?
    for (int i=0; i<number_of_loops;++i){
    code}
    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

    What can we do with the while statement?
    What value needs the statement in the brackets evaluate to in order for the loop to continue?

    What is an infinite loop?
    if the expression being evaluated to determine if the loop runs always evaluates to true, the while loop will execute forever.

    What is an iteration?
    one run of the executable code of the loop

    Quiz
    1) In the above program, why is variable inner declared inside the while block instead of immediately following the declaration of outer?
    inner needs to be re-initialized each time the nested loop runs.

    2) Write a program that prints out the letters a through z along with their ASCII codes. Hint: to print characters as integers, you have to use a static_cast.

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        for (char charVariable = 'a'; charVariable <= 'z'; ++charVariable)
        {
            std::cout << charVariable  << " is ascii " << static_cast<int>(charVariable) << "\n";
        }
    
        return 0;
    }
    

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

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int outer = 1;
        while (outer <= 5)
        {
            int inner = 5;
            while (inner >= outer)
    	{
                std::cout << inner << " ";
    			inner=inner-1;
    	}
            std::cout << "\n";
            outer=outer+1;
        }
    }
    
    1. Now make the numbers print like this:
      (reverse of the above)

      #include
      #include

      using namespace std;

       int main()
       {
       //row goes down
       //column goes across
       int row = 1;
           while (row <= 5)
           {
               // loop between 5 and 1
               int column = 5;
               while (column >= 1)
       	{
                   if (column > row){
                       std::cout << "  ";
                   } else {
                       std::cout << column << " ";
                   }
                   column=column-1;
               }
      
               // start new row
               std::cout << "\n";
               row=row+1;
           }
           std::cout << "\n";
      
           return 0;
       }
    2 Likes
    1. What can we do with the while statement?
      

    Do an action as long as the while statement is true

    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?
      

    when the value passed to the while loop will always evaluate to true

    1. What is an iteration?
      

    one execution of code 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?
      

    when you know exactly how many times you need to iterate

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

    int someValue = 5
    for (int i = 0; i < someValue; i++){
    stuff to execute per iteration
    }

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

    when the you’re missing or adding an iteration because you used < or > rather than <= or >=

    1 Like

    Part 1

    1. With the while statement a piece of code can be executed repeatedly, as long as…
    2. … the condition expression in brackets is evaluated to true.
    3. An infinite loop is executing its loop body forever, again and again.
    4. Each execution of a loop body is called an iteration.

    Part 2

    1. The for statement is preferred over the while statement for looping if we know exactly how many iterations we want. It enables us to define, initialize and change the loop variable after each iteration in a simple format.
    2. for (init-statement; condition-expression; end-expression) { /* body statement */ }
    3. Off-by-one errors occur when loops iterate one too many or one too few times.
    1 Like