Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


for Loops

for Loops

  • The for loop is another excellent control statement tool. The basic syntax of a for loop follows:

    for ([initial condition]; [test]; [incrementation])
           {
           [action to perform]
           }
    

  • The "initial condition" defines where the loop should begin. The "test" defines the logic of the loop by letting the script know the conditions that determine the scripts actions. The "incrementation" defines how the script should perform the loop. For example, we might produce a visible countdown with the following for loop:

    for ($number = 10; $number >= 0; $number--)
           {
           print "$number\n";
           }
    

  • The script would initially assign "10" to the scalar variables $number. It would then test to see if $number was greater than or equal to zero. Since ten is greater than zero, the script would decrement $number by subtracting one from the value of $number.

To decrement, you use $variable_name--. To increment, you use $variable_name++.

  • Executing the statement block, the script would then print out the number nine. Then, it would go back through the loop again and again, printing each decremented numbers until $number was less than zero. At that point, the test would fail and the for loop would exit.

Additional Resources:

while
Table of Contents
Using logical operators (&& and ||)


Up to => Home / Authoring / Scripting / Tutorial




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers