General Programming Introduction to Computing Science and Programming I.

Slides:



Advertisements
Similar presentations
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Introduction to Computing Science and Programming I
Python Programming: An Introduction to Computer Science
Debugging Introduction to Computing Science and Programming I.
Computer Science 1620 Loops.
Pseudocode and Algorithms
Computer Science 1620 Programming & Problem Solving.
CS 201 Functions Debzani Deb.
Programming Fundamentals (750113) Ch1. Problem Solving
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 3 Planning Your Solution
Computer Science 101 Introduction to Programming.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
The University of Texas – Pan American
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Introduction to Python
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Computer Science 101 Introduction to Programming.
Programming Lifecycle
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
C++ crash course Class 8 statements, sort, flight times program.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Python Conditionals chapter 5
1 Program Planning and Design Important stages before actual program is written.
CSCI-100 Introduction to Computing
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Files Tutor: You will need ….
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
TCU CoSc Introduction to Programming (with Java) Java Language Overview.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Variables, Types, Expressions Intro2CS – week 1b 1.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Few More Math Operators
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
CMPT 120 Topic: Python’s building blocks -> More Statements
Pseudocode and comments
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Introduction to Programming and Visual Basic
Completing the Problem-Solving Process
Variables, Expressions, and IO
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
ERRORS AND EXCEPTIONS Errors:
Python programming exercise
Pseudocode and comments
CHAPTER 6 Testing and Debugging.
Presentation transcript:

General Programming Introduction to Computing Science and Programming I

General Programming Basic set of steps for writing a program Basic set of steps for writing a program Define the problem. Define the problem. Create an algorithm to solve the problem and write it out in pseudocode Create an algorithm to solve the problem and write it out in pseudocode Convert this algorithm into actual code, testing small parts of it along the way Convert this algorithm into actual code, testing small parts of it along the way When finished converting the algorithm to code, test for errors When finished converting the algorithm to code, test for errors Debug any errors and go back to the previous step Debug any errors and go back to the previous step

General Programming When you are finished writing your algorithm, look for pieces that you can program and test separately. When you are finished writing your algorithm, look for pieces that you can program and test separately. To illustrate the point, let’s look at the second part of Lab 2 and see how we can break it apart To illustrate the point, let’s look at the second part of Lab 2 and see how we can break it apart

Example Write a program that displays a menu to the user and based on their choice, calculates the area of a circle or triangle who’s attributes are input by the user. Write a program that displays a menu to the user and based on their choice, calculates the area of a circle or triangle who’s attributes are input by the user. Algorithm Algorithm write menu to screen read option if option is triangle read base/height from user if base and height are >= 0 calculate and display area else display error else if option is circle read radius if radius is >= 0 calculate and display circle’s area else display error else write incorrect option entered

Example What parts of this algorithm can you code and test separately? What parts of this algorithm can you code and test separately? Menu Structure Menu Structure Triangle calculation Triangle calculation Circle calculation Circle calculation User input and check for negative values User input and check for negative values Each of these could be coded and tested separately before combining. Each of these could be coded and tested separately before combining.

General Programming This idea of breaking the problem up may seem unimportant when working with the small programs we are now, but it is still helpful. This idea of breaking the problem up may seem unimportant when working with the small programs we are now, but it is still helpful. When you move on to more complex programs the idea is essential. When you move on to more complex programs the idea is essential. If you write large amounts of code with having made sure small pieces are correct, finding errors becomes more difficult. If you write large amounts of code with having made sure small pieces are correct, finding errors becomes more difficult.

Errors You will encounter errors as you write your programs. You will encounter errors as you write your programs. There are other ways to categorize them, but we will look at three basic types of error. There are other ways to categorize them, but we will look at three basic types of error. Syntax Errors Syntax Errors Runtime Errors Runtime Errors Semantic Errors Semantic Errors

