Oh awesome, I completely understand what you mean by placing it outside the loop body now - didn’t think of doing it like that. Thank you for the clarification.
Much appreciate your time, effort and energy. Cheers : )
Part I
1. What can we do with the while statement?
If the expression evaluates to true (non-zero), the statement executes. This is similar to an if statement . 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
3. What is an infinite loop?
This is where the expression always evaluates to true, so the while loop executes forever.
4. What is an iteration?
Each time a loop executes, it is called an iteration .
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?
For loops are tidier and therefore better when we know exactly how many loop iterations we want.
2. How would you write a for-loop in code?
int main()
{
for (int count{0}; count <=10; ++count)
cout << count <<endl;
return 0;
}
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 often happens because > is used instead of >=. These errors can be hard to track down because the compiler will not complain about them. The program will run but still produce the wrong result.
Interesting, I never assigned a value to non array like this before, but it actually compiles very good!
- What can we do with the while statement?
The while statement is similar to an ‘if’ statement and it executes while the evaluated expression is true. The process is repeated while the statement is true.
- What value needs the statement in the brackets evaluate to in order for the loop to continue? The statement needs to evaluate to true in order for the loop to execute.
- What is an infinite loop? This occurs when the statement never evaluate to false. As a result, the loop runs forever.
- What is an iteration? Each time a loop is executes is called an iteration.
Think about the following questions while reading:
- 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 better to use when we know how many times we need to iterate through the loop. When the number of iterations are known it’s easier to initialize and set the values of the loop variables.
- How would you write a for-loop in code?
for(int i{0};i<size(tools); i++)
{
cout<<tools[i]<<endl;
}
- What is an off-by-one error? This error occurs when the loop iterates one too many or one too few times.
This is correct, except size(tools)
is not the right way to get the size, except if you write a function by that name
-
What can we do with the while statement?
While statement allow you to execute code repeatedly based on given expression -
What value needs the statement in the brackets evaluate to in order for the loop to continue ?
The statement needs to evaluate to a true statement in order for the loop to execute repeatedly, until the statement evaluates to false. -
What is an infinite loop?
A while loop that never evaluates to false, causing the program to execute infinitely. -
What is an iteration?
The amount of times 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?
It would be a good idea to use a for loop statement because it’s easy to define, initialize, and chage the value of loop variables after each iteration. For loop statements are compact compared to while loop statemenst. -
How would you write a for-loop in code?
for ( initialize-statement; condition-expression; end-expression )
statement
//Example;
for (int count { 0 }; count < 10; ++count )
std : : << count << ’ '; -
What is an off-by-one error?
Off-by-one errros are when the loop iterates(executes) one too many times. This happens when the wrong relational operator is used in the conditional expression.
Hello Alko, What do you consider low level language? I’m asking because I’m new to programming, I basically have minimal to zero experience. I have experimented with Python before and found it easier than C++. By the way, I’m half through the Basic C++ course.
Well the godfather of all modern procedural languages is C which was actually considered a high level language in the past, but since the development of (even) higher level languages its today considered low level.
If you want you can check it out. Some compilers offer you to see the compiled assembly code if you really want to go into details how programing languages work
Thank you for your reply and recommendation. I see you have plenty of experience, and don’t get thrown off if I get you on a barrage of question, just trying pick you brain. I’ve taken the initiative to review written source code and try to comprehend the outcome. I’m starting to understand a bit of the structure in the code.
We can create loops.
It has to evaluate to true.
An infinite loop happens when the statement in the parentheses always evaluates to true.
An iteration is a loop cycle.
We use for loops when we know how many times we need to iterate through the loop.
We use while loops when we iterate through the loop an unknown amount of times.
for (int i {1}; i < 10; ++i)
std::cout << i << " ";
Off by one error happens when we write the conditional statement with using < or > instead of <= or >= .
In these cases, the outcome of our loop will be off by one iteration.
We must make sure we have used the right conditional operator by checking our code using inputs we know the correct output to.
1. What can we do with the while statement?
With a while statement we can set-up a loop that will keep repeating as long as a bool statement is true. Could be forever.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
It needs to be true.
3. What is an infinite loop?
It’s a loop where the test statement is always true and the function does not have a return, break, or goto instruction to end the loop; so the loop never ends.
4. What is an iteration?
An iteration is one complete run through a loop’s body before it repeats.
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 “for” loop is more appropriate when we want more control on the number of iterations we want the loop to go through.
2. How would you write a for-loop in code?
for(int i = 0; i < 10; i += 3) { etc. }
3. What is an off-by-one error?
It is when a loop goes through one too many or too few iterations than we wanted.
That is true, but a “true” value in C++ can be any non zero value, not just a boolean
- We can execute code in loop while condition is true
- “true”
- An infinite loop is a statement that will be always true, there never terminate
- One loop execution is equal to 1 iteration
-
When you know the increment and the number of iteractions
-
for (init-statement; condition-expression; end-expression){
statement
} -
It’s when you iterate one too many or one too few times. It can happens because a mistake in your conditional expression
-
What can we do with the while statement?
We can create loops that will continue until a certain condition in expression is declared true. -
What value needs the statement in the brackets evaluate to in order for the loop to continue?
The expresson needs to be true, 1, in order for the statement to be executed and the loop to continue.
while(expression)
{
statement;
} -
What is an infinite loop?
An infinite loop occurs when the condition in the expresson can never evaluate as false.
-
What is an iteration?
Every time a statement is executed we call that 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?
If we know exactly how many times we want to iterate through a loop… -
How would you write a for-loop in code?
for( i=0; i<10; i++)
{
cout<< i;
} -
What is an off-by-one error?
It is a common error that can be caused by using the wrong operator.
-
What can we do with the while statement? A statement declared using the while key word evaluates and executes accordingly. If true, the statement is executed. Then the statement returns to the top and repeats. If false, the statement does not execute.
-
What value needs the statement in the brackets to evaluate in order for the loop to continue? A loop will continue as long as the statement is evaluated as {True}.
-
What is an infinite loop? A continuously repeating loop that does not reach a false conclusion and therefore does not end.
-
What is an iteration? Each loop execution 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 (most utilized looping statement in C++) is best when you know the number of times you need to iterate.
-
How would you write a for-loop in code?
i
nt main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
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
1. What can we do with the while statement?
While statement initiates the “while loop” and it check whether a given condition is true or false, if it is true it will run the piece of code that is in its block, if it is false it will exit the loop.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
It needs to evaluate to the boolean true (1).
3. What is an infinite loop?
A loop that never evaluates to false, therefore will keep on iterating infinitely.
4. What is an iteration?
Each time a loop is executed is an iteration.
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?
It serves its purpose best when we know exactly how many times we need to iterate, because it lets us easily define, initialise, and change the values of loop variables after each iteration.
2. How would you write a for-loop in code?
for(inititiate counter variable; condition checker; variable incrementer){
// Code to be executed each iteration
}
Example
for(int count = 1; count < 10; count++){
cout << count
}
3. What is an off-by-one error?
A off-by-one error is a very common mistake in new programmers since their loops usually iterate one time more than it should one less than it should. This problem is usually because of mistyping the condition checker operator, suck as <
or <=
.
True in C++ is actually considered as any non zero value
Uuh nice one, thanks a lot. I thought it was just one.
- For example, the code inside while loop can be executed the amount of times while the condition applies
- While the condition is true
- Loop that has not been given a chance to exit and continues until you terminate the program
- Each time a loop executes
- We know how many times the loop is executed
- for(int counter = 0; counter<=5, counter++{
cout << "Hello! << endl;
} - Iterate the loop one times too much or too less