Lecture 17
JavaScript Control Structures
Sequence
Selection
if
if/else
switch
Repetition
while
for
do/while
while ("pre-test") Loops
do/while ("post-test") Loops
for
("counted")
Loops
Terminology: Each of these loops contains a loop body. It is the loop body that will be executed over and over again.
Each loop will be executed a certain number of times that is governed by a loop control variable (lcv).
Each loop has a test for termination.
Any repetition carried out by one of these methods can be modified to be done by the other two methods.
while Loops
The while loop has the form:
while ( some boolean condition)
{ body of loop }
The body of the loop is executed as long as the Boolean condition is true.
When Boolean becomes false, the loop terminates and execution continues with the code following the loop.
The test for termination occurs "at the top" of the loop, before the body is executed. It is possible that the loop may not be executed at all.
You must update the lcv yourself.
do/while Loops
The form of the do/while loop is:
do
{ body of loop }
while (some Boolean condition)
Test for termination is "at the bottom."
The loop body will be executed at least once.
The loop body will be performed as long as the Boolean condition is true.
You must update the lcv
yourself.
Example:
N = 1;
do
document.writeln("I Love JavaScript.");
N = N + 1;
while (N <= 100)
Application of a do/while Loop
<HTML>
<HEAD>
<TITLE>User Friendly Input</TITLE>
<! ForceUpperCase.html
Forces User to Enter UpperCase Letters -->
<SCRIPT LANGUAGE = "JavaScript">
var valid, Character ;
valid = false;
do
{
Character = window.prompt("Enter UPPERCASE Letter", "");
document.writeln("<H2>" + Character);
if ( (Character >= 'A') && (Character <= 'Z') ) valid = true;
}
while (valid == false);
</SCRIPT>
</HEAD>
<BODY BGCOLOR = "beige">
</BODY>
</HTML>
easiest to use:
test for termination and
updating of loop control variable
are automatically handled
Basic form of for Loop:
for ( lcv initialization; loop continuation condition; lcv update)
{ body of loop }
Example:
var i;
for ( i = 1; i <= 100; i = i+1)
document.writeln("Get
Rid of SUV's Now!");
Example: Find sum of first 200 positive integers:
var i, sum;
sum = 0;
for (i = 1; i <= 200; i = i + 1)
sum = sum + i;
document.writeln(sum);
Example: Find sum of all the even numbers from 2 to 200:
var i, sum;
sum = 0;
for (i = 2; i <= 200; i = i + 2)
sum = sum + i;
document.writeln(sum);
Application of a for Loop
How Much will it Cost to Attend Midd?
<HTML>
<! Homer Simpson March 23, 2001
HomerSimpson/Middfee.html
This Program Projects The Middlebury College Comprehensive Fee Forward 30 years assuming a constant percentage increase -->
<HEAD>
<TITLE>Middlebury's Projected Comprehensive Fee</TITLE>
<SCRIPT LANGUAGE =
"JavaScript">
Fee = 32765;
GrowthRate = 0.047;
for (Year = 2000; Year <= 2030; Year = Year+1)
{
document.writeln("<H2>"+Year + ": $" + Fee + "<BR>");
Fee = Fee + Fee*GrowthRate;
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR = "white">
</BODY>
</HTML>
MiddFee.html
<HTML>
<HEAD>
<TITLE>Middlebury's Projected Comprehensive Fee</TITLE>
<! Homer Simpson March 23, 2001
HomerSimpson/MiddFee2.html
An improvement over MiddFee
Rounds Fee to nearest dollar ->
<SCRIPT LANGUAGE =
"JavaScript">
Fee = 32765;
GrowthRate = 0.047;
document.writeln("<H1>Middlebury's Projected Fee</H1>");
for (Year = 2000; Year <= 2030; Year = Year+1)
{
document.writeln("<H2>"+Year + ": $" + Math.round(Fee) + "<BR>");
Fee = Fee + Fee*GrowthRate;
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR = "white">
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Middlebury's Projected Comprehensive Fee</TITLE>
<! Homer Simpson March 23, 2001
HomerSimpson/MiddFee3.html
Uses a table to display projected Comprehensive Fee ->
<SCRIPT LANGUAGE = "JavaScript">
Fee = 32765;
GrowthRate = 0.047;
document.writeln("<H1>Middlebury's Projected Fee </H1>");
document.writeln( "<TABLE BORDER = '1' >");
document.writeln( "<TR><TD WIDTH = '100'>Year</TD>" );
document.writeln( "<TD><B>Comprehensive
Fee</B></TD>" );
for (Year = 2000; Year <= 2030; Year = Year+1)
{ document.writeln("<TR><TD>" +Year + "</TD><TD>"+ "$" + Math.round(Fee) + "</TD></TR>" );
Fee = Fee + Fee*GrowthRate;
}
document.writeln( "</TABLE>");
</SCRIPT>
</HEAD>
<BODY BGCOLOR = "linen">
</BODY>
Selection
if
if/else
switch
<HTML>
<HEAD>
<TITLE>Pick a College </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
where = window.prompt("Where do you want to go to College? Enter Middlebury, Carleton,Williams, Amherst or Dartmouth","");
switch( where) {
case "Middlebury" :
alert("Great Choice!!!");
window.location = "http://www.middlebury.edu";
break;
case "Carleton":
alert("Not bad, but very far away");
window.location = "http://www.carleton.edu";
break;
case "Williams":
alert("Boo, Hiss");
window.location = "http://www.williams.edu";
break;
case "Amherst":
alert("Are you kidding? Too preppie");
window.location = "http://www.amherst.edu";
break;
case "Dartmouth":
alert("Party School");
window.location = "http://www.dartmouth.edu";
break;
default:
document.writeln("Not available");
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR = "white">
</BODY>
</HTML>
1) Break Text into 2 character blocks
2) Change each block into a 4 digit number using ASCII Values
3) Add 7 (mod 10) to each digit
4) Swap first digit with fourth digit
5) Swap second digit with third digit
6) Transmit new 4 digit number
Example
text: CX 103
2 character blocks: CX_1 03
Change to 4 digits: 6788 3249 4851
Add 7 (mod 10) to each digit
6788 3249 4851
7777 77777777
3455 0916 1528
Swap digits
5543 6190 8251
Send message as 554361908251
What You Can Do On Your Spring Break