4
 It is time to write your first conditional. Here is the syntax as a refresher. 1 if (myCondition ) { 2 myCode; 3 } 1 private var enemyStrength:Number = 500; 2 private var userHP:Number = 300; 3 4 if (enemyStrength >= userHP) { 5 trace("User dies!"); 6 } 7 8 9 Great job! That's all th ere is to this. To finish this level, type true or false. Will 'User dies!' (without quotes) be displayed in the output window? True 1 private var achievementEarned:uint = 0; 2 3 if (achievementEarned == 1) { 4 trace("Display medal!"); 5 } 6 7 if (achievementEaned == 0) { 8 trace("Do not display medal"); 9 }

4-3 First Conditional.docx

Embed Size (px)

Citation preview

8/14/2019 4-3 First Conditional.docx

http://slidepdf.com/reader/full/4-3-first-conditionaldocx 1/4

 

It is time to write your first conditional. Here is the syntax as a refresher.

1 if (myCondition) {

2 myCode;

3 }

1 private var enemyStrength:Number = 500;

2 private var userHP:Number = 300;

3

4 if (enemyStrength >= userHP) {

5 trace("User dies!");

6 }

7

8

9

Great job! That's all there is to this. To finish this level, type true or false. Will 'User dies!' (without

quotes) be displayed in the output window?

True

1 private var achievementEarned:uint = 0;

2

3 if (achievementEarned == 1) {

4 trace("Display medal!");

5 }

6

7 if (achievementEaned == 0) {

8 trace("Do not display medal");

9 }

8/14/2019 4-3 First Conditional.docx

http://slidepdf.com/reader/full/4-3-first-conditionaldocx 2/4

Fantastic. You are really getting the hang of conditionals. Do you notice how the two conditions here

are complete opposites? One handles the case that the user has the achievement while the other

handles if the user does not have the achievement. In the next level, I will show you how to combine

these conditionals with an 'else' statement.

ELSE

1 private var achievementEarned:uint = 0;

2

3 if (achievementEarned ==1) {

4 trace("Display medal!");

5 } else {

6 trace("Do not display medal");

7 }

Do you see what I did there? I just cropped off several lines of code. They way this code works is that if

the condition in parenthesis is true, then the first block of code in brackets will be executed. Otherwise,

the second block in brackets of code (after the else line) will be executed. Either way, AT LEAST ONE

block of code will be executed no matter what.

1 private var lessonAnswer:String = "Next";

2 private var userSubmission:String;

3

4 if (userSubmission == lessonAnswer) {

5 gotoNextLevel();

6 } else {

7 decreaseUserHP();

8 }

9

And there you have it! We have a block of code that checks to see if the user has submitted the correct

answer or not. Further, this code either moves the user on to the next level, or decreases their health.

Now, where do you think that code like this might come in handy? ; )

8/14/2019 4-3 First Conditional.docx

http://slidepdf.com/reader/full/4-3-first-conditionaldocx 3/4

Of course, I've left out a little, like the functions decreaseUserHP(); and gotoNextLevel(); but we will get

there.

We learned about if/else statements. Now we are going to learn about if/else if/ else statements. This

is what the syntax for an if/else if looks like:

1 if (conditionOne) {

2 // code executed if conditionOne is true

3 } else if (conditionTwo) {

4 // executed if conditionOne is false and conditionTwo is true

5 } else {

6 // executed if conditionOne and conditionTwo are both false

Take a minute to study the code above; you will need this knowledge very soon. Do you see how this is

going to work? There are three possible outcomes here, but only one will get executed, depending on

the conditions. The final outcome occurs if both conditions are false.

Notice how the line: } else if (conditionTwo) {

moves the } else { to line 5. Remember, we use the words if and else in every if/else statement.

1 trace("type 1 for cliffs, 2 for enemy trail, or 3 for forest");

2 private var userChoice:uint;

3 if (userChoice == 1) {

4 gotoCliffs();

5 } else if (userChoice == 2) {

6 gotoEnemyTrail();

7 } else {

8 gotoDeepForest();

9 }

8/14/2019 4-3 First Conditional.docx

http://slidepdf.com/reader/full/4-3-first-conditionaldocx 4/4

Congratulations! You just wrote your first if/ else if / else statement! You just gave the user three

options to choose from. By the way, you are not limited to just three possibilities when writing if/ else if

code. If you want, you can write if/ else if/ else if/ else if/ else and so on as much as you want!

Although if you there are tons of options, a 'switch statement' might be better, but we will learn about

that later.

One other neat thing about this code. What happens if your user tries to troll you? What happens ifthey type in 7, or something silly like "pancakes"? In this case, the user would go into deepForest. Think

about it. Does userSubmission == 1? Does userSubmission ==2? Nope! So they are going back to the

forest. The final else statement is like a default answer. It is very important to know how your code will

act when the user tries to mess with it or if the user makes a mistake.

One other note, the code for capturing the user's input text is not included here. We'll learn how to do

that later.