Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Help with C++


  • Please log in to reply
3 replies to this topic

#1 scorpia13

scorpia13

  • Members
  • 41 posts
  • OFFLINE
  •  
  • Gender:Female
  • Local time:05:01 PM

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.

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:10:01 PM

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.

#3 CrimsonSpider

CrimsonSpider

  • Members
  • 91 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Matrix
  • Local time:04:01 AM

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 DaviePCRepair

DaviePCRepair

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:11:01 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users