What is the switch statement?
What is the switch statement?
Rating:
The switch statement is a conditional statement. It is similar to the if statement. The switch statement compares an expression with cases, and if any case returns true, the associated statements are run. If none of the cases evaluate to true, the default statement is run. The switch statement can be used for writing code that makes decisions. The decision-making process is used to execute a program according to one or more conditions.
The syntax of using the switch statement is as follows:
{
case label:
statements;
[break]
case label:
statements;
[break]
[default
statements;
break]
}
In the above syntax, the break statement and the default statement are optional. The break statement stops the execution of the switch statement after the execution of a case statement. If the break statement is removed, all the case statements following the correct case statement will be executed.
For example:
switch(a){
case "a":
trace("You have entered a")
break
case "b":
trace("You have entered b")
break
case "c":
trace("You have entered c")
break
default:
trace("You have entered the wrong character")
break
}
The above code will return "You have entered b", as the variable is b.
Rating:
Other articles
- What is a mask layer?
- What are embedded fonts?
- What is leading?
- How to create a motion path for a tweened animation?
- What is the leading property?