Introduction to Programming CSCE 110 Drawn from James Tam’s material.

Slides:



Advertisements
Similar presentations
Programming recap. Do you know these? LOW LEVEL 1 st generation: machine language (110011) 2 nd generation: assembly language (ADD, SUB) HIGH LEVEL 3.
Advertisements

Introduction to C Programming
Getting Started With Pascal Programming
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal.
James Tam Getting Started With Python Programming How are computer programs created Variables and constants Input and output Operators Common programming.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Getting Started With Pascal Programming
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
Introduction to Programming
Introduction to a Programming Environment
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
Mini-Pascal Compiling Mini-Pascal (MPC) language
Chapter 2: Introduction to C++.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
James Tam Creating Actual Computer Programs This section of notes describes how a computer program written by a person is translated to a form that can.
Computer Science 101 Introduction to Programming.
Variables and Constants
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Introduction to Python
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
CPS120: Introduction to Computer Science
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Introduction to Programming with RAPTOR
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
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.
CPS120: Introduction to Computer Science Variables and Constants.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
A variable is a name for a value stored in memory.
Component 1.6.
Data Types, Identifiers, and Expressions
Revision Lecture
CS 153: Concepts of Compiler Design October 3 Class Meeting
Lecture 2 (b) Introduction to Pascal
Building Java Programs Chapter 2
CMPE 152: Compiler Design October 2 Class Meeting
Chapter 2: Introduction to C++.
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design March 7 Class Meeting
Getting Started With Coding
Presentation transcript:

Introduction to Programming CSCE 110 Drawn from James Tam’s material

Computer Programs 1) A programmer writes a computer program 4)Anybody who has this ___________ installed on their computer can run (use) it. 3) An _________ program is created Binary is the language of the computer 2) A ___________ converts the program into a form that the computer can understand ________ e.g., gpc

Translators Convert computer programs to machine language Types –________________ Each time that the program is run the ____________ translates the program (translating a part at a time). If there are any errors during the process of __________ the program, the program will stop running right when the error is encountered.

Translators Convert computer programs to machine language Types –___________________ Before the program is run the _________ translates the program (compiling it all at once). If there are any errors during the ____________ process, no machine language executable will be produced. If there are no errors during _______________ then the translated machine language program can be run.

Compiling Programs: Basic View gpc Pascal compiler input a.out Machine language program output filename.p Pascal program

Basic Structure Of Pascal Programs Program documentation program name (input, output); Part I: Header const : Part II: Declarations begin : end. Part III: Statements Program name.p (Pascal source code)

Header Program documentation –Comments for the reader of the program (and not the computer) (* Marks the ____________ of the documentation *) Marks the ____________ of the documentation Program heading –Keyword: program, Name of program, if input and/or output operations performed by the program.

Heading Documentation (* * Tax-It v1.0: This program will * electronically calculate your tax * return. * This program will only allow you to * complete a Canadian tax return *) program taxIt (input, output); Example Header

Declarations List of constants More to come later during this term regarding this section

Statements The instructions in the program that actually gets things done They tell the computer what to do as the program is running Statements are __________________________ Example statements: display a message onscreen, prompt the user for input, open a file and write information to that file etc. Much more to come later throughout the rest of the term regarding this section

The Smallest Pascal Program program smallest; begin end. Note: The name in the header "smallest" should match the filename "smallest.pas". You can find an copy of this program here and the compiled version is here.here

Creating and Compiling Pascal in the CS Department PSPad Text Editor filename.pas (Unix file) Pacal Program gpc Pacal Compiler a.out (Unix file) Machine Language Program Use a text editor to create file “filename.pas” To compile the program in Unix type “gpc filename.pas” To run the program in Unix type “a.out”

Source Code Vs. Executable Files Source Code A file that contains the Pascal program code. It must end with a ‘dot-p’ or a ‘dot-pas’ suffix (program name.p). Can be viewed and edited. Cannot be executed. program smallest; begin :: end.

Source Code Vs. Executable Files Executable Code A file that contains machine language (binary) code. By default this file will be called “a.out”. It cannot be directly viewed or edited (meaningless). It can be executed. ::

1.Syntax/Compile Errors filename.p (Unix file) Pascal program PSPad Text editor gpc Pascal compiler Syntax error: ____________ ____________ ___________. They occur as the program is being compiled

