+ 2
C++ - Please describe what this program do [solved]
Please describe what this program do I don't familiar with c++ int x=3; while(x++<10){ x+=2; } cout << x;
17 ответов
+ 1
Yes I've just double checked and I am correct.
You can increment prefix or postfix:
++x prefix changes it before continuing with the line.
Your using x++ postfix which changes it after line.
Prefix:
x=3
While (3+1)4 < 10:
Postfix:
x=3
While 3(+1) < 10:(x now is 4)
I hope this helps to understand 🙂
+ 2
In the first line the variable x is declared. The int keyword before it means it is type integer.
In the second line a while loop is created. This loop will run until its condition becomes false. So the loop will run until x++<10 is false.
x++ is the same as x=x+1
Inside the while loop there is the code that will be executed until the condition is false.
x+=2 is equal to x=x+2
Each time the while loop runs x increases by one and then it increases by 2.
The last line is used to print the variable x to the console. It is the same as a console.log(x) in Javascript or print(x) in Python.
Run this code to see the output.
https://code.sololearn.com/c672JLBagxUS/?ref=app
+ 2
1st iteration - x = 3 < 10 (true) and now x = 3+1 +2 = 6.
2nd iteration - x = 6 < 10 (true) and now x = 6+1 +2 =9.
3rd iteration - x = 9 < 10 (true) and now x = 9+1 +2 = 12.
4th iteration - x = 12 < 10 (false) and now x won't increase (i.e. x+=2) but x++ will increase it by 1.
Therfore x is now 13.
Ans = 13.
+ 1
Olivia so you say that for condition statement it uses of original value of x but after that x is increased by 1?
+ 1
Olivia ok I see it
Can you please upvote the question so that other experts can consider it to answer 🙂
+ 1
Olivia thank you 🍻
+ 1
If your run my code you will see the answer is 13.
First the condition on the loop is checked. Then x is increased by one. Then the code inside the loop runs and x is increased by 2. This will happen again and again until the condition on the while loop is false. Once it is false the x variable is printed to the console.
https://code.sololearn.com/c672JLBagxUS/?ref=app
+ 1
Olivia the output is 13 not 12. After the condition is checked even though it will be false (when x=12), x will still be increased by 1. Only after that the x variable will be printed.
+ 1
For the last time when x=12 the condition of while loop is not satisfied (x++<10) there for it exists the loop after increment x last time (x+1)
So that 12 become 13 at the end
0
Its a loop that will continue as long as x+1 is less than 10. Each loop - x permanently increases by 2,
it then prints the final value of x after ending the while loop.
0
If you move cout<<x; into the while loop:
Before x+=2 : it will print the initial value of x and then continue to print the value of x increased by 2 for each loop. Because its before, it will not print what would be the final value of x because the code that follows will increase it by 2 after its output.
After x+=2 : it will print the value of x every loop after its been increased by 2.
because you output outside of the loop it can only give you the final value, think of it as that line has no knowledge of the workings within the loop.
0
Okay gotcha, forgot to say the line x++ in the while loop is actively changing the value of x at its root it is adding 1.
So you first loop is something like:
X=3
While 3(+1) < 10: (x now equals 4)
X = 6
I imagine the reason it's allowing 10 is because x++ is changing it at it's root but maybe the new value doesn't count until the line has run.
0
It's complicated and I'm a little rusty but I think that's how it's working here which would explain why its ending on 12
0
Thank u. Still I can't understand, how the loop taking values.
0
What is the result for this. Please give me answer 😣
0
Yeah sorry I'm tired haha