JS-VI Increment, Decrement & While Loops Notes - 505537
JS-VI Increment, Decrement & While Loops Notes - 505537
Javascript provides another way of incrementing and decrementing the variable i.e
++/--.
In simple terms, Postpone the operation for later instead of it first printing it.
While Loop 1
Title Increment(++) Decrement(--)
Student Task
Code 1 : Predict the output
var a = 10;
++a;
var b = 10;
b++;
console.log(a)
console.log(b);
Student Task
Code 2 : Predict the output
var a = 10;
var b = 10;
++a;
b++;
console.log(a);
console.log(b);
Loop
While Loop 2
Song Library
While Loop 3
Guests
There are 10 guests coming to my home, After 2-3 days they decided to leave their
home.
They all have the train on the same day and at the same time.
I need to drop them at the railway station but I have one bike which can only take
one person at a time.
Taking the First guest to the railway station, dropping them and arrive back and
follow the same procedure again and again till the end.
While Loop 4
While Loop
The while loop begins with a condition and it is written similar to an if statement. The
inner parenthesis is the condition.
Each time the loop is executed, the variable will change and eventually become
larger or less than the number in the condition, stopping the loop
Case 1 : Given a track of 100 m, Hari train himself for a long Marathon of 100
meter. Hari standing at 0th position and he needs to cover 100 meter distance.
Hari will make 1 meter jump at a time.
While Loop 5
Observation
Starting Point
While ( Till When )
{
How long Jump at a time?
While Loop 6
}
-------------------------------------------------------------------------
Initialization
While ( Condition )
{
Increment/Decrement
}
var position = 0
while(position <= 100)
{
position = position + 1;
}
While Loop 7
var i = 0;
while(i<=5)
{
console.log("hello");
i = i + 1;
}
While Loop 8
var position = 0;
while(true)
{
console.log("Hello Masai");
}
var position = 0;
while(position<100){
console.log("Current Position ",position);
position = position + 2;
}
Student Task
While Loop 9
var position = 0;
while(position<100){
console.log("Current Position",position);
position = position + 15;
}
Student Task
While Loop 10
var position = 100;
while(position>=0){
console.log("Current Position",position);
position = position - 1;
}
Break
Guest Analogy
There are 10 guests coming to my home, After 2-3 days they decided to leave their
home.
They all have the train on the same day and at the same time.
I need to drop them at the railway station but I have one bike which can only take
one person at a time.
While Loop 11
Taking the First guest to the railway station, dropping them and arrive back and
follow the same procedure again and again till the end.
Suppose I took the First Guest and dropped him to the Railway station and come
back.
Now, Next I took the third guest to Railway station and found that Train has gone.
var guest=0;
while(guest<=10)
{
console.log("Guest",guest);
if(guest == 3)
{
break;
}
guest++;
}
Student Task
Code 10 : Predict the output
var count=0;
while(true)
{
console.log("Hello");
count++;
++count;
if(count>5)
{
break;
}
count--;
}
While Loop 12
Continue
Guest Analogy
There are 10 guests coming to my home, After 2-3 days they decided to leave their
home.
They all have the train on the same day and at the same time.
I need to drop them at the railway station but I have one bike which can only take
one person at a time.
Taking the First guest to the railway station, dropping them and arrive back and
follow the same procedure again and again till the end.
Suppose I took the First Guest and dropped him to the Railway station and come
back.
Suppose the third guest is Sick, In that case I will skip him.
and I will continue with the fourth guest and follow the same procedure.
var guest=0;
while(guest<=10)
{
console.log("Guest",guest);
if(guest == 3)
{
continue;
}
guest++;
}
While Loop 13
**Problem** :
// Sum of 1 to 10
// 1 + 2 + 3....... + 10
var i = 1;
var sum = 0;
while(i<=10)
{
sum = sum+i;
i++;
}
console.log(sum);
While Loop 14