2.Runtime Errors filename.p (Unix file) Pascal program gpc Pascal compiler Machine language program a.out (Unix file) Executing a.out Runtime error (________________) They occur as the program is running and _____________________________. PSPad Text editor

3.Logic Errors filename.p (Unix file) Pascal program PSPad Text editor gpc Pascal compiler Machine language program a.out (Unix file) Executing a.out Program ______________ ______________ ______________ They occur as the program is running, ______________________________.

Reserved Words and array begin case const div do downto else end file for foward function goto if in label For more information on reserved words go to the url: Have a predefined meaning in Pascal that cannot be changed

Reserved Words mod nil not of or packed procedure program record repeat set then to type until var while For more information on reserved words go to the url: Have a predefined meaning in Pascal that cannot be changed and array begin case const div do downto else end file for foward function goto if in label

Standard Identifiers Have a predefined meaning in Pascal that SHOULD NOT be changed Predefined constants –false –true –maxint Predefined types –boolean –char –integer –real –text Predefined files –input –output For more information on standard identifiers go to the url:

Predefined Functions abs arctan chr cos eof eoln exp ln odd ord pred round sin sqr sqrt succ trunc Know the ones in Table 3.1 of your book.

Predefined Procedures dispose get new pack page put read readln reset rewrite unpack write writeln

Variables Set aside a location in memory –This location can store __________ ‘piece’ of information Used to store information (temporary) –At most the information will be accessible _________________________________ Picture from Computers in your future by Pfaffenberger B

Variable Types integer – whole numbers real – whole numbers and fractions char – alphabetic, numeric and miscellaneous symbols (in UNIX type “man ascii”) boolean – a true or false value

Using Variables Usage (__________________________!) –Declaration –Accessing or assigning values to the variables

Declaring Variables Sets aside memory Memory locations are addressed through the name of the variable

Declaring Variables Sets aside memory Memory locations are addressed through ________ _______________ RAM Name of variable RESERVED

Declaring Variables Program documentation program name (input, output); Part I: Header const : Part II: Declarations begin : end. Part III: Statements Declare variables between the ‘begin’ and ‘end.’ Declare variables just after the ‘begin’

Declaring Variables Format: var name of first variable : type of first variable; var name of second variable : type of second variable;

Variable Declaration Example Declaring Variables You can find an copy of this program here and the compiled version is here.here program variableExample1; begin var height : real; var weight : real; var age : integer; end.

Global Variables Variables declared outside of the begin-end pair. program anExample; var num1 : integer; begin var num2 : integer; end. Global variable: DON’T DO IT THIS WAY Non-global variable (local variable): DO IT THIS WAY For now avoid doing this (additional details will be provided later in the course): generally this is regarded as ______ programming style.

Variable Naming Rules Should be _______________ Any combination of letters, numbers or underscore (________ begin with a number and _________ begin with an underscore) Can't be a reserved word (see the “Reserved Words” slide)

Variable Naming Conventions (2) Avoid using _________ identifiers (see the “Standard Identifiers” slides) Avoid distinguishing variable names only by ________ For variable names composed of multiple words separate each word by __________ _________________________ (except for ___________) or by using ____________.

Variable Naming Conventions Okay: –tax_rate –firstName Not Okay (___________________) - 1abc –test.msg –good-day –program Not okay (_________________) –x –writeln

Accessing Variables Can be done by referring to the name of the variable Format: name of variable Example: num

Assigning Values To Variables Format: Destination := Source; The source can be any ________________ (_______________, ________________ or ________________________)

Example of Assigning Values You can find an copy of this program here and the compiled version is here.here program variableExample2; begin var height: real; var weight: real; var age: integer; weight := height * 2.2; end. NO!

_____________ Assigning Values To Variables program variableExample2; begin var height : real; var weight: real; var age: integer; height := 69; weight := height * 2.2; end. Important lesson: ALWAYS _____________ your variables to some default starting value before using them.

Assigning Values To Variables program variableExample; begin var num1 : integer; var num2 : real; num1 := 12; num2 := 12.5; num2 := num1; end. num1 := num2; Not allowed!Rare Avoid assigning ________ types

Reminder RAM var num : integer; num num := 888; 888 program anExample; begin end. Variables Must First Be Declared Before They Can Be Used! The ________ Way

