Chapter 4: Conditional Statements
you can also use this statement if and
else to evaluate an expression. Instead of returning a value based on the
result as a conditional operator.
If
Syntax
if (condition)
{
statements
}
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
a=parseInt(prompt("Enter a number",0));
b=parseInt(prompt("Enter a second number",0));
if(a>b)
{
alert("A is greater");
}
</script>
</body>
</html>
Output
Result
The condition can be any logical
expression. If the result of condition is true, the statements enclosed
in curly braces are executed and program execution continues. If the condition
returns false , Javascript ignores the block of code between the curly braces
and continues there after.
If … Else
Sometimes using the if statement alone
is not enough.
You can also reserve a set of
statements to execute if the conditional expression returns false. This is done
by adding an else block of statements immediately following the if block.
Syntax
if(condition)
{
Statements;
}
else
{
Statements
}
Example
<!DOCTYPE
html>
<html
lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>If
statement</title>
</head>
<body>
<script>
if(confirm("Do
you wish to continue"))
{
alert("Reply
is true");
}
else{
alert("Reply
is false");
}
</script>
</body>
</html>
Output
Looping
Loops are used to perform some
operation repeatedly. Following are loops in Javascript.
For
You use for statement to start a loop
in a script.
Syntax
for([initialization_expr];[condition_expr];[loop_expr])
{
Statements
}
Example
<!DOCTYPE
html>
<html
lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>For
loop</title>
</head>
<body>
<script>
var
string1="12345678"
var
limite=string1.length
window.document.open()
window.document.write("<pre>")
for(var
i=0;i<limite;i++)
{
window.document.write(string1.charAt(i))
window.document.write("<br/>")
}
for(i=(limite-1);i>=0;i--)
{
window.document.write(string1.charAt(i))
window.document.write("<br/>")
}
window.document.write("</pre>")
</script>
</body>
</html>
Output
The three expressions enclosed in
parentheses are optional, but if you omit one, the semicolons are still
required . This keeps each expression in its appropriate place. You typically
use the initializing expression to initialize and even declare a variable to
use as a counter for the loop. Next, the condition expression must evaluate to
true before each execution of the statements enclosed in curly braces. Finally
, the loop expression typically increments or decrements the variable that is
used as the counter for the loop.
For…in
You need a basic understanding of
Javascript objects to use a for..in loop. You can use the for..in loop with any
javascript object, regardless of whether it as properties. One iteration is
executed for each property, no loops occur. The for..in loop also works with
custom objects. A variable of a custom Javascript object is considered a
property and therefore executes a loop for each one.
Syntax
for(property in object)
{
Statements;
}
Example
<!DOCTYPE
html>
<html
lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>for</title>
<script>
function
MyObject(type,height,width,x,y)
{
this.type=type
this.height=height
this.width=width
this.pos=new
position(x,y)
this.printme=printout
}
function
position(x,y)
{
this.x=x
this.y=y
}
function
printout()
{
window.document.write("<pre>")
window.document.write('type='+this.type)
document.write("<br/>")
window.document.write('height='+this.height)
document.write("<br/>")
window.document.write('width='+this.width)
document.write("<br/>")
window.document.write('x
value='+this.pos.x)
document.write("<br/>")
window.document.write('y
value='+this.pos.y)
document.write("<br/>")
window.document.write("</pre>")
}
</script>
</head>
<body>
<script>
window.document.open()
shape1=new
MyObject("Circle",10,25,24,167)
shape1.printme();
for(val
i in
shape1)
{
document.write(i+"<br>");
}
</script>
</body>
</html>
While
The statement while acts similarly to a
for loop but does not include the function of initializing or incrementing
variables in its declaration. You must declare variables beforehand and
increment or decrement the variable within the statements block.
Syntax
while (condition)
{
Statements
}
Break and Continue
break is used to terminate the loop and
continue is used to continue the loop from first.
With
You use the with statement to avoid
repeatedly specifying the object prefix when accessing properties or methods of
that object.
Syntax
with(object)
{
Statements
}

No comments:
Post a Comment