PLD Lesson 5 Programming
Document Details
Uploaded by RealisticMesa
Botho University
Tags
Summary
This lesson introduces control structures in programming, covering sequence, selection and repetition using examples. It also covers algorithms and pseudocode.
Full Transcript
Welcome The Plan for Today Review control structures – Sequence – Selection – Repetition Repetition structures – While – Do/While – For Repetition structure example Learning Objectives Explain the three basic types of control structures Explain how the three kinds...
Welcome The Plan for Today Review control structures – Sequence – Selection – Repetition Repetition structures – While – Do/While – For Repetition structure example Learning Objectives Explain the three basic types of control structures Explain how the three kinds of repetition structures work Apply the concept of repetition control structures to a practical problem RECAP ALGORITHM A set of step-by-step instructions to accomplish a task. An algorithm must have start instruction Each instruction must be precise. Each instruction must be unambiguous. Each instruction must be executed in finite time. An algorithm must have stop instruction. RECAP PSEUDOCODE Pseudo code is another program analysis tool that is used for planning program logic. "Pseudo" means imitation or false "Code" refers to the instructions written in a programming language. RECAP Pseudo code is made up of the following basic logic structures that have been proved to be sufficient for writing any computer program. Sequence Selection (IF...THEN...ELSE or IF....THEN) Iteration (DO...WHILE or REPEAT...UNTIL) RECAP Control Structures - Review All programs can be written in terms of three control structures (like building blocks) – Sequence ‘Built-in’ to C – Unless otherwise directed, one statement after the next is executed – Selection (three types: IF, IF-ELSE) Depending on a condition, select between one statement or another – If var1 is greater than 10, do this…, else do that… – Repetition (three types: WHILE, DO-WHILE, FOR) Depending on a condition, execute one or more statements repeatedly Selection Structure - Review Two kinds of selections structures – IF (also called, ‘single-selection’) if condition is true Perform action if condition is false, action is skipped – IF/ELSE (also called, ‘double-selection’) if condition is true Perform action else (if condition is false) Perform a different action Repetition Structure Often need to repeat an action or calculation Repetition structures are often called ‘loops’ 3 Types of Repetition Structures while do/while for while tests a condition at the beginning of the loop – condition must first be true for the loop to run even once do/while tests a condition at the end of the loop – loop will run at least once for facilitates initializing and incrementing the variable that controls the loop – Especially helpful for: Looping for a known number of times Loops that count or that need to increment a variable while Loop – Pseudocode wording WHILE condition is TRUE, repeat: Statement1 Statement2 Etc. END WHILE while Loop - Flowchart View Statement is executed while condition is true – Note that the TRUE condition statement condition must first be true in order for the FALSE statement to be executed even once while loop C syntax General form of a while loop: while(condition) statement1; – When condition becomes false (i.e. == 0), looping stops and the next statement is executed Compound statement form: while(condition) { statement1; statement2; statement3; // etc. } Distance Dropped Program Development Define the problem – Calculate and print the distance traveled each second by an object dropped from a height in a standard gravitational field beginning at t = 0 sec over the next 10 seconds – Simplify Assume v0 and d0 are both zero d 1 gt 2 Inputs – are there any? 2 Outputs – time – distance Solution Algorithm Start Flowchart Pseudocode Declare variables 1. Start 2. Declare variables: ____ Initialize variables d (distance traveled) t (time) (Are these good names?) T 1 2 3. Initialize variables t