Programming Structure

advertisement
Programming Structure
CS 104
9/7/11
Agenda






Go over Quiz
Quiz #2 9/14
Discuss Assignment #1 due today
(11:59pm)
Discuss Assignment #2 due 9/21
(11:59pm)
Finish slides from Wednesday
Introduction to programming logic and
BYOB Scratch
2
Unstructured Code: Spaghetti Code
3
Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory
Three Basic Structures

Three basic structures in programming




Sequence structure
Decision structure
Loop structure
The design dictates which structures are
required
4
Sequence Structure


Sequence: one action occurs
after another
Most basic structure
5
Sequence Structure Example
Wake up
Turn off alarm
Eat breakfast
Brush teeth
Shower
Get dressed
Pack bookbag
Draw a square in Scratch
6
Decision Structure


Just as the name
implies, the
decision structure
requires making a
decision
A true/false
question is asked,
and the program
chooses one of two
paths depending
on whether the
answer is true or
false
7
Decision Structure

Also known as an if-statement or an ifthen-statement
if condition is true then
do processA
else
do processB

It’s possible that only one process exists
if car needs gas then
get gas
8
Decision Structure

It’s possible that only one process exists
if car needs gas then
get gas
9
Decision Statement Example
if weather = raining then
wear rain boots
else
wear sandals
end if
10
Decision Statement Example
Football score calculator
touchdown = 6
extra point = 1
field goal = 3
safety = 2
Modify square program to ask the user if he/she wants to
make another square
11
Looping



Also referred to as Iteration or Repetition
A question is asked, if the answer is true, the
specified action is performed, and the question is
asked again
Different types of loops



Do Until – do the action until the question is true
Do While – do the action while the question is true
For – repeats a specific number of times
12
Loop Structure Example
while quarter < 5
quarter = quarter + 1
end while
Loop the square drawing program
13
Three Basic Structures

Sequence of all three structures
14
Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory
Three Basic Structures

Nesting structures can be necessary

Sequence within a Decision statement
15
Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory
Why Use Structure?



Increases readability
Allows for multiple programmers to work on the
same program (modularity)
Help identify errors more quickly
16
Flowchart and Pseudocode of Structured College
Admission Program

Notice how much easier this is to read than the spaghetti
code version
17
Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory
Pseudocode for Rock, Paper, Scissors

Sequence,
Decision,
and Looping
structures
can be used
to solve
almost any
problem
18
Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory
Summary





Spaghetti code is unorganized making it
difficult to read and maintain
Three types of structures: sequence,
decision, loop
Assignment #2 due 9/21
Quiz #2 9/14
Install BYOB Scratch and start trying it out
=)
19
Download