Flash conditions and looping statements

Source: Internet
Author: User
Tags constant expression valid
condition | loop | statement
The conditions and looping statements of the Actions cript have if, else, else if, for, while, Do...while, for...in, and switch. In Flash they are called Process Control actions (action), but in fact, there is nothing different between quality and other programming languages.
Because the conditional and circular statements are the most basic and practical statements in the programming language, they should be explained here in detail. For people with programming experience such as C + + and Java, you can browse at random.
Conditional language if sentence--if
It can be said to be the most basic conditional Judgment statement in program language. There is a figure in any language.
The following example lets you know its format:
Name = "SiC";
The following is an if statement
if (name = = "SiC") {
Trace ("author");
}
The IF statement determines whether the statement within the curly braces is executed by determining whether the logical expression in the following parentheses is true. If the logical expression name = = "SiC" returns the true value, the "author" is displayed in the Output window, otherwise it is not executed.
Then let's imagine the effect of the following example:
if (1) {
Trace ("always executes");
}
If you remember the contents of the previous data type, it should be seen that the IF statement is essentially superfluous, because for constant 1, the value of the logical expression is constant to True (1 is the numeric representation of the logical value TRUE). But for constant 0, it is always false. You can try to change the 1 in the example to a string constant (such as "hi!") Let's see what the effect will be.
Look at one more example:
Name = "SiC";
The following is an if statement
if (name = "SiC") {
Trace ("author");
}
What is the difference between this example and the first example? The difference is that the first one uses = =. For this example, the If judgment is always true. Think about why? The problem is that the assignment operator = instead of the logical operator = = is used. For an assignment operation, the logical value returned by the value is always true. This is a common mistake for beginners.
Be sure to distinguish between assignment operator = and logical operator = =. Otherwise you will encounter some inexplicable errors and problems, and the grammar check can not find errors (because the assignment operation expression is also a valid conditional expression). So keep in mind that the equality logic operator for the Actions cript is = = instead of =.
If...else
What if you want to perform another operation when you are not in a condition to judge? It's easy to add an else statement block after the IF statement:
Name = "Unknown";
Here is the If...else statement
if (name = = "SiC") {
Trace ("author");
} else {
Trace ("Unknown");
}
It's simple, isn't it? Just put the statement that you want to execute when the result is false, in the curly brace after else.
If...else if
What if there are a lot of conditions to judge? To do, use else if to resolve:
Name = "Sam";
Here is the If...else if statement
if (name = = "SiC") {
Trace ("author");
else if (name = = "Flash MX") {
Trace ("Flash MX is the software name.");
else if (name!= "Unknown") {
Trace ("Who is" + name + "?");
} else {
Trace ("Unknown");
}
You can take any number of else if to judge multiple conditions, and the last else statement block is optional (depending on the actual need). The only thing that is not enough is else if too much execution is slow (also a big problem in other programming languages). Now it's time to switch out.
Switch
Switch
Switch is a handy command in Visual Basic. It can perform different operations by judging different conditional expressions. But there is not much flexibility in the Actions cript. Because the condition of the switch in the Actions cript is fixed to = = = Absolute equals (including the same data type), it is not as if you can use the conditional operators such as >= in VB. So, else if is useful if you need to judge greater than or less. Now let's look at the following example:
MyNumber = 3; Assign value to MyNumber
The following is a switch statement
Switch (MyNumber) {
Case 1:
Trace ("This is the number I want to get.");
Break
Case 2:
Trace ("This number is a little bit smaller than mine.");
Break
Case 3:
Trace ("This is my number.");
Break
Default
Trace ("This is not the number I want.")
}
The example above is a complete block of switch statements. After the case keyword is the condition that needs to be met, if it is not satisfied, Actionscipt looks for a default statement block and, if it does, executes the statement. In addition, you may have found a break keyword after each statement block, why? Because if you do not use a break to jump out of the switch condition selection statement, the program continues to search down the case item (including the Defualt block) that satisfies the condition and executes the statements in it. The following is a modified example:
MyNumber = 3; Assign value to MyNumber
Here is a switch statement without a break
Switch (MyNumber) {
Case 1:
Trace ("This is the number I want to get.");
Case 2:
Trace ("This number is a little bit smaller than mine.");
Case 3:
Trace ("This is my number.");
Default
Trace ("This is not the number I want.")
}
To run this example, you will find that the "This is my number" and "This is not the number I want" will be exported at the same time. Why? Because there is no break, the conditional selection statement continues to execute after the case 3: statement block that satisfies the condition is run, and the default block is always executed, which produces the result. Some common procedural errors come from this.
In the Actions cript there is also a continue command for looping, which can jump directly to the conditional detection portion of the loop (that is, the conditional judgment of the next loop immediately). This command is not commonly used, so there is no explanation here. .
Loop Statement--for
Loop statement
For
For a For loop, I think most readers are not unfamiliar. Below is a program segment that asks for the sum of the natural numbers from 1 to 100.
var sum = 0;
Here is the For loop
for (var i=1; i<=100; i++) {
sum = sum + i;
}
Trace ("sum=" +sum);
The contents of the for back brackets are divided into three parts: the initial value; cyclic conditions; The way the cyclic values change. There is nothing to say about the initial value, just take it. The circulation condition is under what condition continues the circulation, as long as understands the logical expression to be possible; The change of the cyclic value can be changed with any assignment statement. The following is a modified example:
var sum = 0;
Here is the For loop
for (var i=2; i<100; i+=2) {
sum = sum + i; Trace (i);
}
Trace ("sum=" +sum);
In the example above, I changed the initial value I to 2, and the condition changed to <100 (that is, not 100), and the cyclic value was 2 per plus. After the run, look at the results, the result is 1 to 100 of the open interval of all the sum. If you do not know the internal mechanism of the cycle, you can delete the above example for the loop body//trace (i); Before the code is run, the I value is listed in the Output window. So what if the initial value doesn't satisfy the cyclic condition? You can change the i=2 to i=100 to see.
Corresponds to a for and a for...in loop, which involves the contents of the array and the object, which is described later
While & Do...while
While & Do...while
To be honest, for general programming while not much. But you will find the value of it when you need it.
The while loop follows the following steps at run time:
Check to see if the condition within the while behind parentheses is valid.
If the condition is true, run the statement within the statement block. Otherwise, end the loop, and run the statement following the loop body.
After running the statement within the statement block, return to the first step.
n = 0;
Here is the while loop
while (n < 5) {
n++;
}
Trace ("n=" +n);
The above example runs with the result of n=5. When n is less than 5 o'clock the condition of the loop is set up, so the n++ (i.e. n increases by 1) is run. When n equals 5 o'clock, because the loop condition is no longer valid, the loop is aborted, and the following trace statement is executed.
<!--/icon and title--><!--message-->
Source: Flash Bar


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.