JavaScript Basics: While Loop
Hello once again my all readers. A very warm welcome to you. I hope you must be fine. Today, in this topic,we'll learn about while loop. So without wasting time, let's start it.
Lets first see the basic syntax of while loop :
while(condition)
{
// Code to be executed
}
Have you seen it before or something like that when we use conditions in (condition). Yes...! You must have seen it if you are familiar with if else statement. It is also similar to that but why do we use while loop ?
If I say you that write your name 10 times in console. It would be very easy for all of you.But there is a catch here.
If I again say you to write your name now 1000 times in console. Is it possible now ? Yes, it is still possible but this is not a feasible way to write name in console 1000 times.
This is why while loop concept comes here. In round brackets near while (condition), we provide certain conditions here. So If the conditions inside round brackets is true, then code inside curly brackets is executed.
{
// Code to be executed
}
Let's understand it with very basic example :
- WAP to print "Hello World" 10 times ?
var i=1;
while(i<=10)
{
console.log("Hello World");
i++
}
Let's understand it from very first line.
First, we have declared a variable var i=1;. Our loops should have a starting point.
After that, we have defined a condition while(i<=10). It means it should print the code until value of i<=10
If our condition is true, then it'll start printing the code inside the {.....} brackets.
Just one last thing, what if we do not write increment or decrement operator? In our example, we wrote i++. It means every time after printing console.log("Hello World"); we want to add +1 to our variable value. So it'll keep updating its value until i<=10
Now let's undetsand it with another example :
WAP to print numbers from 1-10 ?
var i=1;
while(i<=10)
{
console.log(i);
i++
}
// Output :
// 1
// 2
// 3
//..
//..
// 10
Here again as like previous example, we declared variable var i=1; because we want to start printing value from 1. Then in condition bracket (), we have defined a condition that i<=10. Value of i should not exceed from 10 i.e. 11,12,13,14....and so on.
I hope you must understnding about the while loop.
Now, let's print the table of 5.
WAP to print table of 5 ?
var i = 5;
while(i<=50)
{
console.log(i);
i = i+5;
// Output: It will print table of 5
}
Now, you must have understood why we have taken var i = 5; this time instead of i = 1 as we want to start from 5.
Similarly, in the end, we have incremented value +5 because 5,10,15....till 50.
Now can you write numbers from 10 to 1 means in reverse order?
Let's have a try :
WAP to print numbers from 10-1(in reverse order) ?
var i =10;
while (i>=1)
{
console.log(i);
i=i-1;
// Output : 10,9,8,7.......1
}
WAP to print odd numbers from 1-100 ?
var i = 1;
while(i<=100)
{
console.log(i)
i+=2;
// Output :
// 1
// 3
// 5
// 7
// 9
// ..
// 100
}
I hope these examples definitely cleared your doubts to some extent.
Subscribe to my newsletter
Read articles from Ravi Rathore directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ravi Rathore
Ravi Rathore
Hi, I am Ravi Rathore from Indore(India). I have 1.5+ year of experience in web development. Apart from these, I am a Blogger, Crypto, and Web3 Enthusiast. I love writing that's why I started this blog to share my experience, and tricks related to front-end development in the simplest way. Previously, I used to write on cryptocurrency at www.cryptoinch.com.