The while Loop : The while loop loops through a block of code while a specified condition is true. Syntax of while loop is as follow :
while (variable<endvalue) { code to be executed }
For example
while (i<3) { y=y + "The number is " + i + "<br />"; i++; }
The above while loop will run when value of i less then 3, and it will stop working when it is exceed or equal to 3.
The do .. while Loop : The do .. while loop is a variant of the while loop. This loop will execute the block of code once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. Syntax of do .. while loop is as follow :
do { code to be executed } while (variable<endvalue);
For example
do { y=y + "The number is " + i + "<br />"; i++; } while (i<3);
The above loop will run once before checking condition, and then it will check condition every time, and run only if condition met.
When you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform tasks. There are two different kind of loops, we use in JavaScript, these are as follows :
for
while
We discuss only for loop in this tutorial.
The for Loop : The for loop is used when you know in advance how many times the script should run. Syntax of for loop is as follow :
for ( variable = startvalue; variable<endvalue;variable = variable + increment) { code to be executed }
For example
for (i=0; i<3; i++) { y=y + "The number is " + i + "<br />"; }
This for loop runs 3times, and write results in every new line. The result of above code will be :
Alert Box : An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. Syntax of Alert Box is as follows :
Javascript alert box.For example Javascript alert box in html.Confirm Box : A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. Syntax of Confirm Box is as follows :
Javascript confirm box.For example
Javascript confirm box in html.Prompt Box : A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. Syntax of Prompt Box is as follows :
Javascript prompt box.For example
Javascript prompt box in html.To display line breaks inside a popup box, use a back-slash followed by the character n. For example
The switch statement are used to perform different action based on different conditions. The switch statement can be replaced by performing many if-then-else-if conditions, but when the answer of an evalution can give you many different answers, the switch-case statement can be more efficient. Syntax of switch statement is as follow :
switch(x) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if x is different from case 1 and 2 }
The value of the variable x will be compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. For example
switch ( day ) { case 0: y="Sunday"; break; case 1: y="Monday"; break; case 2: y="Tuesday"; break; }
If the value of variable day is 0 then result will be "Sunday", and result will be "Monday" and "Tuesday" if values will be 1 and 2 respectively.
We use the default keyword to specify what to do if there is no match. For example, there is no value equal to 0, 1 or 2, then write a default message, for example write "friday".
switch ( day ) { case 0: y="Sunday"; break; case 1: y="Monday"; break; case 2: y="Tuesday"; break; default: y="Friday"; }
If there is no match found then result will be "friday".
Conditional statements are used to perform different actions based on different conditions. In JavaScript we have the following conditional statements.
if statement
if...else statement
if...else if....else statement
switch statement
If Statement : We use the if statement to execute some code only if a specified condition is true. Syntax of if statement is as follow :
if (some condition) { code to be executed if condition is true }
For example
if (number<33) { x="failed"; }
If value of variable number is less then 33 then result will be "failed".
If .. else Statement : We use the if else statement to execute some code if a condition is true and another code if the condition is not true. Syntax for this is as follows :
if (some condition) { code to be executed if condition is true } else { code to be executed if condition is not true }
For example
if ( number < 33 ) { y="failed"; } else { y="passed"; }
If the value of variable number less then 33 then result will be "failed", otherwise result will be "passed".
If .. else if .. else Statement : We use the if else if else statement to select one of several blocks of code to be executed. Syntax for this is as follow
if (condition 1 here) { code to be executed if condition1 is true } else if (condition 2 here) { code to be executed if condition2 is true } else { code to be executed if neither condition1 nor condition2 is true }
For example
if (number>90) { y="Passed : Excellent Result"; } else if (number<33) { y="failed"; } else { y="passed"; }
If the value of variable number will be greater then 90 then result will be "Passed : Excellent Result". if value will be less then 33 then result will be "failed", otherwise (when value will be between 33 and 90 ) result will be "passed".