GP_JS_Loops_Jump_Statements
GP_JS_Loops_Jump_Statements
Loops
Loops are used to do something repeatedly. You need to print 'n' numbers, then you
can use a loop to do so. There are many different kinds of loops, but they almost do the
same thing. Their use varies depending on the type of situation, where one loop will be
easy to implement over another.
They are used to run the same code repeatedly, each time with a different value.
Types of loops
● for
● for/in
● for/off
● while
● do-while
for loop
A for loop is used to repeat something until the condition evaluates to false.
1
Initialisation statement
● The initialisation statement is optional, and you can initialise these statements
before the for loop.
Note: You need to provide a semicolon for each part of the loop, even if the options are
missing.
● You can provide more than one initialisation statement using a comma( "," ) as a
separator.
Condition
Often the condition is used to evaluate the condition of the initial variable.
This is not always the case; JavaScript doesn't care. Condition is also optional.
If the condition returns true, the loop will start over again. If it returns false, the loop will
end.
Note: If you discard the condition, a break should be given inside the loop. Otherwise,
the loop will never end. This will crash your browser.
We Will study about break statement further in this module
2
Update Statement
Example : let i = 0 ;
for ( ; i < 5; ) {
console.log(“Coding Ninjas”) ;
i++ ;
}
Output : Prints Coding Ninjas 5 times in 5 different lines
Loop scope
Using var :
var i = 5;
for (var i = 0; i < 10; i++) {
console.log(“Coding Ninjas”) ;
}
console.log(i) ; // 10
Because till 9, the condition would be true, then i increments by one and becomes 10
and exits the loop.
By using var, the variable declared in the loop redeclares the variable outside the loop.
Using let:
let i = 5;
for (let i = 0; i < 10; i++) {
console.log(“Coding Ninjas”) ;
}
console.log(i) ; // 5
By using let, the variable declared in the loop is not redeclared outside the loop, and i
used in the loop(block scope) is different from the outside variable i
3
For/in and For/of Loops
● for/in - loops through the properties of an object
● for/of - loops through the values of an iterable object
They are mainly. used on objects and arrays, hence will be covered in detail in the next
module
while loop
The while statement executes the statements until the condition is not false.
First, the condition is evaluated, and if it is true, then the statements are executed, and
the condition is tested again. The execution stops when the condition returns false.
NOTE: You have to provide an update expression inside the loop so that it does not
repeat infinitely.
Example : let i = 0 ;
while (i < 5) {
console.log(“Coding Ninjas”) ;
i++;
}
Output : Prints Coding Ninjas 5 times in 5 different lines
do while loop
The do-while loop is similar to the while loop, except that the statements are executed
at least once
Syntax : do {
// code to be executed multiple times
} while (condition);
4
Example : let i = -1 ;
do {
console.log(“Coding Ninjas”) ;
i++;
} while (i > 0);
Output : Coding Ninjas
while do while
No semicolon at the end of a while. The semicolon at the end of the while.
while(condition) while(condition);
while loop is entry controlled loop. do while loop is exit controlled loop.
5
Jump Statements
In JavaScript there are two types of jump statements :
● break
● continue
break Statement
We talked about this in the for loop when there is no condition passed then it is used to
jump out of a switch( ) statement.
The break statement can also be used to jump out of a loop.
NOTE: In the above example, there was no condition passed to the for loop, but in case
the condition is mentioned, the break can also be used to jump out of the loop at some
point
continue Statement
The continue statement breaks one iteration (in the loop) if a specified condition occurs
and continues with the next iteration.
For example, you need to print “Coding Ninjas” if the number is odd and the number
along with it from 0 to 10
6
Example : for (let i = 0; i < 10; i++) {
if ( i %2 == 0)
continue ;
console.log(“Coding Ninjas” , i ) ;
}