You can post a question on the Ethereum topic and maybe a student will help you, we are here to help with the topics related to the academy courses
Reading Assignment: Loops in C++
1. What can we do with the while statement?
We can evaluate a value, and if it’s true execute a statement
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
Count value have to be less the wile statement (count < 10)
3. What is an infinite loop?
A never ending program, a pice of code that will loop infinite times.
4. What is an iteration?
Each time the loop repeats the same patterns are called iteration.
Part2
1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
A Wile loop is used to repeat a block of code an unknown number of times, until a condition is met. A do loop is to use in a more controlled loop and execute at least on time and where the condition to stop the can be in the actual code block.
Difference between While loop and Do while loop is that the while loop can end without executing any statement and Do while loop will end only after executing one statement.
2. How would you write a for-loop in code?
for (int count{ 9 }; count >= 0; --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. Typiclity when wrong relational operator is used in the conditional-expression (eg. > instead of >=). T
That would be an if
statement, while
loop iterates a block of code until a condition is meet.
The question was about a for
loop, not do while
- What can we do with the while statement?
The while statement is used to evaluate expressions
- What value needs the statement in the brackets evaluate to in order for the loop to continue?
To true (non zero)
- What is an infinite loop?
When a while statement always evaluates to true thus the while loop executes forever
- What is an iteration?
Each time 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 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?
When the loop iterates one too many or one too few times
-
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 a loop when we know how many times we need to iterate -
How would you write a for-loop in code?
for(int i=0; i<10; ++i) {
cout << i << endl;
. ***
What is an off-by-one error?
It’s when you iterate one too many or one too few times in your conditional expression
What can we do with the while statement?**
You can use it for a while to loop.
-
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. -
What is an infinite loop?
This is when the loop never break, because you expression/value is always true as anexample. -
What is an iteration?
An Iteration is each loop
-
What can we do with the while statement?
– The while statement allows us to loop while a certain condition is true -
What value needs the statement in the brackets evaluate to in order for the loop to continue?
– The statement in the brackets needs to evaluate to true (non-zero) for the loop to continue. -
What is an infinite loop?
– An infinite loop is a loop that executes for ever because the test condition is always true. -
What is an iteration?
– Each execution of a loop 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?
– It’s a good idea to use a for loop 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.
-
While statements allow us the repeated execution of a block of code as long as the condition inside the while () is met
-
It needs to evaluate to true
-
It’s a loop whose associated execution code will run infinite times, causing the program to crash.
-
We call iteration every single repeated execution of the same block of code of a loop cycle.
-
for statements are best suited for situations where we know how many iterations we need (a finite number of iterations)
-
for (index initialization; index condition; pace) --> example: for (int counter = 0; counter < 10; counter++)
-
it’s a common mistake made when writing a for loop, where inadvertedly the index condition is set to < (or <=) causing the loop block to iterate one more (or less) time than expected.
Not necessarily, you can for example have a program that will run indefinitely in the background and periodically check some state an infinite loop is perfect for that.
1. What can we do with the while statement?
You can use a while to loop. It reads undefined size list, complete line or string using EOL, EOF of object.lenght(), object.sizef()
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 your expression/value is always true for example. Some hard coding true the while condition and using IF statement in the body to break the loop is an comune error.
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. Initiate large array with the same value with a for loop is a great practice.
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 conditional expression. The issue appears when you use sizeof() as limit. Never forget the last index will be sizeof() minus 1.
1.) With the while statement, you can create a while loop.
2.) true.
3.) An infinite loop is a loop that continually iterates because the return value is always true.
4.) An iteration happens when a loop completes and restarts.
5.) A for loop is best used when you know exactly how many times you need a loop to iterate, because they are concise.
6.) for(int cout{0}; cout< 20; ++count) std:: cout<<cout<<‘’;
7.) An off by one error often occurs when there was an incorrect operator in the expression causing the loop to return a value one too few or too many to the desired return.
While Statements:
1. What can we do with the while statement?
It gives the ability to run loops until specified conditions are met.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
It needs to be true to continue looping.
3. What is an infinite loop?
If value of the condition given is always true, the loop will endlessly continue.
4. What is an iteration?
An iteration is a complete cycle of one 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?
If you know what values you’re evaluating and the amount of iterations needed, a do statement would be preferred versus a while statement which is conditional based on the input given.
2. How would you write a for-loop in code?
int abc{};
cout << "Count from 1 to whatever # U put here: ";
cin >> abc;
for (int xyz{1}; xyz <= abc; ++xyz)
cout << xyz << ’ ';
3. What is an off-by-one error?
It’s when values are evaluated incorrectly up one or down one by using the wrong operator. (e.g: (x < 10) would be everything below 10, not including 10. If you need 10 included, you would replace < with <=).
Article 1
1. What can we do with the while statement?
Create loops that will repeat a piece of code until the end conditions are 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?
an endless loop that will prevent the program ever ending. caused by the value in the brackets always evaluating to true
4. What is an iteration?
one loop of the while loop is an iteration
Article 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 you know how many iterations are needed a for loop is best because you can easily define and change the variables after each iteration
2. How would you write a for-loop in code?
for (int count{ 0 }; count < 10; ++count)
std::cout << count << ’ '<<endl;
3. What is an off-by-one error?
When a loop iterates one too many or one too few times. quite hard to detect because the program will run fine, but the results will not be right
-
What can we do with the while statement?
You can write out loops. -
What value needs the statement in the brackets evaluate to in order for the loop to continue?
It needs to be true to continue. -
What is an infinite loop?
An expression always coming out true and will be in an indefinite loop. -
What is an iteration?
Each time a loop is executed 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 you know how many loops you plan to iterate
-
How would you write a for-loop in code?
for (int count{ 0 }; count < 10; ++count)
std::cout << count << ’ '; -
What is an off-by-one error?
When the program has too many iterations or too few for the loop.
1. What can we do with the while statement?
- We can execute a block of code as long as a condition holds 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 always has a True condition, thus it never terminates.
4. What is an iteration? - An iteration is one pass through 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?
- Generally, use for loops when you have a defined range of values to iterate over. Use while loops when you aren’t iterating, or don’t know when your exit condition will be true.
2. How would you write a for-loop in code? -
for (int count{ 0 }; count < exponent; ++count) { std::cout<<"total_pre: "<<total<<" "; total *= base; std::cout<<"total_post: "<<total<<" "<<"base: "<<base<<'\n'; }
3. What is an off-by-one error?
- This is a common noob error where the loop either executes one too many or one too few times.
- We can create loops.
- True.
- An infinite loop is a loop without an increment, it will execute over and over because the expression will always evaluate to true.
- Each time the loop executes.
… - When you know how many times you want to iterate through.
int main() { for( int x=0; x<10;x++) { cout<<"The number is: "<<x<<endl; } }
- Off-by-one errors occur when the loop iterates one too many or one too few times.
- Creating a loop.
- for(init-statement; condition-expression; end=expression).
-
or >=
- 1 loop cycle.
- To know how many iteration are required.
6.for(int counter = 0; counter < 100; counter++)
7.Loop execute too many or too little
Not sure how you got these answers for the 2. and 3. question, can you try again?
1. What can we do with the while statement?
We can do loops
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 where the while loop expression always evaluates true
4. What is an iteration?
Every time the loop executes 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?
When we know how many times we need to iterate
2. How would you write a for-loop in code?
for (int count{ 0 }; count < 10; ++count)
std::cout << count << ’ ';
3. What is an off-by-one error?
It is when it loops one to many times or one too few times. This happens due to the the expression code not being correctly written
-
What can we do with the while statement? We could do a simple loop, similar to if else, but 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? Need to evaluate a true condition expression to keep running.
-
What is an infinite loop? A serious problem if you do not count on it. Never ending Loop.
-
What is an iteration? Each time a loop executes is 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? The 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.
-
How would you write a for-loop in code? for (init-statement; condition-expression; end-expression)
statement -
What is an off-by-one error? Is 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.
-
using While we can continue repeating a code for as many time we need to, or till the condition we set is met.
-
If the expression is true(non-zero), the execution will continue.
-
When all the time the expression is true, the loop will continue to execute and this is called infinite loop.
-
each time a loop executes, its called iteration.
part 2:
-
When we exactly know how many times we need a loop to execute, if loops will allow us to easily change the loop variables after each execution.
-
for (init-statement, condition-expression, end-expression)
statement -
off-by-one error occurs when the loop iterates one too many or one too few times.