+ All Categories
Home > Documents > Programming Structure CS 104 9/7/11. 2 Agenda Go over Quiz Quiz #2 9/14 Discuss Assignment #1 due...

Programming Structure CS 104 9/7/11. 2 Agenda Go over Quiz Quiz #2 9/14 Discuss Assignment #1 due...

Date post: 14-Dec-2015
Category:
Upload: jane-may
View: 221 times
Download: 0 times
Share this document with a friend
Popular Tags:
19
Programming Structure CS 104 9/7/11
Transcript

Programming Structure

CS 104

9/7/11

2

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

3

Unstructured Code: Spaghetti Code

Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory

4

Three Basic Structures Three basic structures in programming

Sequence structure Decision structure Loop structure

The design dictates which structures are required

5

Sequence Structure

Sequence: one action occurs after another

Most basic structure

Sequence Structure ExampleWake up

Turn off alarm

Eat breakfast

Brush teeth

Shower

Get dressed

Pack bookbag

6

Draw a square in Scratch

7

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

Also known as an if-statement or an if-then-statement

if condition is true then

do processA

else

do processB

It’s possible that only one process existsif car needs gas then

get gas

8

Decision Structure

It’s possible that only one process existsif car needs gas then

get gas

9

Decision Structure

Decision Statement Example

if weather = raining then

wear rain boots

else

wear sandals

end if

10

Decision Statement Example

11

Football score calculatortouchdown = 6extra point = 1field goal = 3safety = 2

Modify square program to ask the user if he/she wants to make another square

12

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

Loop Structure Example

while quarter < 5

quarter = quarter + 1

end while

Loop the square drawing program

13

14

Three Basic Structures Sequence of all three structures

Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory

15

Three Basic Structures Nesting structures can be necessary

Sequence within a Decision statement

Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory

16

Why Use Structure? Increases readability Allows for multiple programmers to work on the

same program (modularity) Help identify errors more quickly

17

Flowchart and Pseudocode of Structured College Admission Program

Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory

Notice how much easier this is to read than the spaghetti code version

18

Pseudocode for Rock, Paper, Scissors

Sequence, Decision, and Looping structures can be used to solve almost any problem

Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory

19

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

=)


Recommended