BleepingComputer.com: Help with C++

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help with C++

#1 User is offline   scorpia13 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 41
  • Joined: 07-December 10
  • Gender:Female

Posted 13 December 2010 - 10:56 PM

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.
The core of mans' spirit comes from new experiences.

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

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 User is offline   CrimsonSpider 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 87
  • Joined: 28-October 10
  • Gender:Male
  • Location:The Matrix

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
"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)

#4 User is offline   DaviePCRepair 

  • New Member
  • Pip
  • Find Topics
  • Group: Members
  • Posts: 7
  • Joined: 25-July 10

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
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users