190 likes | 331 Views
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. Unstructured Code: Spaghetti Code.
E N D
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
Unstructured Code: Spaghetti Code 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
Sequence Structure • Sequence: one action occurs after another • Most basic structure
Sequence Structure Example Wake up Turn off alarm Eat breakfast Brush teeth Shower Get dressed Pack bookbag Draw a square in Scratch
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
Decision Structure • 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 exists if car needs gas then get gas
Decision Structure • It’s possible that only one process exists if car needs gas then get gas
Decision Statement Example if weather = raining then wear rain boots else wear sandals end if
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
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
Three Basic Structures • Sequence of all three structures Ferrell, Joyce. Programming Logic and Design, Third Edition Introductory
Three Basic Structures • Nesting structures can be necessary • Sequence within a Decision statement 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
Flowchart and Pseudocode of Structured College Admission Program • Notice how much easier this is to read than the spaghetti code version 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 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 =)