Logo         Community
The Company
Your Account
Contact Us
Demystifying WMLScript (part 2)
Loops, operators, and some pre-defined WMLScript functions.

| Round And Round.... |

As you may already know, a loop is a simple programming construct which allows you to repeat a set of program statements over and over again; the number of times the statements are repeated is either dependent on the fulfillment of a pre-defined condition, or fixed in advance. WMLScript allows both types of contortions via the "while" and "for" loops (incidentally, unlike "real" programming languagss like C and Perl, these are the *only* two loops the language supports!)

The syntax of the "while" loop is as follows:

while (condition)
do this!

Or, to make the concept clearer,

while (rich Aunt Petunia's in a good mood)
hit her for a loan

The "condition" here is a standard conditional expression, which evaluates to either true or false.

In order to demonstrate this better, take a look at our next example, which asks for a number and calculates the factorial of that number (for those of you who flunked math class, the factorial of a number is the product of that number and all the numbers preceding it, all the way down to 1)


<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<card id="Factorial" title="Factorial">
Gimme a number:
<input format="N*" name="number" />
The factorial of the number $(number) is $(answer)
<do type="accept" label="Calculate">
<go href="fact.wmls#factorial($(number))"/>

The WML deck above sets up the page which asks the user to enter a number. Once the number has been entered and the user has hit the ACCEPT button, the WML page will call the factorial() function in the file "fact.wmls"

Output image


// function to calculate factorial
extern function factorial(number)

var tempfact = 1;

while(number != 1 && number != 0)
tempfact = tempfact * number;


The factorial() function accepts a number and uses a "while" loop to count down from that number until it reaches 1. The product is then passed back to the browser via the standard WMLScript libraries.

Note the conditional expression, which ensures that a factorial is not calculated if the number entered is either 0 or 1, and the autodecrement operator --, which is used to reduce the value of the variable "number" by 1 during each iteration of the loop.

Output image

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback