Programming Structure
description
Transcript of Programming Structure
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
Sequence Structure ExampleWake upTurn off alarmEat breakfastBrush teethShowerGet dressedPack 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 thendo processA
else do processB
It’s possible that only one process existsif car needs gas then
get gas
8
Decision Structure
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 < 5quarter = 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