View Full Version : ;

06-10-2003, 08:39 AM
At the moment, I've been sprinkling my code with semicolons pretty much at random.

I know they are required between statements, but are they actually needed at the end of a script, or at the end of a list of statements?

Do I need to write

if (condition) then {statement;statement;};

or can I just have

if (condition) then {statement;statement}

if nothing else follows after?

06-10-2003, 08:47 AM
no, not required at the lines ends unless u don't want to make a new line by pressing ENTER

also, they required within the for loop only, not the if statment

Mortimer Jazz
06-10-2003, 10:52 AM
Flash is pretty lax about requiring semi-colons, so you can omit them completely as long as your statements are separated by carriage returns (ie on separate lines in the actionscript window) , however I certainly wouldn't advise leaving them out.

With the exception if conditionals (if/else etc) and functions you should put a semicolon at the end of each statement to signify the end of that statement. This also allows you to put several statements/declarations on a single line should you wish to do so:


Obviously this ruins the clarity of your code, so it's not advised.
The closing "}" bracket on an if-statement or function is a way of denoting the end of that function, so the semi-colon isn't really needed there.

However, it is convension that if you write a "function literal" you put a semi-colon at the end. I have no idea why and it isn't necessary. Below is a fucntion literal and below that is a normal function, just to demonstrate

//function literal (some people put semi-colons after the closing bracket, but not essential)
doSomething = function(){
//code here

//standard function (don't put a semi-colon afetr the closing bracket)
function doSomething(){
//code here

As faffy says, they are only required to separate statements within a for loop.

06-10-2003, 01:35 PM
at the end of the day, you will probably always check your syntax anyway and that will tell you if you are missing semicolons or not.

06-10-2003, 11:29 PM
Mortimer Jazz
I have no idea why ... Isn't that just because your first example is not really a function, but a statement (doSomething = ...)?

Mortimer Jazz
06-11-2003, 12:09 AM
Well, good point, but it's not so straight forward.
Just as "instance" and "object" can be used interchangably so can "statement" and "function".

Strictly speaking it's a function (a function-literal to be precise), but a function declaration is also a statment (and this goes for both normal functions and function literals). This is why I thought it better to say I don't know the reasoning behind including the semi-colon. I can hazzard a guess, but I don't actually know for sure:

If I remember correctly, both types are functions are nothing more than variable assignments (funtions are just datatype, and so can be treated as such), but the difference is that normal functions are available to all scripts in your movie, regardless of where the function is declared (because they are the first things to get processed by the flash player), whilst function literals are only available to code written after their declaration, and so act more like a standard variable assignment.

In other words function literals are sort of proceedural in nature, and so the semi-colon indicates the end of that statement, and that we're ready for the next. This doesn't really apply to normal functions due to the fact that they always get processed before everything else.

I hope this helps :)

06-11-2003, 12:24 AM
This is not my thread, but it's always a pleasure to stumble across one of your in-deep explanations of AS

Mortimer Jazz
06-11-2003, 12:37 AM
Not a problem :)
Like I said, I'm not 100%, but it sounds like the most logical explanation to me.

[edit: I should just clarify (for the purpose of anyone searching threads, and so I don't get shouted at) that when I say "normal functions are available to all scripts in your movie, regardless of where the function is declared", it is standard proceedure to declare all 'normal' functions on the first frame of your movie. If you declare a function on a frame that the playhead hasn't reached yet, and then try and call that function it will not be called because it doesn't exist ]

Andy, I know this is a post to the newbie forum, so i'll just state, obviously this isn't essential stuff to know.

Just remember that you only need semi-colons at the end of variable assignments (anywhere where a value is assigned to a variable name)

myVariable = "my value";

That's about it really.
Hope this helps :)