Title: CS 17700 Recitation Slides
1CS 17700Recitation Slides
2Announcements
- Reminder Exam 2 will be on Tuesday (Nov 4) from
630-730 PM - PHYS 112 for students in lab sections 0101-0701
- FRNY G140 for students in lab sections 0801-1201
- Covers Chapters 8-13
- Evening consulting hours will be from 730 to
900 PM that day
3Announcements
- Do not cheat! We DO check for suspiciously
similar files, and students who cheat on an
assignment will receive a zero on it. - Before you turn in an assignment, check to make
sure that all your code is in the file with the
required name (ex. "user_name.html"), and delete
any unneeded files in your lab/project folder.
4Questions?
5Conditional Execution
- An if statement is a control statement that
provides conditional execution - control statements control the execution of other
JavaScript statements - conditional execution lets your program react
differently (execute different code) in different
situations
6Conditional Repetition
- A while loop is a control statement that provides
conditional repetition - Conditional repetition is similar to conditional
execution, but the statements inside the loop can
be executed more than once - Many problems involve repeating a task until some
condition is met (or fails to be met)
7While Loops
- General form of a while loop
- The statements between the curly brackets are
called the loop body - As long as the boolean test evaluates to true,
the loop body statements are executed and the
test re-evaluated
8While Loop Example
- Let's say that we want to choose a random number
between 1 and 100 until we get one that is less
than 20
num1 20 num2 RandomInt(1, 100) document.writ
e(num2 "ltbr/gt") while(num2 gt num1) num2
RandomInt(1, 100) document.write(num2
"ltbr/gt") document.write("ltbr/gtThe number
"num2" is less than 20.")
9Avoiding Redundancy
- In the previous code, we gave num2 a random value
in two different places (this is redundant) - Want to avoid redundant code
- it makes code needlessly repetitive
- it can lead to errors if you try to change the
code
10Avoiding Redundancy
- To avoid redundant code
- "prime" the loop with default values
- make sure that your default values dont make
your code work incorrectly! - make sure your default values will cause the loop
body to be executed at least once - define a boolean "flag" that is used in the
boolean test instead of other variables
11Avoiding Redundancy Examples
- Priming the loop
- Defining a flag
num1 20 num2 50 //50 is greater than 20,
so loop will run gt1 times while(num2 gt num1)
num2 RandomInt(1, 100) alert("The
number " num2 " is less than 20.")
num1 20 done false while(done false)
num2 RandomInt(1, 100) if(num2 lt num1)
done true //change the flag so the loop
will stop alert("The number " num2 "
is less than 20.")
12Loop Tests
- How humans think "do x until y happens"
- ex. choose a random number until you get a number
less than 20 - How while loops work "do x while z happens"
- ex. choose a new random number while the current
one is greater than or equal to 20 - The loop test will check for z, which is usually
the opposite of y - ex. while (rand_num gt 20)
13Counter-Driven Loops
- A while loop can also be used to repeat a task a
certain number of times - The boolean test is based on a counter variable,
which is changed (usually incremented by one) in
the loop body - General form
14Counter-Driven Loops Example
- num_kittens parseFloat(prompt("How many kittens
do you want?")) - num_ducks parseFloat(prompt("How many ducks do
you want?")) - count 0
- while( count lt num_kittens )
- document.write("ltimg src'kitten.jpg' /gt")
- count count 1
-
- count 0
- while( count lt num_ducks )
- document.write("ltimg src'daffy.jpg' /gt")
- count count 1
15Counter-Driven Loops
- Note the counter doesn't have to start at zero,
nor do you have to add or subtract one to it each
time - Other counter-driven loops
- start at 1000, divide by 3 each time
- start at 1, add 2 each time
- etc.
16Infinite Loops
- If the boolean test always evaluates to true, the
while loop will run forever - Infinite loops are (usually) bad
- in JavaScript, they can freeze your browser
- in other programming languages, they can freeze
your whole computer - To prevent infinite loops, make sure some part of
the loop test changes inside the loop
17Computer Science
- Computer Science is the study of computation and
problem solving - Major themes in CS
- Theory - design and mathematical analysis of
algorithms, understanding the capabilities and
limitations of computers - Software design, development, testing and
maintenance of software - Hardware - development of devices to execute
programs
18Encryption Algorithms
- Private Key Encryption
- each pair of users agrees on a private key
- a message is encrypted with the private key by
the sender (A), and decrypted with the same
private key by the receiver (B) - problem how do A B securely agree on a private
key?
19Encryption Algorithms
- Public Key Encryption
- Each user has a public and private key, and gives
their public key out to other users - A encrypts with B's public key, and B decrypts
with their private key - only B can read the message, but can't be sure
that it came from A - even better A encrypts with A's private and B's
public keys, and B decrypts with B's private and
A's public keys - only B can read the message, and it definitely
came from A
20Software Engineering
- Software engineering is "industrial strength"
programming (programming on a large scale) - Involves teams of programmers and stages of
development - Coordination among programmers/teams and rigorous
code testing are essential to successful projects
21Software Engineering
- Stages in the Software Life Cycle
- Requirement Analysis and Specification Figure
out what the customer needs and write it down
with lots of detail - Design Design the software system conceptually
(break it down into distinct pieces and figure
out which algorithms to use) - Implementation Write code. Teams may work
independently and integrate their modules to
achieve the finished program - Testing The code is tested both as independent
modules (unit testing) and as a whole. Redesign
and reimplementation are done as needed to fix
problems. - Operation and Maintenance The system must be
installed and maintained over time (updates, etc).
22Artificial Intelligence
- AI attempts to create programs that exhibit
human-like characteristics (the ability to
reason, think, etc) - Expert Systems programs that encapsulate expert
knowledge in a specific domain - ex. medical expert systems capture the knowledge
of a number of doctors, and can be used to
diagnose illnesses as if the patient were being
treated by all those doctors in person - Neural Computing design of programs that mimic
the biological brain
23Questions?