JAVASCRIPT-2
_________
Loops :
Loops are handy, if you want to run the same code over and over again, each time with a different value.
- syntax is the set of rules, how JavaScript programs are constructed:
1- Foor loop
2- While loop
Foor loop
The syntax of for loop is
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
for (inti ; condetion; update)
{
Statements
}
-
Statement 1 is executed (one time) before the execution of the code block.
-
Statement 2 defines the condition for executing the code block.
-
Statement 3 is executed (every time) after the code block has been executed.
Example :
______ # The While Loop The while loop loops through a block of code as long as a specified condition is true.
Syntax
Init ;
while (condition) {
// code block to be executed(Update)
update
}
Example :
. ____ # Comparing For and While BASIS FOR COMPARISON |FOR | WHILE ————————|——|—– Declaration | for(initialization; condition; iteration){//body of ‘for’ loop} |while ( condition) {statements; //body of loop} Format | Initialization, condition checking, iteration statement are written at the top of the loop. | Only initialization and condition checking is done at the top of the loop. Use | The ‘for’ loop used only when we already knew the number of iterations. |The ‘while’ loop used only when the number of iteration are not exactly known. Condition | If the condition is not put up in ‘for’ loop, then loop iterates infinite times.| If the condition is not put up in ‘while’ loop, it provides compilation error. Initialization | In ‘for’ loop the initialization once done is never repeated.| In while loop if initialization is done during condition checking, then initialization is done each time the loop iterate.
## JavaScript Comparison Operators Operator | Descriptio ———|———— == | equal to === | equal value and equal type != | not equal !== | not equal value or not equal type “>” | greater than < | less than “>=” | greater than or equal to <= | less than or equal to ? | ternary operator