mercredi 24 juillet 2013
JavaScript While Loop
In JavaScript, WHILE loop has same function as the FOR loop. The Syntax however is bit different. The WHILE Loop will check if a condition is being met, if yes then it will execute the code lines specified within the Brackets, else it will stop the looping process.
The counter variable must be incremented within the code lines to enable the while command to work properly. Unlike the FOR loop, the syntax does not provide a special spot for specifying the increment.
The initial value of the variable must also be specified before the While command.
Syntax
While(Condition)
{
code lines to be executed
}
Simple JavaScript While Loop Example
We will use the same example as used in the last lesson. Here we will achieve the same objective with While Loop instead of the FOR loop. It will help you easily compare both.
Create a mathematical table for the number 5, i.e. multiply it with numbers 1 to 10 and display results.
Js While Loop
Step 1- We declared a variable called 'a' and assigned it a value of 5.
Step 2- We declared a variable called counter and assigned it a value of 1. We called it counter because its value will change every time the loop is run.
Step 3- The condition- Until the condition in the following round brackets is true (counter <= 10), execute the code in the curly brackets below.
Step 4- We then print on the screen, the number 5, multiplication symbol x, the current value of the variable counter and the result of 5 multiplied with the current value of counter.
Step 5- After printing it will increment the value of variable counter and then go back to top of the loop command. Note that unlike the For Loop example in the last lesson, we enter the increment code within the code to be executed.
STEP 6- When the counter variable value becomes 11 and the loop goes back to top, the condition becomes FALSE as 11 is neither 10 nor less than it. So the loop will no longer be executed.
First time the code is executed, 5 x 1 and the result is a *5 which is five.
Now the next time code is executed, all values remain same but the value of the variable counter is incremented by 1 as we instructed in the for command- counter++ which is same as counter = counter+1. So the second time the loop runs, a * counter is 5 * 2 which results in 10.
While loop result
Example-
Make a JavaScript Program for creating/displaying mathematical tables of any number that user wants
Much similar to the example above. Just that here we will ask the user to enter a number through a Prompt PopUp box, whose mathematical table she wants. Instead of incrementing the value of counter variable by 1, we will do it by 2 in this example.
Instead of providing a value for a, here user will enter a value in the prompt box, which we will assign to a.
javascript while loop
Here we increment the value of the variable counter by 2. counter=counter + 2 means, the new value of counter is equal to the old value of counter + 2.
Once the value of counter becomes 11, it no longer meets the condition of < = 10. therefore the loop stops.
Result
A popup box will prompt the user to enter a number.
popup box JS for loops
table result for Javascript for loop
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire