Pseudocode and comments

Slides:



Advertisements
Similar presentations
Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes.
Advertisements

MATH 224 – Discrete Mathematics
Chapter 3: Modules, Hierarchy Charts, and Documentation
Programming Logic and Design Fourth Edition, Introductory
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Programming in Visual Basic
Program Design and Development
Computer Science 1620 Programming & Problem Solving.
Understanding the Mainline Logical Flow Through a Program (continued)
Main task -write me a program
Review Algorithm Analysis Problem Solving Space Complexity
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Python
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
General Programming Introduction to Computing Science and Programming I.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Computer Science 101 Introduction to Programming.
Programming Lifecycle
Input, Output, and Processing
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
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.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
The Hashemite University Computer Engineering Department
Controlling Program Flow with Decision Structures.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
 Problem Analysis  Coding  Debugging  Testing.
Part of the Mathematics glossary: An algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem. The word derives from the name.
Introduction to Programming
Starter What does the following code do?
The Little man computer
Introduction to Computing Science and Programming I
Pseudocode and comments
Topic: Python’s building blocks -> Variables, Values, and Types
Topics Designing a Program Input, Processing, and Output
CSCI-235 Micro-Computer Applications
Topic: Python’s building blocks -> Variables, Values, and Types
GC211Data Structure Lecture2 Sara Alhajjam.
ALGORITHMS AND FLOWCHARTS
Lecture 3 of Computer Science II
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Introduction to Computer Programming
CS 240 – Lecture 11 Pseudocode.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Unit# 9: Computer Program Development
Analysis Algorithms.
Programming in Pseudocode
1) C program development 2) Selection structure
Creating your first C program
Algorithm Discovery and Design
Introduction to Algorithms and Programming
Unit 6 Assignment 2 Chris Boardley.
Examples Example Problems, their Algorithms, and their C Source Code.
Topics Designing a Program Input, Processing, and Output
Lecture 7 Algorithm Design & Implementation. All problems can be solved by employing any one of the following building blocks or their combinations 1.
Topics Designing a Program Input, Processing, and Output
ICS 3U Thursday, September 9.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Creating Maintainable code
Incremental Programming
Software Development Techniques
Hardware is… Software is…
Creating readable code
Presentation transcript:

Pseudocode and comments Peter Wad Sackett

Formal explanation of pseudocode Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a normal programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are essential for machine understanding of the algorithm, such as variable declarations, system-specific code and some subroutines. The programming language is augmented with natural language description details, where convenient, or with compact mathematical notation. The purpose of using pseudocode is that it is easier for people to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications that are documenting various algorithms, and also in planning of computer program development, for sketching out the structure of the program before the actual coding takes place. Thank you, wikipedia

Example Summing numbers in a file open file foreach line in file convert line to number add number to sum output sum Notice the use of indentation to show the structure of the code. The real code: sum = 0.0 with open(”numbers.dat”, ”r”) as infile: for line in infile: sum += float(line) print(”The sum is:”, sum) This (on purpose) does not deal with empty files.

Why use pseudocode? Structures the thoughts without the nitty-gritty details of the actual language syntax. Creates a framework to build your program. Minimizes confusion by separating the what-to-do from the how-to-do. Gives a top-down approach to solving the problem. Can start with a high-level description and narrow in on difficult spots. Great for communicating ideas to other people. Pseudocode can be written on a piece of paper, a text editor, a whiteboard or - well - anywhere. There is no formal syntax. Anything goes as long as it expresses your ideas clearly. More about pseudo code here: http://www.slideshare.net/DamianGordon1/pseudocode-10373156

Pseudocode in Real Life

The importance of comments in the program They explain to the reader what is going on. They enhance both memory and understanding. They divide the program in smaller parts, which are easier to grasp. They show the logic and thoughts behind the design. They give opportunity to explain hard parts. They tie neatly into the pseudocode, which you already made. Comments need to be structured, as they can also confuse the issue instead of enlighten it. There is a limit to how many comments you should make. A rule of thump is 1 line of comment for every 5-10 lines of code. It is fine to have 5-10 lines of comments to explain the function of a difficult part. However coding/comment style is very individual, and can vary with the complexity of the differents parts of the program.

Bad example Num1 = input('Enter a number:') Num2 = input('Enter a second number higher or lower:') #Transforming inputs to integers Num1 = int(Num1) Num2 = int(Num2) if Num1 > Num2: #If Num1 is greater than Num2 while (Num1-1) > Num2: Num2 += 1 # Num2 is slowly adding up to Num1-1 which will break the loop print(Num2) #Print all the numbers in between excluding Num1 and Num2 elif Num1 < Num2: #If Num2 is greater than Num1 while Num1 < (Num2-1): Num1 += 1 # Num1 is slowly adding up to Num2-1 which will break the loop print(Num1) #Print all the numbers in between excluding Num1 and Num2 elif Num1 == Num2: #If the numbers are equal, the user is prompted to run the program again. print('The numbers need to differ, rerun the program') The comments are confusing the code. Some are self-evident. Some too long. Some badly placed.

Bad example made good # Get input as integers Num1 = input('Enter a number:') Num2 = input('Enter a second number higher or lower:') Num1 = int(Num1) Num2 = int(Num2) # Decide which loop depending on order of Num1 and Num2 if Num1 > Num2: # Looping from smallest number to biggest number while (Num1-1) > Num2: Num2 += 1 # increment Num2 print(Num2) # print current value of Num2 elif Num1 < Num2: while Num1 < (Num2-1): Num1 += 1 # increment Num1 print(Num1) # print current value of Num1 elif Num1 == Num2: # if same number, run again print('The numbers need to differ, rerun the program') A long comment should be placed right above the statement or block of statements it concerns, at the same indention level. A short comment can be placed on the same line tabbed away from the code in such fashion that all short comments start the same place. Empty lines can be used to good effect to create space between elements of code.

Practical logic and algorithm design Borrowed from the web