Structured Programming Taken from notes by Dr. Neil Moore

Slides:



Advertisements
Similar presentations
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Advertisements

Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Invitation to Computer Science, Java Version, Second Edition.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
CPS120 Introduction to Computer Science Iteration (Looping)
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CPS120 Introduction to Computer Science Iteration (Looping)
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Calvin College Controlling Behavior The if, switch and for Statements.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
ITM © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Structured Programming The Basics
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 6: Loops.
Def: A control structure is a control statement and
Introduction To Repetition The for loop
Topics Introduction to Repetition Structures
Python: Control Structures
Lesson 05: Iterations Class Chat: Attendance: Participation
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Karel J Robot Chapter 6.
CS 115 Lecture 8 Structured Programming; for loops
Incrementing ITP © Ron Poet Lecture 8.
Topics Introduction to Repetition Structures
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Structured Programming; for loops Taken from notes by Dr. Neil Moore
Exceptions and files Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Control Structures - Repetition
Functions Taken from notes by Dr. Neil Moore
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Program Flow CSCE 121 J. Michael Moore.
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Exceptions and files Taken from notes by Dr. Neil Moore
CS190/295 Programming in Python for Life Sciences: Lecture 6
The University of Texas – Pan American
Coding Concepts (Basics)
Debugging Taken from notes by Dr. Neil Moore
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Debugging Taken from notes by Dr. Neil Moore
Recursion Taken from notes by Dr. Neil Moore
For loops Taken from notes by Dr. Neil Moore
The structure of programming
Topics Introduction to Repetition Structures
Chapter 4: Repetition Structures: Looping
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Loops CGS3416 Spring 2019 Lecture 7.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Chapter 4: Loops and Iteration
Presentation transcript:

Structured Programming Taken from notes by Dr. Neil Moore CS 115 Lecture Structured Programming Taken from notes by Dr. Neil Moore

The bad old days: GOTO In the early days of programming, we didn’t have for loops, if statements, etc. Instead, we had just “if this is true, go to 10”. You could use that to skip over code (like an if does) … or go back to an earlier line to make a loop This was very tedious and error prone Especially if something had to be changed “Spaghetti code”: trying to trace a program’s execution was like trying to trace one strand of spaghetti in a plate of it

Spaghetti code

Structured programming In the 1960’s, computer scientists started to think about how to write programs that were easier to understand and modify. Edsger Dijkstra, “Go To Statement Considered Harmful” (1968) They introduced the paradigm of structured programming. Patterns that lead to easier-to-understand code Easier to test and debug Easier to modify and maintain Easier to collaborate on large programs

Data structures and Control structures We’ve already seen a little about data structures The ways of organizing data in a program Simple ones: constants and variables More complex: graphics objects, strings, lists, … Control structures are ways of controlling the execution of a program which statements execute, and in which order

The three basic control structures In 1966, Böhm and Jacopini showed that any program using “go to” could be rearranged to use only three simple control structures Sequence Selection Iteration Added a fourth later: Subprograms (more in chapter 5) Each of these control structures has two important guarantees: Only one way to enter the control structure Only one way to exit the control structure One entrance, one exit.

The sequence control structure “Sequencing” or “sequential execution” just means: running one statement after another In Python we write one line after the next “The default” control structure Guarantees unique to sequence The steps will execute in the order given Steps will not be skipped It will always start at the first statement … And finish at the last statement of the sequence

Selection control structure “Selection” means choosing which code to run based on a condition or question In Python, an if-else statement Two branches, based on True and False Each branch is another control structure (most often a sequence, can be a loop or another if) Guarantees: Always starts with the question/condition Runs one branch or the other, never both … and never neither – MUST do one of the two Avoid dead code: code that is never executed – not just not executed on one particular run, but NEVER executed Usually because the condition is always False

Iteration control structure “Iteration” means running code multiple times (a loop) In structured programming, “repeat this body until a condition is false” In Python, a while loop (in about a week) for loops are a special case of iteration Guarantees: Always starts with the question/condition If the condition is True, executes the entire body, then comes back to the condition If the condition is False, leaves the loop Beware of infinite loops where the condition is always True

Subprogram control structure Sometimes we need to repeat the same code in several different places It would be nice if we didn’t have to write the code multiple times A subprogram is a chunk of the code treated as a single unit When we need to execute that code, we call (invoke) the subprogram The call runs the subprogram, waits for it to finish Then execution keeps going from where the call is Sometimes we send values to the subprogram Sometimes the subprogram sends value(s) back

Subprogram control structure In Python, subprograms are called functions Arguments are the values we send to the function And the function can return a result (or results) Can you think of Python functions that Take one or more arguments? Take no arguments? Return a result? Don’t return a result?

Subprogram control structure Guarantees for subprograms: Forces the invoking code to pause execution Starts execution at top of subprogram code Completes execution at bottom of subprogram Always returns execution control to the point where the subprogram was invoked Does NOT execute unless invoked (called)

Control structures summary Sequence (one statement after another, easy to forget its name!) Selection (conditionals: if and variations) Iteration (loops: for and while) Subprogram (functions: def and calls)