CX 103: Introduction to Computers

Lecture 16
 
 

Web Pages Due Friday

Print out First Page;

Include URL on First Page

Sample


JavaScript Control Structures

Sequence

Selection

if

if/else

switch

Repetition

while

for

do/while


Comments on Homework Assignment 14

I. sum = First + Second + Third;

average = (First + Second + Third)/3;

vs.

sum = First + Second + Third;

average = sum/3;
 

Begin to think about computational efficiency.


II. if ( First > Second) { if (First > Third) largest = First ; }

if ( Second > First) { if (Second > Third) largest = Second;}

if ( Third > First) { if (Third > Second ) largest = Third ; }
 
 




Some Issues to Consider

Can you generalize your algorithm to handle larger collections?

Does your algorithm work in extreme cases? What are the extreme cases?


Notes on Today's Homework

Top-Down Design and Stepwise Refinement
 
 

Design 1:

Collect a bunch of letters

Determine how many vowels and how many consonants.

Design 2:

a) Get a letter

b) Determine whether it is a vowel.

c) If a vowel, increase number of vowels seen by 1.

d) If a consonant, increase number of consonants by 1.

Repeat steps (a) - (d) until done


Alternative Design 2:

a) Get a letter

b) Determine whether it is a vowel.

c) If a vowel, increase number of vowels seen by 1.

d) Increase number of letters seen by 1

Repeat steps (a) - (d) until done

Consonants = Letters seen - Vowels seen


Design 3:

While (Letter is not *)

{ Get Letter

Increment Letters seen by 1

If letter is vowel then increment Vowels }

Consonants = Letters - Vowels


Design 4:

Initialize Vowels and Letters to be 0

While (Letter is not *)

{ Get Letter

Increment Letters seen by 1

If letter is vowel then increment Vowels }

Consonants = Letters - Vowels


Design 5:

Vowels = 0; Letters =0;

Get Letter;

While (Letter is not *)

{ Increment Letters by 1;

If letter is vowel, increment Vowels by 1;

Get Letter; }

Consonants = Letters - Vowels;


Re-examine Body of Loop:

Testing for a Vowel

while( Number < 6) {

A = window.prompt("Enter an uppercase letter", " ");

if (A == "A") Vowels = Vowels + 1;

if (A == "E") Vowels = Vowels + 1;

if (A == "I") Vowels = Vowels + 1;

if (A == "O") Vowels = Vowels + 1;

if (A == "U") Vowels = Vowels + 1;

document.writeln(A);

Number = Number + 1 ; }

Is there any way to simplify further?


Compound Boolean Expressions

if ( Combined SAT > 1400 AND GPA > 3.9) Admit

if ( Combined SAT > 1400 OR GPA > 3.9) Admit

In JavaScript:

if ( (Combined_SAT > 1400) && (GPA > 3.9) ) Admit;

if ( (Combined_SAT > 1400) || (GPA > 3.9) )Admit;



 

What if we wanted to admit anyone who met at least one of these qualifications:

SAT > 1400

GPA > 3.9

Olympic Team Member

are met?

if ( (Combined_SAT > 1400) || (GPA > 3.9) || (OlympicTeamMember) )Admit;
 



<HTML>

<!- Homer Simpson March 21, 2001

HomerSimpson/VowelCounter3.html -->

<HEAD>

<TITLE>Assignment 14: Program 2</TITLE>

<SCRIPT LANGUAGE = "JavaScript">

/* Programs gets six letterss

from the user and then counts are reports

the number of vowels (A,E,I,O,U) */

var Letter, Vowels, Consonants, Number;

Vowels = 0; // initialize counter

Number = 0;

document.writeln("<H1>");

document.writeln("The letters you submitted were ")

while( Number < 6) {

Letter = window.prompt("Enter an uppercase letter", " ");

if ( (Letter == "A") || (Letter == "E") || (Letter == "I") || (Letter == "O") || (Letter == "U") )

Vowels = Vowels + 1;

document.writeln(Letter);

Number = Number + 1 ; }

document.writeln("<BR>");

Consonants = 6 - Vowels;

document.writeln("There were " + Vowels + " vowels and " + Consonants + " consonants");

document.writeln("</H1");

</SCRIPT></HEAD>

<BODY> </BODY>

</HTML>


Boolean Algebra


   
AND
OR
NOT
P
Q
P && Q P|| Q ! P
true true true true false
true false false true false
false true false true true
false false false false true


JavaScript Control Structures

Sequence

Selection

if

if/else

switch
 
 
Repetition

while

for

do/while



 
 

Repetition with Loops

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.
 
 

for Loops

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++)

document.writeln("It's time for Spring Break!");


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);
 
 
 
 


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)