Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame
Use these to jump around or read it all
This example looks at the While loop. Remember that usually you use For loops when you know how many times you want to perform a loop and While loops when you are not sure how many times you want to perform a loop. This sample will break the rule! We're doing this to show you how to use variables to count iterations in a loop and to help you get ready for your assignment.
The Script's Effect
Deconstructing the Script
- Another short program. We'll whip right through it.
- While is similar to For in syntax. The difference is that we set the beginning index number and increment right in the For statement. The While statement just contains a condition.
- while(num<=loops) tells the program to do the loop over and over while num is less than or equal to loops. See the use of the < and = signs?
In other words, the program will repeat the loop 3 times, once for num=1, once for num=2, and once for num=3.
- Each time the program goes through the loop it writes "Happy" and adds one to num. When num becomes 4, the loop will end. 4 is not less than or equal to 3, thus the program writes "Happy" three times!
- The finishing touch is the addition of "Birthday" at the end through a document.write statement.
What You Have Learned
Here's another challenge. First, type in the program above and make it work. Then modify the program so that you use Prompt to ask the user "How many times do you want Happy to appear before Birthday?" You should capture the response in a variable.
Remember to use Eval() to change the response from text to a number. The program then should write "Happy" as many times as requested.
Here's a possible answer to this assignment
(this will open a new window)
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...