Learning objectives: To further grasp the use of conditional statements, in order to achieve the purpose of independent use
Tutorial: In the previous section, we initially tried to use the conditional statement, because the conditional statement is a very high frequency statement, so we need to delve into it now.
The basic format of a conditional statement
if (conditional expression) {
When the condition is true, the command to be executed
}
This statement is: When the condition is true, execute the command; Now we take advantage of the knowledge we have learned before. To do an example, the requirement of this example is: After clicking the button, if the text in the input text is "worm", then the worm moves, otherwise the worm does nothing. Follow these steps:
1. Drag a text box on the stage with a text tool, open the property panel, modify its properties to input text, and note that the border is selected. As shown in figure
{this.resized=true;this.style.width=500;} "border=0> |
2. Put bug MC on stage and enter instance name bug for it
3, put another button on the stage.
4, select the button, in the Action panel input
On (Press) { if (This.mytxt.text = = "Worm") { This.bug._x + 20; } } |
Through the test can be seen, as long as the input text as "bug", execute the statement command, or nothing to do
Second, the compound form of conditional statement
if (conditional expression) {
Command
}
if (conditional expression) {
Command
}
if (conditional expression) {
Command
} ........... It means that the first condition is met, the command for the first condition is fulfilled, and if the second condition is not satisfied, then the command that executes the second condition does not satisfy the third condition to continue checking ... Let's use the example above to illustrate the effect now. Delete the statement on the button and enter the following statement
On (Press) { if (This.mytxt.text = = 10) { This.bug._x + 10; } if (This.mytxt.text = = 20) { This.bug._x + 20 } if (This.mytxt.text = = 30) { This.bug._x + 30; } } |
Through the test you can know: input text input 10, then move 10 pixels, enter 20 to move 20 pixels .... If you do not enter 10, 20, 30, do nothing.
III. embedded structure of conditional statements
if (condition) {
if (condition) {
Executed statement
}
}
We add one more input text in the previous instance, named Mytxt0, and enter the following statement on the button.
On (Press) { if (This.mytxt0.text = = "Worm") { if (This.mytxt.text = = 20) { This.bug._x + 20; } } } |
Tested: The statement in curly braces is executed only if the text above is "worm" and the following text is 20.
Iv. the complete structure of conditional statements
if (condition) {
Command
}else{
Command
}
This statement is when the condition is met, the command is executed, or the latter command is executed. In fact, parallel nesting can make conditional statements quite complex, and these more complex left to the reader to taste.