00:00
00:00
Newgrounds Background Image Theme

Allthingz2020 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Control Structures

1,121 Views | 11 Replies
New Topic Respond to this Topic

AS: Control Structures 2006-01-22 08:25:37


AS:Main

AS: Control Structures

Good To Know
AS:Variables by Rantzien

First Few Words
I'm pretty sure some of these I will take up here has been covered already but to easily compare the different control structures I will show 6 different here.

The Control Structures
The different control structures we will examine are:
- if
- for
- while
- do while
- switch
Plus a little extra list on operators.

if statement
The if statement goes by 3 different statements; if, if...else and if...else if.
One example of the if statement is as follows:

if (var1 == 10) {
trace ( "var1 is the same value as 10" );
}

This will check if the variable var1 is more than the value 10.
And if it is var1 is the same value as 10 will be shown in the Output window.
To use the same but with the if...else statement you can use:

if (var1 == 10) {
trace ( "var1 is the same value as 10" );
}
else {
trace ( "var1 is not the same value as 10" );
}

If var1 is not more than 10 the var1 is not the same value as 10 string will be shown in the Output window.
You can use the if, if...else and if...else if statements in the same code like this:

if (var1 == 10) {
trace ( "var1 is the same value as 10" );
}
else if (var1 == 11) {
trace ( "var1 is the same value as 11" );
}
else {
trace ( "var1 is not the same value as 10" );
}

There, now we got the code for the different if statements.
You should pretty easily see how it works since it's very common to the english language.

for statement
The for statement is when you want something to happen until you want it to stop...
Not a very good explanation but I'll show you the built for it:

for ( var i = 0; i < 10; i ++ ) {
}

This is the most common use for it.
First parameter declares a new variable count to the value of 0. If you dont understand this parameter see AS:Variables as posted above.
Second parameter is to see how long it will keep doing this.
On this example it will do the actions as long as i is lower than 10.
So the actions will stop and not go to infinite you have to set the third parameter to do something. In this case it will increase i by 1 until i is not lower than 10.
Example:

for ( var i = 0; i < 10; i ++ ) {
trace ( "You are now " + i + " years old" );
}

This will trace in the Output window:

You are now 0 years old
You are now 1 years old
You are now 2 years old
You are now 3 years old
You are now 4 years old
You are now 5 years old
You are now 6 years old
You are now 7 years old
You are now 8 years old
You are now 9 years old

To stop the loop from looping somewhere you want you can add a break;

for ( var i = 0; i < 10; i ++ ) {
trace ( "You are now " + i + " years old" );
if ( i == 5 ) {
break;
}
}

This will stop the loop from looping when i = 5 so the output will show like this:

You are now 0 years old
You are now 1 years old
You are now 2 years old
You are now 3 years old
You are now 4 years old
You are now 5 years old

See? This was not so hard that you thought right? ;)

while statement
while is very much alike the for statement because it loops until false.
You can also say that it is like the if statement but loops instead of doing it every frame.
I reccomend using for because atleast I think it's more simplier.
Just look at this example between while and for:

while
var i:Number = 0;
while (i < 10) {
trace (i);
i ++;
}

for
for (var i = 0; i < 10; i ++) {
trace (i);
}

So you can use the for statement or while statement and both do the same things.

do while statement
In the while statement the actions will only be trigged while the statement is true.
But in do while the do comes first followed up by the while which makes the actions trigger once even thought it is false;

do{
i ++;
} while (!uber)

switch statement
[ thanks to AS bible for the switch info ]
The switch statement can be said to use many if statements but in another way. This may come in handy in some occasions.

var Value:Number = 10;
switch (Value) {
case 11:
trace ( "11" );
case 10:
trace ( "10" );
case 5:
trace ( "5" );
}

This will be seen in Output window:

10
5

So this basicly checkes everything from its value which in this case is 10 and lower so everything from 10 and lower cases will be shown. Therefore not case 11.
If you want only 10 to be shown you can make a break; as used above:

