programming basics lec 3-5

Upload: venkata-vikas

Post on 03-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

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

    1/2

    So we've seen that JavaScript has words like alert and prompt that arecausing it to do things to pop updialog boxes. But also, it's beginning to be obvious that there are symbols like the equals sign that areabsolutely as meaningful as words are in a programming language, because this also causes things tohappen. It can take the literal value 500, and cause it to be put in the variable called Balance and thisequals sign, this assignment operator, is one of many operators in programming languages that willcause things to happen.Let's explore a few more of these operators by starting off with a few that youalready know how touse. So the most obvious ones are the arithmetic operators. We have operators for addition, subtraction,multiplication, and division which we're using the asterisk for multiplication and the forward slash fordivision. We're usually using this in combination with the equals sign as well. But let's say we create acouple of variables, variable a = 100, variable b, set it equal to 50, and then we can use the plus sign onthe right-hand side of the assignment operator to perform this operation, to add these two together, andsave the result in a new variable called result.

    in this case this would be 150. Or we can change that plus sign for a minussign and do subtraction. And Ithink you know where I am going with the asterisk for multiplication, or the forward slash for division. Butwhat we're doing is evaluating what's on the right-hand side of the equals sign and assigning that value towhat's on the left-hand side of the equals sign. Now you can use multiple operators together, butJavaScript like any programminglanguage does have operator precedence.simply meaning, some of these operator symbols are treated as moreimportant than others. So let's saywith this simple statement, if you just read this left to right, you're going to see what's on the right -handside of the equals sign, and you are going to say 5+5 is 10, *10 is 100. But no, the multiplication isregarded as more important. So the 5*10 is done first which is 50, and then we add 5 to it, and the resultwould be 55.

    If I want to make sure that I can impose an order on this and I have multiple operators, I simply take theimportant pieces and I surround them with parentheses. So in this one for example, I can make sure that5+5 will be evaluated by itself as 10, and then multiply it by 10 and we have 100. Now, one thing that'svery common is to see the same variable name on both sides of the equals sign. But remember, whatwe're looking at is to evaluate whatever is on the right-hand side, we take that as an expression.

    So the first thing we do here is we look at whatever the current value of scoreis and then we'll add 10 to it,and then we'll store the result in the variable score. So if its score is 100, it will now be 110. This idea toadd a value to an existing variable rather than creating a new variable is so common that there is ashorthand for it which is +=. score += 10 just simply means take whatever the value of the scorevariable is and add 10 to it. Now, here the + and the = have to be written as one.There is no space in between them. This is considered one operator, and the same way there is a +=, wealso have -=, *=, and a /= for subtract a value or multiply a value or divide a value. Not only that, but it'svery common to see something like this. If we want to just add 1 to an existing variable, well we couldwrite it out such as a = a + 1 or we have that shorthand a += 1. But the idea of an increment of just adding1 to a variable is so common, it actually has its own shorthand which is ++.a++ simply means add 1 to the variable a. This idea is where the languageC++ got its name from. It's theidea of C+1, the next version of C. And the same idea, we can subtract 1 from a variable by writing it long

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

    2/2