WMLScript script program design

Source: Internet
Author: User
As a compilation language, WMLScript provides strong functions, statements and database functions, as well as external compilation, access control, and other support, at the same time, detection methods and specific solutions are provided for possible errors during the program running. These contents belong

As a compilation language, WMLScript provides strong functions, statements and database functions, as well as external compilation, access control, and other support, at the same time, detection methods and specific solutions are provided for possible errors during the program running. This content is part of the WMLScript script programming knowledge and further programming requirements. we will introduce this content in this chapter.

5.1 Statement

We have learned variables, controllers, and expressions before, but only these content cannot accomplish a complete effect, because they cannot form a complete control or processing program. Variables are like serious words. expressions, such as phrases in a language, cannot express a complete meaning. only statements are complete sentences in a language, can express the complete meaning and achieve a complete effect. WML Script provides a wide range of statement functions. with these statements, we can establish interaction functions and other complex functions in the WML card.

In WML Script, each statement must end with a semicolon. To develop a rigorous programming style, we recommend that you add a semicolon (;) to the statement when writing a script, which also helps us form a good programming habit.

WML Script statements can be written and arranged freely. We can write multiple statements continuously in the same program line, or divide the same statement into multiple rows. WML Script determines the specific content of the statement based on the semicolon.

WML Script statements include two types. The first type is basic statements, such as empty statements, expression statements, block statements, variable statements, and return statements. The second type is conditional statements, such as if statements. The third type is cyclic statements, for example, while statements, for statements, break statements, and continue statements. The following describes the syntax, functions, and application methods of these statements.

5.1.1 Basic statements

Basic WML Script statements are mainly used for program pattern control and variable declaration. some of them are not unfamiliar to us.

Empty statement

An empty statement is used to define an empty program line. it does not have any identifier or identifier, nor does it perform any hold. It only ends with a semicolon. The syntax is as follows:

;

Obviously, a null statement is a very special statement. In order to make the program more readable, we usually add a few null statements in the appropriate place in the program to separate or highlight the role. For example, the following program contains an empty statement:

Str = 'hello ';

Val = 25;

;

MyVal = val * val 5;

Alert ('Hi, Hi !!! ');

Another example is that the while statement is used to determine a condition and perform the corresponding task when the condition is met. However, if we expect that nothing will be done when the condition is met, then you can configure it with an empty statement so that the fulfillment of the condition is null:

While (! Poll (device ));

This is actually a combination of the while statement and the null statement. Here, the semicolon (;) represents an empty statement. These two statements are used to wait until the poll () function is true.

Expression statement

Expression statements are used to assign values to variables, perform mathematical calculations, or call functions. Expression statements are also the most commonly used statements. the syntax pattern is:

Expression;

The following lines of programs are all regular expression statements:

Str = 'h' yourName;

Val3 = prevVal 4

Counter;

MyValue1 = counter, myValue2 = val3

Alert ('Watch out! ');

RetVal = 16 * Lang. max (val3, counter );

Block statement

Block statement application two curly braces ({}) contain a statement set to form a statement body. Many WML Script statements require block statements to implement the statement body. the syntax pattern of quick statements is as follows:

{

Statement list;

}

The following simple program is an example of an Application Block statement:

}

Vari = 0;

Var x = Lang. abs (B );

PopUp ('Remember! ');

}

Variable statement

Variable statements are used to initialize and assign values to life variables. If you do not assign a value, WML Script automatically assigns the variable in the life of the variable statement to an empty string (''). The basic syntax is:

Var variable name;

For example, if multiple variables exist at a time, the comma (,) interval is applied between adjacent variable names. the syntax pattern is as follows:

VAR variable name 1, variable name 2..., variable name n;

If you want to initialize the variable at the same time as the life variable, you can write it in the following syntax format:

Var variable name = initialization

To help you better grasp the variable statement, we provide a program to apply the statement more:

Function count (stu ){

Var result = 0; // declare the variable and initialize the variable

While (str! = ''){

Var ind = 0; // initialize each loop

// To exit the loop, this block statement should provide the statement for modifying the str value.

};

Return result

};

Function example (param ){

Var a = 0;

If (param> ){

Var B = a 1; // declare variable B and apply variable a to initialize variable B.

} Else {

Var B = a 2; // declare the c variable and apply the variable to initialize the c variable

};

Return a; // return the value of variable.

};

Comment statement

Strictly speaking, the comment statement is not really a statement in WML Script, but it is just a pre-pointing rule. However, it also has a strict syntax and annotation method, so here we are still like other programming languages, let's take the WML Script annotation method into account in the statement situation first.

Annotations do not have any effect when performing the program, but can be used to describe the program to enhance the readability of the program. To form a good programming style, we should develop a good habit of writing comments. There are two ways to express comments:

(1) comment a line through the double slash, so that the characters in the double oblique sequence will become comments without being executed. This comment row can be written in a single, unique row, or placed behind other statements.

For example, you can perform the following annotations:

// Variable j is used to decimal the number of days per month

J = 0; // Here we assign j to 0.

(2) use the symbols "/*" and "*/" to specify the comment statement. this annotation method can be used to annotate multiple lines, the content between the symbols "/*" and "*/" is the comment statement. For example, you can comment multiple lines as follows:

/* We define two variables: I and j. Where:

I is used to describe the number of months in a year,

J is used to describe the number of days per month */

J = 0;/* Here we assign j to 0 */

Return statement

The return statement is mainly used in the function body. before the function ends, you can use the return statement to return the result of the function processing to the statement that calls the function. Its syntax is as follows:

Return expression;

The following function provides an example of using the return statement:

Function square (x ){

If (! (Lang. isFloat (x) return invalid;

Return x * x

};

5.1.2 condition statement

In a condition statement, when a certain condition is met, some specified code will be executed, while in another condition of the code group, other code will be executed. The condition statement of WML Script is the if... else statement. its general expression is as follows:

If (condition ){

Code block 1

}

Else {

Code block 2

}

In this way, when the condition is met, code Block 1 is executed; if the condition is not met, code Block 2 is executed. If there is only one statement in the code block and code block 2, curly braces ({}) can be omitted. if there are multiple statements, you must include the code block in curly brackets. In the if... else statement, the else part is optional. that is to say, we can apply the following expressions:

If (condition ){

Code block

}

In this way, when conditions are met, the code block will be executed. if the conditions are not met, nothing will be done.

For example, if we need to determine the score of a student, if the score is greater than 60, then we think that the student's natural performance is qualified, and vice versa, at the same time, the status is recorded to the variable status. the corresponding WMLScript statement is as follows:

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.