var Value:Number = 10;
switch (Value) {
case 11:
trace ( "11" );
case 10:
trace ( "10" );
break;
case 5:
trace ( "5" );
}

A Little List of Operators
= - used when decalring a variable: var Value:Number = 10;
== - used when checking a variable: if (Value == 10) { }
> - used when checking if a value is bigger than another value: if (Value > AnotherValue) { }
< used when checking if a value is smaller than another value: if (Value < AnotherValue) { }

Just a small list with common operators.

And that's it for me

-Snail-


BBS Signature

Response to AS: Control Structures 2006-01-22 08:30:39


Sometime I swear you just make AS topics for the sake of it. Don't reprocess shit that has already been done, "control structures" is a stupidly broad range to cover, for and while are loops and have already been done, if is so amazingly simple it shouldnt have to be done, seriously come on, it isnt a competition to see how many times you can get your name on the list, the only thing youve covered here that hasnt been covered before is switch, which is still just a single operator. come on.

Response to AS: Control Structures 2006-01-22 08:31:54


Ok, sorry.
Denvish doesn't have to include this but I just thought this is a easy way to understand the different statements...


BBS Signature

Response to AS: Control Structures 2006-01-22 08:34:49


Snail sez:
You are now 5 years old

But mummy said I am not little! >:(

... Nice tut, pretty useful although there have been two tutorials about if and while.


BBS Signature

Response to AS: Control Structures 2006-01-22 08:35:47


So you can use the for statement or while statement and both do the same things.

No they don't, for loops have a fixed number of iterations denoted by the condition and next arguments, while loops repeat indefinitely.

Also, you missed out "for in", which iterates with respect to properties in an object (or elements in an array)

Pretty nice tute though

Response to AS: Control Structures 2006-01-22 08:36:47


At 1/22/06 08:34 AM, -Toast- wrote:
Snail sez:
You are now 5 years old
But mummy said I am not little! >:(

No, you grow fast.
Just in a simple loop you are from 0 to 9 years old :D


BBS Signature

Response to AS: Control Structures 2006-01-22 09:25:06


At 1/22/06 08:35 AM, Sekky wrote:
So you can use the for statement or while statement and both do the same things.
No they don't, for loops have a fixed number of iterations denoted by the condition and next arguments, while loops repeat indefinitely.

while loop also proven to run slightly faster than for loop, so try to use than when possible.

Response to AS: Control Structures 2006-01-22 09:46:33


At 1/22/06 09:25 AM, T-H wrote: so try to use than when possible.

wouldn't dream of it, my for loops are too convenient than an extra two lines.

Response to AS: Control Structures 2006-01-22 11:29:04


At 1/22/06 08:25 AM, -Snail- wrote: I'm pretty sure some of these I will take up here has been covered already but to easily compare the different control structures I will show 6 different here.

Actually all of these were covered by BleeBlap in Loops and Conditions. =/
I think that you should have covered something new instead, like the structure of a condition, how a condition is checked (in which order etc.) and how it returns a value and makes the loops and such work. And maybe the order Flash reads syntax in general.


BBS Signature

Response to AS: Control Structures 2006-01-22 11:43:45


At 1/22/06 09:46 AM, Sekky wrote:
At 1/22/06 09:25 AM, T-H wrote: so try to use than when possible.
wouldn't dream of it, my for loops are too convenient than an extra two lines.

haha, yes teh for loops are far superior i know.

Response to AS: Control Structures 2006-01-23 20:09:36


Also, if actionscript is anything like C++...

If I remember correctly, in C++ a do/while will always execute at least once.

One of those while commands in C++ does, anyhow.


Writer @ www.Johnrickett.com

Response to AS: Control Structures 2006-01-23 20:15:44


At 1/23/06 08:09 PM, Johnny_Krysys wrote: Also, if actionscript is anything like C++...

If I remember correctly, in C++ a do/while will always execute at least once.

This is true