programming basics lec 3-3

Upload: venkata-vikas

Post on 03-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Programming Basics Lec 3-3

    1/2

    Working with numbers

    So anytime we're working with variables, we really have two things. We have the variable, the container,the bucket, and we have the different values that we're going to put in it from moment to moment. Now,the most common kind of values in computer programs are numeric values, numbers, and really this is nosurprise. Sure, these days, when we use the word computer, we think of a machine. But for hundreds ofyears, computer was a job. In the 17th, 18th, 19th centuries, you could be employed as a computer.It just meant a person who computes, someone who does calculations. Then these machines come alongand they do it far better than the person ever could. These days we think a computer meanshardware. But the reason they were invented was always about number crunching. But we need to becomfortable with our use of numbers. When we're writing programs, we're dealing with bank balances anddollars and cents, the position of a spaceship on the screen, the height and width of a web browserwindow, the coordinates of a device using geolocation, the amount of milliseconds we've been playing anMP3 song, even colors are represented with numeric ranges of red, green, and blue. All numbers.

    Now, unlike many programming languages you'll come across, where you need to be very conscious ofwhat type of number you have-- integer, floating-point, positive, negative, is it a big number, is it a smallnumber-- JavaScript instead has a very human approach to numbers. They're all just considerednumbers. So let's see how to work with them. So I have an empty file here set up to write someJavaScript in. Let's start off this way, var a. Now, this statement tells JavaScript create a variable and callit a. When this line of code is executed, the value of this variable is undefined.So on the next statement, I will set that variable to the number 5. Now, when you're brand-new toprogramming, lines like a = 5 and b = 10 can look a little weird, particularly if the last time you sawanything like this was equations in school. But remember, when you see the equals sign here, this is nota politedescription. This is a command. This is an instruction. This is saying take the value 5 and put it inthe variable called a.I don't care what a was before, but after this line of code is executed, it will have the value 5. This is whatthe equals sign does. It assigns a value. That's why it's formally called the assignment operator. Now,notice that there are no quotes around the number 5. The number 5 here is referred to as a numericliteral. It represents the fixed value 5. The variable part of this line can change as our program runs, butthe literal does not.

    5 always means 5. What if we want to change this? What if we want a million? Well, I'll just say a =1000000. We don't use commas in it. We just use the number itself. While in other programminglanguages we have to be very conscious if values are integers, meaning whole numbers, JavaScriptdoesn't care. If we want something after the decimal point, we just type that in. Numbers are regarded aspositive by default, but they can be negative as well.If I want to make a negative number, I just put a minus sign in front of it. In this case -500. So we havefive statements so far. I'm going to add a sixth. I'm going to use alert again, and in parentheses, just put a.Now, notice that I'm not using quotes here. I don't want to write out the letter a as an alert dialog box. Iwant to write out the value of the variable a, which is what this is going to do. So I'm going to save thisand I already have a folder set up where I can just run this JavaScript by opening the HTML page.

  • 8/11/2019 Programming Basics Lec 3-3

    2/2

    So I open that up and we get one dialog box with -500 in it. That's what we should expect. What'shappening is we're opening up, our program runs, our code runs, we create the variable, we change it to5, we change it to 1000000, we change it to 123.654, we change it to -500, and then we write itout.Simple instructions created and executed one after the other. But this is how we can start to work withnumber variables in JavaScript.

    Now, as one final note, understand there's a very big difference betweencreating a variable like this, var b= 123, and creating a variable like this, var c =. Similar looking value, but inside quotes. Well, the first onehere, var b, is a number 123. The second one, var c, is a string. Not the number 123, but the digits 1, 2, 3and sometimes that's a pretty big difference in behavior.So let's talk about strings next.