Code Clichés and Conventions Lecture 10: Supporting Material Dr Kathryn Merrick Thursday 2 nd April, 2009.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Objectives In this chapter, you will learn about:
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Program Design and Development
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Lecture Notes 1/21/04 Program Design & Intro to Algorithms.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
Fundamentals of Python: From First Programs Through Data Structures
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Fundamentals of Python: First Programs
Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.
Simple Program Design Third Edition A Step-by-Step Approach
Chapter 2 - Algorithms and Design
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Flow of Control: Loops Lecture 9: Supporting Material Dr Kathryn Merrick Tuesday 31 st March, 2009.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Flow of Control Part 1: Selection
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
C Lecture Notes 1 Structured Program Development.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Graphical Programming Languages Lecture 19: Supporting Material Dr Kathryn Merrick Thursday 21 st May, 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Programming with Spread Sheets Lecture 18: Supporting Material Dr Kathryn Merrick Tuesday 19 th May, 2009.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
COMP Loop Statements Yi Hong May 21, 2015.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Repetition Structures Chapter 9
7 - Programming 7P, Q, R - Testing.
Introduction to Programming
Algorithm and Ambiguity
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structure Senior Lecturer
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to pseudocode
Structured Program
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 3 - Structured Program Development
Structured Programming Taken from notes by Dr. Neil Moore
Coding Concepts (Basics)
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 3 - Structured Program Development
Problem Solving Skill Area 305.1
Algorithm and Ambiguity
Let’s all Repeat Together
CISC101 Reminders All assignments are now posted.
Using Decision Structures
Basic Concepts of Algorithm
Presentation transcript:

Code Clichés and Conventions Lecture 10: Supporting Material Dr Kathryn Merrick Thursday 2 nd April, 2009

Overview What is a code cliché? Common code clichés: Traversal Error checking Flag guarded loops Sentinel guarded loops Enumeration and case Useful conventions

Code Clichés In English a cliché is a phrase that is used a lot (or even overused!) In computer programming, a code cliché is a sequence of instructions that are used together a lot to achieve some goal

Problem You have a freshly cooked bowl of soup in your favourite flavour… You’d like to calculate the soup’s temperature at every second over a specified period of time.

From Problem to Program… 1.Understand the problem ‘in English’ 2.Develop a rough solution on paper 3.Implement the solution in MATLAB 4.Test your solution

Newton’s Law of Cooling You know that: T t = T a + (T 0 – T a )e -kt Where: T 0 is the initial temperature of the soup T t is the temperature at time t T a is the ambient temperature of the room k = for soup Source:

Traversal A cliché to process every value in a list of values General Syntax: for val = start:end code to process value end

Demo 1: Traversal Soup Cooling…

Error Checking Use an if statement to check error conditions Use a different branch for each type of error Use the built-in error function to print error message Else branch runs the program if no errors were detected

Demo 2: Error Checking Better soup…

Another Problem You have a freshly cooked bowl of soup in your favourite flavour… You’d like to calculate the soup’s temperature at every second until it reaches an edible 60 degrees.

Flag Guarded Loops Used to permit repeated processing of an unknown number of inputs A Boolean ‘flag’ locks execution in a loop When value of flag changes execution can exit the loop Combines while+if statements

Flag Guarded Loops: Syntax finished =... % initialise the flag while ~finished do something if desired event has happened finished = true; end This code cliché is useful when the loop exit condition is very complex.

Demo 3: A flag guarded loop Yet more soup…

Sentinel Guarded Loops Used to permit repeated processing of an unknown number of inputs A special value indicates end-of-data Special value should not be a feasible input for processing Frequently used with user input

Sentinel Guarded Loops: Syntax sentinel = special value; val = initial value while val ~= sentinel code to process value val = code to get next value end

Demo 4: A sentinel guarded loop Ctrl-C

Enumeration and Case Avoid ‘hard coding’ values into a case statement Comparing strings is a relatively slow operation Instead, enumerate your values as constants at the start of your function Put the enumerated values into the case statements

Enumerated Case Statement: Syntax DON’T  switch service case 1 do something case 2 do something case 3 do something end DON’T  switch service case ‘army’ do something case ‘navy’ do something case ‘air force’ do something end

Enumerated Case Statement: Syntax DO ARMY = 1; NAVY = 2; AIR_FORCE = 3; switch service case ARMY do something case NAVY do something case AIR_FORCE do something end

Other Conventions: Revision follow_a_variable_naming_convention thereAreDifferentOnesToChooseFrom USE_CONSTANTS Use meaningful variable names % Comment liberally.

More Conventions DON’T WRITE MESSAGES IN CAPITALS – THIS IS SHOUTING Suppress all output inside functions; Use consistent indentation Makes your code easier to read Makes it easier to see syntax errors Comments and error messages should use correct English spelling and grammar and be clear and meaningful.

Summary After today’s lecture you should be able to: Identify and use a number of common code clichés Adhere to coding conventions