Certify and Increase Opportunity.
Be
Govt. Certified DHTML & Javascript Developer

 

Conditionals and loop statements

Conditional Statements

Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.

In JavaScript we have three conditional statements:

if statement – use this statement if you want to execute a set of code when a condition is true
if…else statement – use this statement if you want to select one of two sets of lines to execute
switch statement – use this statement if you want to select one of many sets of lines to execute

If and If…else Statement

You should use the if statement if you want to execute some code if a condition is true.
Syntax

if (condition)
{
code to be executed if condition is true
}

Example

//If the time on your browser is less than 10,
//you will get a “Good morning” greeting.

<script type=”text/javascript”>
var d=new Date()
var time=d.getHours()

if (time<10)
{
document.write(“<b>Good morning</b>”)
}
</script>

Notice that there is no ..else.. in this syntax. You just tell the code to execute some code if the condition is true.

Switch Statement

You should use the Switch statement if you want to select one of many blocks of code to be executed.
Syntax

switch (expression)
{
case label1:
code to be executed if expression = label1
break
case label2:
code to be executed if expression = label2
break
default:
code to be executed
if expression is different
from both label1 and label2
}

This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.

Conditional Operator

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax

variablename=(condition)?value1:value2

Example

greeting=(visitor==”PRES”)?”Dear President “:”Dear ”

If the variable visitor is equal to PRES, then put the string “Dear President ” in the variable named greeting. If the variable visitor is not equal to PRES, then put the string “Dear ” into the variable named greeting.

Looping

Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to do this.

In JavaScript we have the following looping statements:

while – loops through a block of code while a condition is true
do…while – loops through a block of code once, and then repeats the loop while a condition is true
for – run statements a specified number of times

while

The while statement will execute a block of code while a condition is true..

while (condition)
{
code to be executed
}

do…while

The do…while statement will execute a block of code once, and then it will repeat the loop while a condition is true

do
{
code to be executed
}
while (condition)

for

The for statement will execute a block of code a specified number of times

for (initialization; condition; increment)
{
code to be executed
}

Examples

For loop
How to write a For loop. Use a For loop to run the same block of code a specified number of times
<html>
<body>
<script type=”text/javascript”>
for (i=0; i<=5; i++)
{
document.write(“<b>The number is ” + i + “</b>”)
document.write(“<br>”)
}
</script>
<p>Explanation:
<p>The for loop sets <b>i</b> equal to 0.
<p>As long as <b>i</b> is less than or equal to 5, the loop will continue to run.
<p><b>i</b> will increase by 1 each time the loop runs.
</body>
</html>

 

IT Professionals, Web Developers, Web Programmers, IT students can Apply for the certification course to move ahead in their careers.

 

Angular 4 Tutorial IndexBack to Angular 4 Tutorial Main Page