I decided to teach myself C++. I've been reading these tutorials :
http://www.cplusplus.com/doc/tutorial/variables/
But I don't understand this bit in the program :
"a = 5;
b = 2;
a = a + 1;
result = a - b;"
The answer is 4. I perfectly understood how you get 4, but I can't understand why it's done that way. Its very confusing writing the program like that.
Why do you actually need the first a = 5, if it might be easier to just write it as:
a = 6;
b = 2;
result = a - b;
I know it's probably a part of the language, but I want to know why it's done that way.
Page 1 of 1
Help with C++
#1
Posted 13 December 2010 - 10:56 PM
The core of mans' spirit comes from new experiences.
#2
Posted 14 December 2010 - 08:03 AM
The application is just showing how variables can be assigned different values. In the real world, you would do it your way.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens
#3
Posted 20 December 2010 - 09:23 PM
It's important to look at the equation as a time-frame.
You first declare a as 5.
And b as 2, which you understand.
When you see;
a = a - b
Think of it as:
new a (solution) = old a + old b
So a will have a new output and b will remain the same.
Hope i helped,
CrimsonSpider
You first declare a as 5.
And b as 2, which you understand.
When you see;
a = a - b
Think of it as:
new a (solution) = old a + old b
So a will have a new output and b will remain the same.
Hope i helped,
CrimsonSpider
"Don’t worry if it doesn’t work right. If everything did, you’d be out of a job."
(Mosher’s Law of Software Engineering)
(Mosher’s Law of Software Engineering)
#4
Posted 30 December 2010 - 07:39 PM
It's basically showing you variable manipulation so you can see, at a basic level, how you can work with variables and manipulate them into other values.
Share this topic:
Page 1 of 1

Help



Back to top










