We will look into how to implement various loop and if else statement in JavaScript
Looping and branching
in JavaScript
JavaScript is client side scripting language. Basically it
is very light weight programming language which runs on client browser. As it
is programming language it also supports few criteria to be a programming
language like looping branching statement etc.
In this article we will see how to implement looping and
branching in JavaScript. If you are familiar with looping and branching in
other programming language then to understand this concept in JavaScript is a piece
of cake for you.
Looping : As we
know , loop is useful to do same operation again and again . Basically there
are three popular loops are available in JavaScript. They are for, while and
do-while.
Branching : Branching is very essential to execute program with respect to certain
condition. Here we will see how to use if-else keyword to implement program
flow.
Let’s start with Looping at first
For Loop:
In the code below, for loop to go through all elements of
array is implemented. We can use length property of JavaScript array to detect number of
elements in array.
<html>
<head>
</head>
<body>
<script>
var person=new Array();
person[0]="sourav";
person[1]="Sathis";
person[2]="Nontey";
for(var i = 0 ;i< person.length ;i++)
{
document.write("Person " + i + " : "+ person[i] + "<br>");
}
</script>
</body>
</html>


While Loop:
In case of while loop there is only one condition within
while() statement.
<html>
<head>
</head>
<body>
<script>
var person=new Array();
person[0]="sourav";
person[1]="Sathis";
person[2]="Nontey";
var i =0;
document.write("Using While loop" + "<br>");
while(i < person.length)
{
document.write("Person " + i + " : "+ person[i] + "<br>");
i++;
}
</script>
</body>
</html>

Do-While Loop
Do while is very similar with While loop but do- while is
post checking loop. So, even if your condition is false loop will execute at least
one time.
<html>
<head>
</head>
<body>
<script>
var person=new Array();
person[0]="sourav";
person[1]="Sathis";
person[2]="Nontey";
var i =0;
document.write("Using Do-While loop" + "<br>");
do
{
document.write("Person " + i + " : "+ person[i] + "<br>");
i++;
}while(i< person.length);
</script>
</body>
</html>

Branching:
Branching is common feature in all programming language.
Using branch statement we can control program flow in code. Generally we can
control program flow using if, else condition.
In case of more than one condition we may use ladder of if else or
nested if else depending on situation.
Here we will see one simple example of if else in JavaScript
and will try to understand implementation.
<html>
<head>
</head>
<body>
<script>
var x="";
var time=new Date().getHours();
if (time<20)
{
x="Have Good day";
}
else
{
x="Have Good evening";
}
document.write(x);
</script>
</body>
</html>
In this example at first we are taking current hours using
date function in JavaScript and then depending on hour we are showing message
to user with the help of if –else statement.
