-
July 16th, 2017, 04:27 PM
#1
Question regarding While loops
Hello all,
I have a question regarding the following piece of code:
Code:
int i=0;
while (i <= 20)
{
cout<< i << " ";
i=i+5
}
cout<< endl;
this is an example from my textbook, they say when the value of i=5 the output is 5, to me it is a little confusing, because 5+5=10 which they say is the new value for i. But that is not what is outputted? what does this new value of i even do then?
Would you say things like take that new value of i (which is 10) and do something with it?
Which part of this code is outputting 5?
Also they say if you omit the statement: i=i+5; from the body of the loop, you will have an infinite loop, continually printing rows of zeros. why is this the case?
Thanks
William
Last edited by cookiemnstr510; July 16th, 2017 at 04:34 PM.
-
July 16th, 2017, 07:04 PM
#2
Re: Question regarding While loops
Use your debugger and step through the code. As written, the code is in an infinite loop.
-
July 17th, 2017, 03:13 AM
#3
Re: Question regarding While loops
Code:
int i=0;
while (i <= 20)
{
cout<< i << " ";
i = i+5
}
cout<< endl;
Examining this code, first i is of type integer and is initialised to 0. Then there is the while loop. With a while loop, the condition within the round brackets is examined and if the condition is true then the code within the curly brackets is executed and the loop loops back to the beginning of the while loop again and evaluates the condition. This loop continues until the condition is evaluated as false when the code following the closing curly bracket is executed.
Within the curly brackets in this code, first there is a cout statement. This displays the current value of i then a space. So the first time through the loop the value of i is 0 and so 0 is displayed. Then we have the assignment statement. This might look a little weird compared to maths algebra you might have learnt, but its fairly simple. First the right hand side (rhs) of the = is evaluated using the current values of the variables. So first time round the loop i is 0 so the rhs is 5 as 5 is added to i. The result of the rhs is then assigned to the left hand side (lhs) which is i. So the new value of i is 5.
We then enter the while condition again. i is less than 20 so the body of the while loop is executed again. i is now 5 so 5 is displayed. the rhs of the assignment evaluates to 10 so i is set to 10 and the whole loop starts again until i is greater than 20 when the loop terminates and the statement after the closing curly bracket is executed - the cout << end; to display a new line.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|