Programming basics

Conditions

In programming code very often you need evaluate some conditions.

For example you write a letter and want to make such decision… what salutation to use? Mr. or Ms.

In programming with Action Script it will look something like that

salut = "Dear ";
if (clientGender=="f") {
     salut += "Ms. ";
}
else {
   salut +=  "Mr. ";
}

In the first line we assigned a variable for salutation. Then depending on result of our condition we made either decision. Words "if" and "else" are two of ActionScript keywords or reserved words and you cannot use them as a variable identifiers.

Simpler conditions don’t include keyword "else" in case only one action is necessary to do. It may be like that

if (patronAge < 19) {
   message = "Sorry, too young";
}

In the situation where several various conditions are evaluated it’s possible to use a switch structure. Example below assign name of the city to the "city" variable depending on areaCode variable value. Default case embraces all options outside 604, 401 and 250. There are three new keywords here switch shows parameter to evaluate. Every case gives a value to compare with switch value, default – takes the rest of values, break prevents from execution of the rest conditional statements. Watch colons after case and default.


switch (areaCode) {
	case 604:
		city = "Vancouver";
		break;
	case 401:
		city="Toronto";
		break;
	case 250:
		city="Victoria";
		break;
	default:
		city="Unknown";
}



Next