program anExample; begin end. Reminder RAM var num : integer; num := 888; Variables Must First Be Declared Before They Can Be Used! Compile Error: Where is num??? The __________ Way

Named Constants A memory location that is assigned a value that ______________________________ Declared in the constant declaration ("const") section The naming conventions for choosing variable names generally apply to constants but the name of constants should be all ____________. (You can separate multiple words with ___________________).

Named Constant Format const NAME_OF_FIRST_CONSTANT = value of 1st const; NAME_OF_SECOND_CONSTANT = value of 2nd const; etc.

Named Constant Examples const TAX_RATE = 0.25; SAMPLE_SIZE = 1000; YES = True; NO = False;

Declaring Named Constants Named constants are declared in the declarations section Program documentation program name (input, output); Part I: Header const : Part II: Declarations begin : end. Part III: Statements Declare constants here

Named Constants Compilable Ex. program anExample; const TAX_RATE = 0.25; SAMPLE_SIZE = 1000; YES = True; NO = False; MY_FIRST_INITIAL = ‘J’; begin var grossIncome : real; var afterTaxes : real; grossIncome := ; afterTaxes := grossIncome – (grossIncome * TAX_RATE); end.

Purpose Of Named Constants Makes the program easier to understand populationChange := ( – ) * currentPopulation; What do these mean??? Magic Numbers (_____________ _____________!) const BIRTH_RATE = ; DEATH_RATE = ; begin populationChange := (BIRTH_RATE – DEATH_RATE) * currentPopulation; Better

Purpose Of Named Constants Makes the program __________________ If the constant is referred to _______________ throughout the program, changing the value of the constant _______ will change it throughout the program.

Purpose Of Named Constants program population (output); const BIRTH_RATE = ; DEATH_RATE = ; begin var populationChange : real; var currentPopulation : real; populationChange := (BIRTH_RATE - DEATH_RATE) * currentPopulation; if (populationChange > 0) then writeln(‘Births: ‘, BIRTH_RATE, ‘ Deaths:’, DEATH_RATE, ‘ Change:’, populationChange) else if (populationChange < 0) then writeln(‘Births: ‘, BIRTH_RATE, ‘ Deaths:’, DEATH_RATE, ‘ Change:’, populationChange) end.

Purpose Of Named Constants program population (output); const BIRTH_RATE = 0.5; DEATH_RATE = ; begin var populationChange : real; var currentPopulation : real; populationChange := (BIRTH_RATE - DEATH_RATE) * currentPopulation; if (populationChange > 0) then writeln(‘Births: ‘, BIRTH_RATE, ‘ Deaths:’, DEATH_RATE, ‘ Change:’, populationChange) else if (populationChange < 0) then writeln(‘Births: ‘, BIRTH_RATE, ‘ Deaths:’, DEATH_RATE, ‘ Change:’, populationChange) end.

Purpose Of Named Constants program population (output); const BIRTH_RATE = ; DEATH_RATE = 0.01; begin var populationChange : real; var currentPopulation : real; populationChange := (BIRTH_RATE - DEATH_RATE) * currentPopulation; if (populationChange > 0) then writeln(‘Births: ‘, BIRTH_RATE, ‘ Deaths:’, DEATH_RATE, ‘ Change:’, populationChange) else if (populationChange < 0) then writeln(‘Births: ‘, BIRTH_RATE, ‘ Deaths:’, DEATH_RATE, ‘ Change:’, populationChange) end.

Performing Calculations OperationSymbol (Operator) Addition+ Subtraction- Multiplication* Real number division/ Integer divisionDIV Remainder (modulo)MOD

Priority of Operators High: –* –/ –DIV –MOD Low: –+ –- –unary + –unary -

Program Documentation It doesn’t __________________________ It doesn’t __________________________ __________________________________ It is for ____________________________

Program Documentation What does the program do e.g., tax program. What are it’s capabilities e.g., it calculates personal or small business tax. What are it’s limitations e.g., it only follows Canadian tax laws and cannot be used in the US.

Program Documentation What is the version of the program –If you don’t use numbers for the different versions of your program then consider using dates. How does the program work. –This is often a description in English (or another high- level) language that describes the way in which the program fulfills its functions. –The purpose of this description is to help the reader more quickly understand how the program works