Syntax Errors Errors in the syntax of your code. Errors in the syntax of your code. Syntax refers to the rules that define proper statements. Syntax refers to the rules that define proper statements. Python will detect syntax errors and not allow you to run your code until you correct them. Python will detect syntax errors and not allow you to run your code until you correct them.

Runtime Errors Once your code has no syntactic errors, you are allowed to execute it Once your code has no syntactic errors, you are allowed to execute it Runtime errors refer to errors that do not present themselves until your code is executed (runtime). Runtime errors refer to errors that do not present themselves until your code is executed (runtime). Examples: Examples: Division by zero. Division by zero. Improper conversions. Improper conversions. Your program will be terminated if a runtime error occurs during execution. Your program will be terminated if a runtime error occurs during execution.

Semantic Errors Errors in the semantics of your code. Errors in the semantics of your code. Syntax defines how you can create statements. Semantics tell you what the meaning of those statements are. Syntax defines how you can create statements. Semantics tell you what the meaning of those statements are. A program has semantic errors if it runs and completes, but does not do what it was created to. A program has semantic errors if it runs and completes, but does not do what it was created to.

Error Message When an error occurs, Python prints some info to help you understand what happened. Learning how to understand these error message will help you solve problems in your programs. When an error occurs, Python prints some info to help you understand what happened. Learning how to understand these error message will help you solve problems in your programs. For Example For Example >>> 4/0 Traceback (most recent call last): File " ", line 1, in File " ", line 1, in 4/0 4/0 ZeroDivisionError: integer division or modulo by zero The last line tells you what type of error occurred. The last line tells you what type of error occurred. The third line prints out the statement from that line. The third line prints out the statement from that line. The second line tells you on what line of code the error occurred. This is more helpful when executing a program. The second line tells you on what line of code the error occurred. This is more helpful when executing a program.

Some Python Errors ZeroDivisionError ZeroDivisionError NameError NameError Try to use a variable that doesn’t exist. Try to use a variable that doesn’t exist. ValueError ValueError Try to convert an inappropriate string to a number Try to convert an inappropriate string to a number

Correcting Errors Strategies for correcting errors (debugging) Strategies for correcting errors (debugging) Remove parts of your code to see if they are involved in the error. Remove parts of your code to see if they are involved in the error. Add print statements to check the value of a variable that is involved. (remember to remove these statements when finished) Add print statements to check the value of a variable that is involved. (remember to remove these statements when finished)

Coding Style Coding style deals with how you format your code. Good coding style makes it easy for people to understand your code Coding style deals with how you format your code. Good coding style makes it easy for people to understand your code Coding style issues. Coding style issues. Comments Comments Proper spacing Proper spacing Good variable/function names Good variable/function names

Comments A comment is text you put in your code that will be ignored. This allows you to give short explanations to help people understand your code. A comment is text you put in your code that will be ignored. This allows you to give short explanations to help people understand your code. Use the # character and anything written after it will be ignored Use the # character and anything written after it will be ignored

Comments Don’t add comments to explain every line Don’t add comments to explain every line #add one to x x = x+1 Often useful to add a comment for chunks of code, such as before control structures Often useful to add a comment for chunks of code, such as before control structures # if the user entered good data, add it in if value >= 0: sum = sum + value count = count + 1 # search for a value that divides num while num%factor != 0: factor = factor + 1

Spacing Poor spacing can make lines of code more difficult to understand. Poor spacing can make lines of code more difficult to understand. Bad: y = 100 / x+1 Good: y = 100/x + 1 Add blank lines in the code to make it easier to read. Add blank lines in the code to make it easier to read.

Coding Style Bad Bad a = int(raw_input("Enter an integer: ")) b = 0 for i in range(a+1): if i>0: if a % i==0: b = b + 1 print "There are " + str(b) + " factors.“ Good Good # get user input and initialize num = int(raw_input("Enter an integer: ")) count = 0 # check every possible factor (1...num) for factor in range(1, num+1): # if factor divides num, it really is a factor if num%factor == 0: count = count + 1 # output results print "There are " + str(count) + " factors."