Udacity CS 101: Variables

Now variables are interesting objects. Its pretty similar to algebra, the way you solve for the unknown x, but in this case you decide what x is!

Variables example

Image courtesy of http://www.itechsociety.com

Take hits for example:

You are trying to find out the number of seconds in a year. So you would take 365 days multiplied by 24 hours multiplied by 60 minutes multiplied by 60 seconds. 31536000 seconds is the answer. Now imagine you have to refer to this multiple times in your calculation, without variables, you would have to constantly refer to 31536000, you might not even get the number correct, and if you are writing a program that involves a lot of numbers, you might end up not remembering what this number stands for! And numbers are really difficult to type quickly on the keyboard.

With variables, what you can do is you give a name to 31536000, lets say you call it seconds-in-a-year. Now it is much easier to remember, since it is literal, easier to type and harder to misspell. So at this point you might be even more interested and want to find out how to give a name to these expressions:

Name = Expression
seconds-in-a-year = 31536000

Or you if you remember the concept on Backus Naur form, expressions can be made up of even more expression you can have this:

seconds-in-ten-years = seconds-in-a-year * 10

That is the magic of variables.

Continue to try the Variables Quiz and if you have any questions just ask away below!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s