Jul 01 2008

Conditionals

Published by Andy G. Cook at 3:50 pm under Beginners, Flash

If statement:

Think about life. Oftentimes, you make choices based on something.

If I have $.99, I can buy this doughnut, otherwise, I can’t.

If I get a 90 or above, I get $50. If I get above a 80 but below a 90, I am going out for ice cream, but if I get below an 80, I am grounded.

These types of statements are called conditional statements, and they come up in programming all the time. Most are in the form of an if/else statement. Lets take the aforementioned examples and make them into code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if ( nMoney >= .99 )
{
	bCanGetDoughnut = true;
}
 
if ( nGrade >= 90 )
{
	nMoney += 50;
}
else if ( nGrade >= 80 && nGrade < 90 )
{
	bGettingIceCream = true;
}
else
{
	bGrounded = true;
}

Basic structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ( /*Something happens*/ )
{
	/*Do this code and end the if statement*/
}
else if ( /*the first thing didn’t happen, but this one did*/ )
{
	/*Do this code and end the if statement*/
}
else if ( /*So far nothing is true*/ )
{
	/*Do this code and end the if statement*/
}
else
{
	/*Nothing else was true, so lets just run this code*/
}

The actual conditions:

So you need to actually evaluate something right? We do that by comparing things:

( sName == “Bob” ) - Use == to check whether the left side is equal to the right side

( sName != “Bob” - The ! is used to check whether something is not happening

( nHeight > 60 ) - Use to check whether left side is greater than right side

( nHeight < 60 ) - Checks whether left side is less than right side

( nAge >= 21 ) - Checks whether left side is greater than or equal to right side

( nAge <= 20 ) - Checks whether left side is less than or equal to right side

There are also ways to string together conditionals. They come in the form and/ or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if ( sWeather == "cold" || sWeather == "windy" )
{
	wearSweatshirt ();
}
 
//English: If the weather is cold, or the weather is windy, bring a sweatshirt.
/*You can use the || as the english word or. If any one of the things you are comparing in your or statement is true,  then the if statement will evaluate to true and do whatever is in your brackets.*/
 
if ( bHasSweatshirt == true && sWeather == "cold" )
{
	wearSweatshirt();
}
 
//English: If you have a sweatshirt, and the weather is cold, wear the sweatshirt.
/* You can use the && opeator to check to make sure more than one thing is true.  All of the statements in your && statement must be true in order for the code in the brackets to be executed.*/

You can string together these conditionals to do some pretty advanced checking too

1
2
3
4
5
if ( ( sName == "Donny" || sName == "Leo" || sName == "Mike" || sName == "Raf" ) && ( nAge >= 13 && nAge < 20 ) && bIsTurtle == true  )
{
	trace ( "It’s  a Teenage Mutant Ninja Turtle" );
}
/*So this checks whether the sName string is equal to Donny, Leo, Mike, or Raf, and checks whether the age is greater than 13 but less than 20, and checks whether it is a turtle.*/

A few things to remember when using || and &&: Once you use an && or an ||, you are done comparing that side of the conditional and are effectively starting a new conditional.
Example:

1
2
3
4
if ( myString == "this is oneconditional" || mString2 == "this is a completely different conditional statement" )
{
	trace "In order to get into here, one of those things had to be true.";
}

Switch statement:

One step to being a great coder is being efficient. Typing less means saving time. So developers thought of the switch statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch ( /*thing you want to check*/ )
{
	case:
	//Do whatever code is right here
	break; /*- Ends the switch statement. Just like when and if statement ends.*/
 
	case:
	//if above case didn’t match up, do this code if it matches up
	break;
 
	case:
	//Nothing has matched up yet, so do this code if it matches
	break;
 
	default:
	//Okay, so nothing matched up. This is like else. Just run this as a last ditch
}

Lets see it in action:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var nAge:Number = 18;
switch ( nAge )
{
	case 16:
	trace ("Sweet sixteen");
	break;
 
	case 17:
	trace ("Rated R movies");
	break;
 
	case 18:
	trace ("Scratch tickets");
 
	case 19:
	trace ( "Nothing great really happened");
	break;
 
	default:
	trace ( "You are not 16-19" );
}

Output Window: Scratch Tickets

That’s the basics of conditionals. They come up all the time, so get to be friends with them.

Comments and questions are encouraged. Thanks!

Trackback URI | Comments RSS

Leave a Reply