|
||||||||||||||||||||||
JS Tutorial
Part I - JS language
Part II- JS objects
Part III - Client-Side JS |
JavaScript Ch4 - Control Structures and Looping 4.1 Conditional Statements If statement Theif is defined in the following way:
if(condition){ [statements] }If the condition is true, the statements are executed.
Example
<HTML>
<head>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
var visitorInterest;
visitorInterest = "Technical Support";
document.writeln("Hello, my name is Frank Zealous!");
if(visitorInterest == "New Products"){
document.writeln("Thank-you for inquiring about our products!");
}
if(visitorInterest == "Technical Support"){
document.writeln("Technical support is now available.");
document.write("But first, let me introduce you to our ");
document.writeln("newest products!");
}
document.write("Our newest products will satisfy all of yours");
document.writeln("business needs!");
</BODY>
</head>
</HTML>
If...else statement:
if(condition){
statements
} else{
statements
}
Example
<HTML>
<head>
<BODY>
<SCRIPT type="text/javascript" language="javascript">
<!-- begin hiding from old browsers
var purchaseAmount;
purchaseAmount = 10.00;
if( purchaseAmount > 500.00){
document.writeln("Thank-you for your purchase!");
}else{
document.writeln("Thank-you, but surely there is something ");
document.writeln("else you would like to purchases.");
}
</SCRIPT>
</BODY>
</head>
</HTML>
for statement:
for([initializing_expr];[condition_expr];[loop_expr]){
statements
}
The three expressions enclosed in parentheses are optional, but if you omit one, the semi-colons 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 typcially increments or decrements the variable that is used as the counter for the loop. Example
<HTML>
<head>
<title>JavaScript Unleashed</title>
<DOBY>
<SCRIPT LANGUAGE="JavaScript">
document.writeln("Numbers 0 through 99 :<br>");
for (var i = 0; i < 100; ++i){
if(i%10 == 0){
document.writeln("<br>");
}
document.write(i + " ");
}
document.writeln("<br><br>After completing the loop, it equals : "+i);
</SCRIPT>
</DOBY>
</head>
</HTML>
for ... in statement:
for(property in object){
statements
}
property is a string literal gerated for you by JavaScript. For each loop, property is assigned the next property name contained in object until each one is used. Example
<HTML>
<HEAD>
<TITLE>JavaScript Unleased</TITLE>
</HEAD>
<BODY>
<PRE>
<script language="JavaScript">
var anObject = document;
var propertyInfo = "";
for (var propertyName in anObject){
propertyInfo = propertyName + " = " + anObject[propertyName];
document.write(propertyInfo +"<br>");
}
</script>
</PRE>
</BODY>
</HTML>
while statement:
while(condition_expr){
statements
}
The while statement acts much like a
<HTML>
<HEAD>
<TITLE>JavaScript Unleased</TITLE>
</HEAD>
<BODY>
<PRE>
<script language="JavaScript">
var i = 0;
var result = 0;
var status = true;
document.write(i);
while(status){
result = result + ++i;
document.write(" + " +i);
if(i == 10){
status = false;
}
}
document.write(" = " + result);
</script>
</PRE>
</BODY>
</HTML>
break and continue statement:
A loop doesn't stop repeating itself until
the specified condition returns
<HTML>
<HEAD>
<TITLE>JavaScript Unleashed</TITLE>
</head>
<BODY>
<script type="text/javascript">
var highestNum = 0;
var n = 175;
for ( var i = 0; i < n; ++i){
document.writeln(i);
if(n < 0){
document.write("n cannot be negative.");
break;
}
if(i * i <= n){
highestNum = i;
continue;
}
document.writeln("Finished!");
break;
}
</script>
</BODY>
</HTML>
labeled statement:
The
<HTML>
<HEAD>
<TITLE>JavaScript Unleashed</TITLE>
</head>
<BODY>
<script type="text/javascript">
var stopX = 3;
var stopY = 8;
document.writeln("All x,y pairs between (0,0) and ("+stopX+","+stopY+"):\n");
loopX:
for(var x=0; x < 10; ++x){
for(var y = 0; y < 10; ++ y){
document.write("("+x+","+y+")");
if(x == stopX && y == stopY){
break loopX;
}
}
document.writeln();
}
document.writeln("\nAfter completing the loop, x equals:"+x);
document.writeln("\nAfter completing the loop, y equals: "+y);
</script>
</BODY>
</HTML>
with statement:
The
with (object){
statements
}
Example
<HTML>
<HEAD>
<TITLE>JavaScript Unleashed</TITLE>
</head>
<BODY>
<script type="text/javascript">
with(document){
writeln("Hello");
writeln("The URL for this document is:" + URL);
writeln("Now you can avoid using the object's prefix each time!");
}
</script>
</BODY>
</HTML>
switch statement: The
<HTML>
<HEAD>
<TITLE>JavaScript Unleashed</TITLE>
</head>
<BODY>
<script type="text/javascript">
var request = "Name";
switch (request){
case "Logo":
document.write('<IMG src="logo.gif" alt="Logo" height=45 width= 45>');
document.write("<br>");
break;
case "Name":
document.write("Acadia Software Inc.");
document.write("<br>");
break;
}
</script>
</BODY>
</HTML>
|
References(1) Aland Shalloway & James R. Trott, Design Patterns Explained, Second Edition.(2) Allen Holub, Holub on Patterns, Learning Design Patterns by Looking at Code (3) Eric Evans, Domain-Driven Design, Tackling complexity in the heart of software. Advertisement |
||||||||||||||||||||
|
||||||||||||||||||||||