Loops in C++ - Reading Assignment

[quote=“ivan, post:1, topic:3168, full:true”]
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?

A while statement is declared using the while keyword. When a while statement is executed, the condition is evaluated. If the condition evaluates to true , the associated 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. This means a while statement will keep looping for as long as the condition evaluates to 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?

If the while statement continues forever, it is an infinite loop.
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.

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?

The for statement (also called a for loop) is preferred when we have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables

2. How would you write a for-loop in code?
for (init-statement; condition; 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 to produce the desired result.

1 Like

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?

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?

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

1: we can create loops with different conditions.

2: a boolean condition has to be true, otherwise the iterations of said loop will stop.

3: a loop where no ending condition is met.

4: a single run through of a loop.

5:Prefer for loops over while loops when there is an obvious loop variable. Prefer while loops over for loops when there is no obvious loop variable.

6:like: for(int x=0;x<10;++x){};

7:a mistake that happens if you declare your boolean in a wrong way(like x>10 instead of x=>10).

1 Like

While loop

  1. We can use a while statement to do looping.
  2. The while statement will keep looping for as long as the condition evaluates to true.
  3. Infinite loop is the condition where the expression always evaluates to true, the while loop will execute forever
  4. An iteration is referred to each time a loop executes.

For loop

  1. For loop is preferred when we have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.
  2. for (init-statement; condition; end-expression)
    statement
  3. Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result.
1 Like

What can we do with the while statement?

The while loop/statement is used to repeat a section of code an unknown number of times until a specific condition is met.

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

True

What is an infinite loop?

An infinite loop has an expression that always evaluates to True.

What is an iteration?

This is when a loop executes.

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 have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.

How would you write a for-loop in code?

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

 
   statement

What is an off-by-one error?

It is an error which occurs when the loop iterates one too many or one too few times to produce the desired result.

1 Like

Part1

  1. You can use loops.

  2. true.

  3. A continuous loop in which an expression is always true.

  4. An iteration is when a loop is executed.

Part2

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

  2. int i=0; i <5; i++){}

  3. When a loop is iterated an incorrect number of times.

1 Like

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?
Condition of the while loop must be true for the statement to continue
3. What is an infinite loop?
An infinite loop ocurse when the argument can’t become false.
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?
For loops are more compact and efficient and don’t have the problem of the iterator such as “count” potentially losing scope.
2. How would you write a for-loop in code?
for (initial statement; condition expression; end expression)
statement
3. What is an off-by-one error?
occur when the loop repeats one too many or one too few times.

1 Like

Part I:
1. What can we do with the while statement?

  • The while loop loops through a block of code as long as a specified condition is true :
while (condition) {
   statement;
// (code block to be executed);
}

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

  • As long as the condition evaluates to true , the associated statement executes.

3. What is an infinite loop?

  • An infinite loop occurs if the expression always evaluates to true. The while loop will never terminate and continue to execute forever.
while (true)
{
  // this loop will execute forever
}

4. What is an iteration?

  • An iteration happens each time a loop executes, incrementing or decrementing the count by a specified amount.
ie: 
--count;
++count;

Part II:
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, aka for loop, is preferred when we have an obvious loop variable or know how many times the loop should run because it lets us easily and concisely define, initialize, test, and change the value of loop variables.

  • If you want the loop to break based on a condition other than the number of times it runs, you should use a while loop .

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

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

==============================================

int main(){
    for (int count = { 1 }; count <= 10; ++count)
    cout << count << " ";

    return 0;
    // 1 2 3 4 5 6 7 8 9 10 
}

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 to produce the desired result. It’s important to be aware of using the correct relational operator.
2 Likes
  1. The while statement can be used to create loops.

  2. The value must be true for the loop to continue.

  3. An infinite loop is caused when the expression always resulting in a true result.

  4. An iteration is a complete cycle of a loop.

  5. The For loop is used when you know exacly how many times you want the loop to run. A while loop is used when you want the loop to run until a certain condition is reached.

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

  7. Off-by-one errors occur when the loop iterates one too many or one too few times to produce the desired result.

2 Likes

Part 1

Q: What can we do with the while statement?

A: You can create a while loops, the simplest out of the three loops that C++ consists of.

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

A: When the value in brackets turns out as a true statement the while loop will start to execute. The values that consist the while loop to function is declare an integer, then declare the length on the while statement and the way it will do it. For example:

int loop {1};

while (loop <=30) {

cout << loop <<” “ ;

++loop;

};

Q: What is an infinite loop?

A: If the statement in the loops turns out always true you will have an infinite loop. The program will continue running forever.

Q: What is an iteration?

A: Each time a loop gets executed it is called an iteration.

Part 2

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

A: When it is an obvious loop variable because we can define it easier.

Q: How would you write a for-loop in code?

A: for(int loop {1}; loop <5; ++loop)

Q: What is an off-by-one error?

A: When the iteration happens too less or too many times. The reason why it happens might be because you might have a mistake on the conditional.

2 Likes
  1. The while statement is used to form the while loop.

  2. loops use while to evaluate the condition. If true, the statement executes as long as the statement is true. The loop continues to output the corresponding data until the statement returns false.

  3. Is an expression that always evaluates true.

  4. Iteration represents every time a loop executes.

  5. Use for loops in almost all cases where a counter is needed, and generally in most cases where a need for a loop is obvious.

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

  7. A common error from new programmers who used the wrong relational operator.

