Title: ISP 121
1ISP 121
- Algorithms
- and
- Computer Programming
2Why Know Simple Programming?
- You can create powerful macros in Access / Excel
/ Word / ??? to manipulate data - You can create interactive web pages, such as the
DePaul QRC mapping tool - You can write simple programs to analyze data and
write the next article exposing cheaters in your
school district
3What is an Algorithm?
- Computers require precise instructions to perform
an operation - The instructions cannot be ambiguous
- An algorithm is an ordered sequence of
instructions that is guaranteed to solve a
specific problem
4What is an Algorithm?
- Algorithms consist of three basic types of
operations or instructions - Sequential operations, e.g. Add 1 cup of butter
Subtract the amount of the check from the current
account balance Set the value of x to 1 - Conditional operations, e.g. If the mixture is
too dry, then add ½ cup water If the current
account balance lt 0, then account overdrawn
5What is an Algorithm?
- Algorithms consist of three basic types of
operations or instructions - Iterative operations, e.g. Repeat the previous
two steps until the mixture has thickened Repeat
the following five steps until there are no more
checks to be processed Repeat steps 1,2 and 3
until the value of y is equal to 1
6Example
- Add two values
- 32542
- 42892
How would you describe this operation to someone
who has never seen carry arithmetic?
7Average Miles Per Gallon (1)
- Step 1 Get values for gallons used, starting
mileage, ending mileage - Step 2 Set the value of distance driven to
(ending mileage starting mileage) - Step 3 Set the value of average miles per gallon
to (distanced driven / gallons used) - Step 4 Print the value of average miles per
gallon - Step 5 Stop
8Average Miles Per Gallon(2)
- Step 1 Get values for gallons used, starting
mileage, ending mileage - Step 2 Set the value of distance driven to
(ending mileage starting mileage) - Step 3 Set the value of average miles per gallon
to (distanced driven / gallons used) - Step 4 Print the value of average miles per
gallon (more on next slide)
9Average Miles Per Gallon(2)
- Step 5 If average miles per gallon is greater
than 25.0 then - Print the message You are getting good gas
mileage. - Else
- Print the message You are NOT getting good gas
mileage. - Step 6 Stop
10Average Miles Per Gallon (3)
- Step 1 Repeat step 2 to step 8 until Response is
No - Step 2 Get values for gallons used, starting
mileage, ending mileage - Step 3 Set the value of distance driven to
(ending mileage starting mileage) - Step 4 Set the value of average miles per gallon
to (distanced driven / gallons used) - Step 5 Print the value of average miles per
gallon
11Average Miles Per Gallon (3)
- Step 6 If average miles per gallon is greater
than 25.0 then - Print the message You are getting good gas
mileage. - Else
- Print the message You are NOT getting good gas
mileage. - Step 7 Print the message Do you want to do this
again, Yes or No? - Step 8 Get a value for Response from the user
- Step 9 Stop
12HTML
- Hyper-Text Markup Language
- All web pages are made (to varying degrees) from
HTML - Each HTML command tells the web browser what to
do next, such as start a new paragraph, display
an image, or insert a link
13ltHTMLgt Begins every HTML document ltHEADgt Beg
ins the head section ltTITLEgtDePaul
University lt/TITLEgt Title that appears on
browser title bar lt/HEADgt Ends the head
section ltBODYgt Begins the body section This is
the first line.ltBRgt Text followed by line
Break ltPgtStart a new paragraph.lt/Pgt Begins a new
paragraph ltH1gtFirst Headinglt/H1gt Level 1 heading
(biggest) ltH2gtA second level headinglt/H2gt Level 2
heading (little smaller) ltHRgt Inserts a
horizontal rule (straight line) ltBgtThis line is
bold.lt/BgtltBRgt Bold text ltIgtThis line is
italicizedlt/IgtltBRgt Italicized text ltIMG
SRC\images\banner.gifgt Insert an image
here ltA HREFhttp//www.cs.depaul.edugt
DePaul CS Pagelt/Agt Link to another web
page lt/BODYgt Close the body section lt/HTMLgt E
nds every HTML document
14JavaScript
- JavaScript can be added to an HTML document to
provide dynamic features - JavaScript can be added to the head or body of
HTML code (but we will concentrate on examples in
body), can assign values to variables, can
perform decision statements, and can perform loop
statements
15Example - JavaScript in Body
lthtmlgt ltheadgt lt/headgt ltbodygt ltscript
type"text/javascript"gt document.write("This
message is written when the page
loads") lt/scriptgt lt/bodygt lt/htmlgt
16Example - JavaScript in Body
lthtmlgt ltheadgt lt/headgt ltbodygt ltscript
type"text/javascript"gt alert("This message is
written inside an Alert box") lt/scriptgt lt/bodygt lt
/htmlgt
17Example - Using a Variable
lthtmlgt ltbodygt ltscript type"text/javascript"gt var
name name prompt(Please enter a
name) document.write(name) lt/scriptgt lt/bodygt lt/h
tmlgt
18Example - Using an If
lthtmlgt ltbodygt ltscript type"text/javascript"gt var
d new Date() var time d.getHours() if (time
lt 12) document.write("ltbgtGood
morninglt/bgt") lt/scriptgt ltpgtThis example
demonstrates the If statement.lt/pgt ltpgtIf the
time on your browser is less than 12, you will
get a "Good morning" greeting.lt/pgt lt/bodygt lt/htmlgt
19Example - Using an IfElse
lthtmlgt ltbodygt ltscript type"text/javascript"gt var
d new Date() var time d.getHours() if (time lt
12) document.write("ltbgtGood
morninglt/bgt") else document.write("ltbgtGoo
d afternoonlt/bgt") lt/scriptgt ltpgtThis example
demonstrates the If...Else statement.lt/pgt ltpgtIf
the time on your browser is less than 12, you
will get a "Good morning" greeting. Otherwise you
will get a "Good day" greeting.lt/pgt lt/bodygt lt/html
gt
20Logical Operators
- You can use the following logical operators in IF
and WHILE statements -
- !
- gt
- gt
- lt
- lt
21Example - Using a While to Count
lthtmlgt ltbodygt ltscript type"text/javascript"gt var
i 0 while (i lt 5) document.write("The
number is " i) document.write("ltbrgt")
i lt/scriptgt ltpgtExplanationlt/pgt ltpgtltbgtilt/bgt
equal to 0.lt/pgt ltpgtWhile ltbgtilt/bgt is less than ,
or equal to, 5, the loop will continue to
run.lt/pgt ltpgtltbgtilt/bgt will increase by 1 each time
the loop runs.lt/pgt lt/bodygt lt/htmlgt
22Example - Using a While to Prompt
lthtmlgt ltbodygt ltscript type"text/javascript"gt var
answer answer prompt(Do you understand quantum
physics?) while (answer no) document.write
(It is actually based on quantum
things.") answer prompt(Now do you understand
quantum physics?) document.write(Congrats!) lt
/scriptgt lt/bodygt lt/htmlgt