02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Addition

Let's study the addition of both strings and numbers. Numbers we can guess, but what happens when you add two strings? If you think that isn't enough, we'll even look at error 1084 and figure out how to avoid it.

We know how to add two integers using Flash: it's very easy and can be done using the '+' operators. To trace the addition of two numbers, there isn't much effort involved. But as we develop as programmers, we may have to add something more than just numbers. We need to perform additions on not only integers but Strings as well. You might wonder how and why we add strings. It's the same as adding numbers, but the strings to be summed up must be within double quotes as usual. Once you put a + sign between the two strings, the output is one string containing both the words/sentences together. The addition of strings (concatenation) is also performed using the '+' operator.

Why do we need to add strings?

Many people might think that if the need is just to combine the words together, why can't we directly write two words together within the double quotes? This will be answered when we start dealing with some bigger programs where the two strings are needed separately but must also be combined later, so addition becomes a must. An easy example: one string that must be combined with only one of twelve: "My birthday is " and one of "Dec", "Jan", "Feb"... You now know how to use strings in variables. Your code could look like this:

trace("My birthday is"+ monthStr);

Variables add extra control in our creation process. We declare variables and assign them strings separately because these strings are needed in the program separately. Hence, we use the first variable wherever the first string is needed and second variable where the second string is needed etc. So now if we want a string which is the combination of these two, we don't waste time in declaring a new variable and assigning it the combined string. In fact, we add these two variables together to form the combined string as output.

The point that must be remembered is that addition is not just about adding numbers but strings as well. The good news is even through this is a new concept, its the exact same process we did for variables – only the outcome is two strings combined. An example taken from an ancient internet joke:

var strPet:String="Benoline"; // user enters the name of their first pet into String strPet var strStreet:String="Programmer Blvd"; // user enters the name of the first street they lived on into String strStreet trace("Your stripper name is " + strPet + strStreet);

Video Transcript

00:00We were talking about adding numbers together and the way we added numbers together were I'm going to make a little trace here
00:07So we can see something in the output and I'm going to do just type a really basic
00:13Addition of 2 plus 2 which that flash will then figure out how to connect them together before it outputs it to the stage
00:20And then it would output 4 and we can see that flash outputs into our trace panel into our output panel the result 4
00:31Now if we wanted to add together text together
00:34We could do it as well
00:35And we could use the same type of style and text and flash like we said was is called strings
00:40So let's try to connect two strings together and what flash basically does when you connect two strings together
00:45It just concatenates them together, which basically means it just sticks them together
00:51So if I would type let's stop using my name if I type hello as
00:56My first string and I do and I add a plus and I type world
01:05And I add my second string so right now we could see that
01:08We're actually adding strings together the same way that we would add numbers together the only difference is that flash realizes
01:16That we're adding two strings together and not two numbers and because we're not adding two numbers together, and it's two strings
01:22It's going to concatenate them, so if we
01:26Run our application right now. We're going to see that a low world now become became one one blob one one one string all
01:34concatenated together
01:35Now if we wanted to create a space in between you could add a space inside of our double quote
01:40Or we could add a space inside of our double quote quote before the world or after the hello word
01:46Or we could add into it another another string by
01:51Add here another string
01:53Which is just a double quote with the space in between and I'm going to add them in together again
01:59If you don't add a plus together, it's going to generate an error
02:04so if I try to run the application in this way we're going to see an error and flash is going to tell us that an
02:10Error that not necessarily tells us exactly what it is, but it tells us that we're in line six
02:16And we could double click on it
02:18It'll take us to our line and it's telling us that there's some sort of syntax error here
02:22It's expecting a right paren before world
02:26So if we look at that world and we go to that point and we say okay before world flash is expecting a right paren
02:35so maybe
02:37That is just enough to tell us okay, so we're missing something here because we don't want to close our our trace out
02:43Yeah, and we could see that we're missing our plus so even if you make a mistake
02:49Look at the debugger look at look at sorry look at the compiling error look what it get what error tells you
02:55What's the description see if it helps see what line it points you to sometimes? It's not going to point you to the right place
03:02but it's going to give you a few clues to help you and
03:07the more we practice with it the more we're going to see how to use the
03:12Compiler and and kind of decrypt messages when they're not very clear, but this message is quite clear
03:17It's telling us we have an error in line 6 and it's even telling us where exactly there is and it's telling us
03:23It's in this area where I just highlighted
03:28Now that we talked about how we could add numbers together and how we could add strings together

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!