2 Likes

PART ONE:

1: Using a while statement for a loop.

2: The While statement because they check to see if the expression is true.

3: A loop that never stops because all values put into it is true. this makes the loop endless.

4: Each different loop is considered an iteration.

PART TWO:

1: We use the For loop when we know how many times we are going to iterate the loop. The While loop is used when there is no set number of times you need to do the loop.

2: for(int m = 0; m < length; m++) {
cout << m <<endl;
}

3: It is when you have too many if or for statements or too few, and there is a mistake in the code.

2 Likes
  1. What can we do with the while statement? Run a program that will loop to give the expected results.

  2. What value needs the statemen t in the brackets evaluate to in order for the loop to continue? A statement that will return a boolean. True or False

  3. What is an infinite loop? A program that will run forever.

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

2 Likes

1 while (while statement) can be used to execute code while condition is true
2 To be “ true”
3 Unconditional true loop
4 Iterate code x… number times

  1. When we have obvious variable and know many iterations you need

  2. For (int sub(0); sub<100; ++sub;
    Std::cout<< sub<<””;

  3. The loop iterates one too many or one too few times to produce the desired result.

2 Likes
  1. What can we do with the while statement?
    To loop a section of code while a given condition is 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 that executes continuously

  4. What is an iteration?
    each loop is an iteration

  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 we know how many times we need to iterate

  6. How would you write a for-loop in code?
    for(int i=0; i<10; ++i) {
    cout << i << endl;
    }

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

2 Likes
  1. With a While statement we can create a while loop.

  2. The While statement needs the condition to evaluate to true in order for the loop to continue.

  3. An infinite loop always evaluates to true and runs forever. Intentional infinite loops are used in web applications.

  4. Each time a Loop executes is called an iteration.

  5. Prefer for loops over while loops when there is an obvious loop variable.
    Prefer while loops over for loops when there is no obvious loop variable.

  6. for (int count{0}; count <=10; ++count) for C++

  7. A One Off Error is when a programmer is off on count by 1 due to possibly using an improper operator.

1 Like

What can we do with the while statement?
Simply said we can use it to create loops

What value needs the statement in the brackets evaluate to in order for the loop to continue?
It has to be true

What is an infinite loop?
It runs 4ever, as example:

    while (true) {
        cout << "Really wild one" << endl;
    };

What is an iteration?
Each execution of the code in a loop is called an interation

When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
To avoid infinite loops, just joking. When you have need a counter or you know how often you want to iterate

How would you write a for-loop in code?

 cout << "Hello world!" << endl;
    for(int i=0; i<7; ++i) {
        cout << "Current Number: " << i << endl;
    };

What is an off-by-one error?
An error which is doing 1 iteration too much/less

1 Like
  1. What can we do with the while statement? -With the while statement you can program statements to execute 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 never terminates.
  4. What is an iteration? - Every time a loop executes.

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 a foor loop when you know how many iterations there would be. While loops are mostly used when you do not know how many times the loop executes.
2. How would you write a for-loop in code?
for(int i=0; i<10; ++i) {
cout << i << endl;
}
3. What is an off-by-one error? - is a logic error when an iterative loop iterates one time too many or too few.

1 Like

PART 1:

  1. We can loop a condition, if the condition evaluates to true, the while statement will keep loop for as long the condition evaluate to true.

  2. The statement needs to be tru in order to continue the loop.

  3. An infinite loop happens when the expression always evaluates to true.

  4. An iteration is when each time a loop is executed.

PART 2:

  1. When we have an obvious loop variable, because it lets us derive the value of loop variables.

  • Initialization- Initializes variable and it’s executed only once.

  • Condition- If ‘true’ the body of ‘for’ loop is executed. If ‘false’, the loop is terminated.

  • Update- Updates the value of initialized variables and again checks the condition.

image

  1. It is when the loop iterates one too many or one too few times, to produce the desired resuly.
1 Like
  1. What can we do with the while statement? we create a loop and we execute a code for as long as our statement is 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? loop that never stops

  4. ***What is an iteration?***is the counter on a for 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? Prefer for loops over while loops when there is an obvious loop variable.
    Prefer while loops over for loops when there is no obvious loop variable.

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

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

1 Like
  1. While statements can be used to create a recurring loop until a certain condition is met.
  2. The value within the brackets within the statement needs to evaluate to true in order for the loop to continue.
  3. A while statement will continue to loop as long as the associated condition evaluates to true.
  4. Every time a loop executes it is called an iteration.
  5. It is a good idea to use a for statement instead of a while loop when we have an obvious loop variable because it lets us easily and concisely define, initialize, test, and change the value of loop variables.
  6. This is the way to write a for-loop
    for (init-statement; condition; end-expression) statement
    First, declare and initialise the loop variable. Second, for each loop iteration evaluate the condition and if true execute the condition, if not true terminate the loop. Third, evaluate the end-expression usually by incrementing or decrementing the loop variable and return to step two of the for loop.
  7. An off-by-one error is a mistake in the for-loop code causing it to iterate too many or too few times which produces a result whose value is incorrect.
1 Like