James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.

Slides:



Advertisements
Similar presentations
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Advertisements

Introduction to Programming CSCE 110 Drawn from James Tam’s material.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
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 Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
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.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
Getting Started With Pascal Programming
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
Data types and variables
Introduction to Programming
Making Decisions In Python
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
Chapter 2 Data Types, Declarations, and Displays
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Objectives You should be able to describe: Data Types
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 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.
Input & Output: Console
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
CPS120: Introduction to Computer Science
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to Pascal The Basics of Program writing.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Programming, an introduction to Pascal
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
CPS120: Introduction to Computer Science Variables and Constants.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
So now we’re programming What do programs do? Manipulate (process) data Math Read files Write to files Create files.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
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.
© 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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
CST 1101 Problem Solving Using Computers
Computing and Statistical Data Analysis Lecture 2
Revision Lecture
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
CS 432: Compiler Construction Lecture 11
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Getting Started With Coding
Presentation transcript:

James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with Pascal Useful mathematical functions in Pascal

James Tam Basic Structure Of Pascal Programs (* Header *) program name (input, output) declarations var const begin end.

James Tam Variables Set aside a location in memory Used to store information (temporary) Types: integer – whole numbers real – whole numbers and fractions -Can't end or start with a decimal char – alphabetic, numeric and miscellaneous symbols boolean – true or false values Usage: Declaration Using values stored

James Tam Declaring Variables Sets aside memory Memory locations addressed through the name Naming conventions Should be meaningful Any combination of letters, numbers or underscore (can't begin with a number and shouldn't begin with an underscore) Can't be a reserved word e.g., program, begin, end (see Appendix B) Avoid using words with an existing meaning e.g., integer, real, boolean, write, writeln, read, readln Avoid distinguishing variable names only by case Okay: -tax_rate -firstName Not Okay - 1abc -x-x -test.msg -good-day

James Tam Declaring Variables (2) Typically occurs in the variable declaration ("var") section i.e., var name of first variable, name of second variable…: type of variables; e.g., var height, weight: real; age: integer;

James Tam Using Values Stored In Variables Assignment Performed via the assignment operator := Usage: -Destination := Source; 1 Example: -x := 5; -x:= y; -interest := principle * rate; -Initial := 'j'; Avoid assigning mixed types e.g., var num1: integer; num2: real; Begin num1 = 12; num2 = 12.5; num2 := num1; 1 The source can be any expression (constant, variable or formula) num1 := num2; Not allowed!

James Tam Named Constants A memory location that is assigned a value that cannot be changed Format: const name of first constant = value of first constant; name of second constant = value of second constant; etc. Location Anywhere after the "program" statement and before the "begin"

James Tam Purpose of Named Constants 1) Makes the program easier to understand e.g., begin population_change := ( – ) * current_population; Vs. const BIRTHRATE = ; DEATHRATE = ; begin population_change := (BIRTHRATE - DEATHRATE) * current_population; 2) Makes the program easier to maintain Magic Numbers (avoid!)

James Tam Output Displaying information onscreen Done via the write and writeln statements Formats (either write or writeln): write ('text message'); or writeln('text message'); write(name of variable or constant); or writeln (name of variable or constant); write('message', name of variable, 'message'…); or writeln('message', name of variable, 'message'…);

James Tam Formatting Output Computer often inserts spaces as it thinks is necessary in order to display output. Manually formatting of output: write or writeln (data to output: field width for data: no. of decimal places) e.g., writeln (num1:6:2); If the field width doesn’t match the actual size of the field Field width too small – extra spaces will be added for numerical variables. Field width too large – the data will be right justified (extra spaces will be put in front of the data).

James Tam Formatting Output (2) If the number of decimal places doesn’t match the actual number of decimal places. Set number of decimal places less than the actual number of decimal places – number will be rounded up. Set number of decimal places greater than the actual number of decimal places – number will be padded with zeros.

James Tam Formatting Output: Examples For the complete program and executable look under /home/231/examples/getting_started/out1.p (out1 for the compiled version) num1 := 123; num2 := ; writeln('Auto formatted by Pascal ', num1, num2); writeln('Manual format':13, num1:3, num2:7:3); writeln('Manual not enough':13, num1:2, num2:6:3); writeln('Manual too much':16, num1:4, num2:8:4);

James Tam Input The computer program getting information from the user Done via the read and readln statements Formats: (single input) read (name of variable); or readln (name of variable); (multiple inputs) read (nv1, nv2…); or readln (nv2, nv3…);

James Tam Input: Read Vs. Readln Both: Reads each value inputted and matches it the corresponding variable. Read If the user inputs additional values they will remain Readln Any additional values inputted will be discarded

James Tam Input: Read Vs. Readln (An example) For the complete version of this program look in Unix under: /home/231/examples/getting_started/read1.p (or read1 and read2 for the compiled version) e.g., read1.p write('Input some integers making sure to separate each one with a space '); write('or a new line: '); read (num1, num2); write('Input some integers making sure to separate each one with a space '); write('or a newline: '); read(num3, num4);

James Tam Input: Read Vs. Readln (An example (2)) For the complete version of this program look in Unix under: /home/231/examples/getting_started/read2.p (or read2 for the compiled version) e.g., read2.p write('Input some integers making sure to separate each one with a space '); write('or a newline: '); readln (num1, num2); write('Input some integers making sure to separate each one with a space '); write('or a newline: '); readln(num3, num4);

James Tam Uses Of Readln To filter out extraneous input As an input prompt e.g., writeln('To continue press return'); readln;

James Tam Some Useful Functions See also Appendix D in Pascal Programming and Problem Solving by Leestma S. and Nyhoff L. Name Description Input type Type of result Example abs absolute value integer integer abs(-2) = 2 real real abs(-2.2) = 2.2 round rounding real integer round(2.6) = 3 trunc truncation real integer trunc(2.6) = 2 sqr squaring integer integer sqr(2) = 4 real real sqr(1.1) = 1.21 sqrt square root integer real sqrt(4) = 2.00 or real

James Tam Summary What are the fundamental parts of a Pascal program What are the basic types of variables employed in Pascal and how are they used How to output information with the write and writeln statements Getting information from the user through the read and readln statements How are some common mathematical functions performed in Pascal