What is a condition and why is it useful?
If you think about your every day life, you use conditional logic all the time. Example: If I have something I can contribute to the thread then I will post a reply (okay, so mayb not all of us). Similarily, Flash can test if something is true or false and then execute different commands based on that. This could be used to test if a password is correct, or if a movie clip has a certain proprety.
if()
In basic programming languages, "then" usually accompanies each and every if. In Flash, the then is implied. The if operator takes one parameter (the thing inside the parenthesis.) The parameter is a boolean (variable that can only be true or false). If that parameter is true, then Flash executes the code between the curly brackets. If its false then it doesn't. Example:
userInput = "bologna"
if (userInput == "bologna") {
trace("I could use a sandwich!");
}
This tests to see if the variable userInput is equal to the string bologna. In the above case it returns true and the line "I could use a sandwich" is displayed in the output screen. If you change user input to say something like turkey, then the sentence won't be displayed.
<else>
Else can only occur once per if statement. Else tells Flash what to do if the parameter in the if statement is false. Example:
userInput = "ham"
if (userInput == "bologna") {
trace("I could use a sandwich!");
} else {
trace("I prefer bologna.");
}
This will display the line "I prefer bologna." in the output window because "ham" doesn't equal "bologna". If you change userInput back to "bologna" then you should see the first line shown again instead.
else if ()
Else if allows you to make multipath loops. If Flash determines the initial if statement to be false then it begins testing the else ifs until it finds one that is true. Example:
grade = 75;
if (grade >= 90) {
gradeLetter = "A";
} else if (grade >= 80) {
gradeLetter = "B";
} else if (grade >= 70) {
gradeLetter = "C";
} else if (grade >= 60) {
gradeLetter = "D";
} else {
gradeLetter = "F";
}
trace(gradeLetter):
This will test if the grade is greater than or equal to the grade needed for the grade letter. If it is true, then it will assign that and skip the rest of the if/elseif/else statement. If it gets all the way down without finding an answer it will give the grade an F.
switch, case, default and break
I am lumping all of these together, because without one another they are relativly useless. When used together they can accomplish some basic things:
userInput = "bologna"
switch (userInput) {
case "bologna":
trace("my favorite!");
break;
case "ham":
trace("ham is okay, but bologna is better.");
break;
case "turkey":
trace("Yucky, go with bologna next time.");
break;
default:
trace("You must like strange sandwiches");
}
Note the colons instead of the semicolons after each case statement. You can test around with this. The break statements skip out of the switch as soon as one of the conditions is met. In this case you could remove it and there would be no noticable difference.
What is a loop and why is it useful?
A loop is a set of code that gets executed several (or even thousands of) times in a row. There will be some variable in the loop that changes each time. They are useful for hundreds of things. A few examples would be: testing your character against multiple platforms in your newest platformer, testing for multiple correct passwords, testing pratically anything that you have more than one of.
while()
While is the simplest loop there is. It's pretty much self explanatory, but I'm going to explain it anyway. While takes one parameter. This parameter is a boolean. The loop executes whatever is in the block of code while the parameter is true. Example:
myNumber = 10;
while (myNumber <=100) {
myNumber = myNumber + 5;
trace(myNumber);
}
If you copy and paste this into a frame, you will see that Flash runs through the loop until the myNumber <= 100 returns false.
do ... while
This is almost exactly like the code above with a slight difference: The loop is executed and then it checks if it should go back and do it again. So, using a similar example to above to illistrate the difference:
myNumber = 110;
do {
myNumber = myNumber + 5;
} while (myNumber<=100);
trace(myNumber);
You will see that the number is increased by 5 even though it was already above 100. This is because the do ... while loop will always be called at least once.
for()
For loops are my favorite because they are specialized while loops that I often find more use for. The for loops requires three parameters. The first is an initiation spot. The second is the condition. Third is what Flash does each time it completes a loop. For example (no pun intended):
for (x=0; x <= 100; x++) {
trace(x);
}
How Flash reads this: First Flash sets x equal to 0. It then tests x <= 100 and returns true so it runs all of the code between the curly brackets (in this case trace). After that code is done, Flash increments x (adds one to x). Flash then tests if x <= 100 again and then runs the code again and then increments and tests and so on.