Programming, an introduction to Pascal

Slides:



Advertisements
Similar presentations
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Advertisements

Programming recap. Do you know these? LOW LEVEL 1 st generation: machine language (110011) 2 nd generation: assembly language (ADD, SUB) HIGH LEVEL 3.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
COMP 4—Power Tools for the Mind1 PowerTools What’s in the Box? Turing 1: An Introduction to Programming You will learn elementary computer programming.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers.
Introduction to a Programming Environment
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Computer Science 101 Introduction to Programming.
Computers and Programming อนันต์ ผลเพิ่ม Anan Phonphoem
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to Python
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Science 101 Introduction to Programming.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
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
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Introduction to Pascal The Basics of Program writing.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Programming Section 3.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
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.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
PASCAL PROGRAMMING.
CPS120: Introduction to Computer Science Variables and Constants.
The Hashemite University Computer Engineering Department
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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)
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Assignment and Interactive Input.
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
The Selection Structure
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
High Level Programming Languages
Chapter 2 - Introduction to C Programming
Getting Started With Coding
Presentation transcript:

Programming, an introduction to Pascal The Basics of Program writing.

Generations of Languages Classification of programming languages 1st Generation Machine Code 2nd Generation Assembly Language 3rd Generation Pascal 4th Generation programming languages 5th Generation programming languages Low Level High Level

Machine language vs Assembly Language Consists of strings made of 1’s and 0’s. It is the only programming language as computer can understand. Assembly Language Uses special codes called MNEMONIC to represent the language instructions instead of using 1’s and 0’s. AD, SUB, JMP, MUL

Low-Level languages They are machine dependent (The code can only be understood by a machine). FIRST GENERATION LANGUAGES Made of Machine code. Eg. 1001011011 SECOND GENERATION LANGUAGES Assembly Language with simple commands like “Add A,B” (Add the contents of A to B). Also called mnemonics. Write out a sample machine code program.

High Level Languages Not Machine dependent and use words similar to English making them easier to write. THIRD GENERATION LANGUAGES These languages are converted from simple English to machine code. Eg. FORTRAN, BASIC, Pascal, C FOURTH GENERATION LANGUAGES Easier to write by just giving simple commands and no long lines of code. Eg. COBOL

High Level Languages FIFTH GENERATION LANGUAGES These languages perform tasks based on the goal to be achieved. In theory it should be able to translate the words a person says. Eg. PROLOG

History of Pascal Pascal, named in honor of the French mathematician and philosopher Blaise Pascal, was developed by Niklaus Wirth.

Pascal Format HEADER Program <program name>; const <constant values in the program>; var <variables used in the program> begin <the actual program code> end. CONSTANT DECLARATION VARIABLE DECLARATION PROGRAM BODY

Constants and Variables Identifier – The name that identifies a variable or constant Constant – a value that is stored in memory and cannot be changed. Eg. Pi = 3.14 Variable – A value that is stored in memory and can be changed. Eg. x,a,b,y…….

Variable Types Integer – positive and negative numbers which do not have a decimal point. Real – positive and negative numbers that may have a decimal point Char – a single character (letter, digit or symbol) String – a group of characters Boolean – either true or false

THREE VARIABLES OF THE SAME TYPE CAN BE SEPARATED BY A COMMA Declaring Variables var gender: char; num1, num2, num3: integer; product: real; THREE VARIABLES OF THE SAME TYPE CAN BE SEPARATED BY A COMMA

Declaring Constants const pi := 3.142; VAT := 0.15; Gravity := 9.81;

Basic Commands Write – writes the command on the same line Writeln – writes the command and goes to the next line Read – reads the input Readln – reads the input and then goes to the next line.

Read Commands Read(num1) This will read the input from the user of the program and store the value in num1 Readln(num1) This will read the input from the user and store the value in num1 and go to the next line. It is also used to pause at the end of a program until the user presses enter.

Write commands Write(‘ hello world’); Writeln (‘hello world’); DISPLAYS ‘HELLO WORLD’ IN THE SAME LINE Hello World DISPLAYS ‘HELLO WORLD’ AND GOES TO THE NEXT LINE Hello World _

Assignments The value of a variable can be changed by doing an assignment statement. It is done by using a colon and equal sign. := eg. Age := 15 cost := 65 Count := count + 1

Simple Programs This program writes Hello World and then waits for the user to press enter to exit the program. Program HelloWorld; Begin Write(‘Hello World’); Readln; End. Explain Program line by line

Simple Programs This program writes ‘Hello’ in one line then goes to the other, then it prints ‘World’ and waits for the user to press enter. program Hello;   begin    Writeln('Hello');    Write('world');    Readln; end. Explain Program line by line

Simple Programs Program Addition; Var a:integer; Begin This declares ‘a’ as an integer. It then adds two numbers and assigns the result to ‘a’. Simple Programs Program Addition;   Var a:integer; Begin Writeln('The sum of 6 and 4 is:'); a := 6 + 4; write(a); readln; End. Explain Program line by line

This declares ‘a’ as an integer. It then multiplies two numbers and assigns the result to ‘a’. Then displays ‘a’ Simple Programs Program Multiplication; Var a: integer; Begin Writeln ('This will find the product of 6 and 4'); a := 6*4 ; writeln ('Press enter to view the result'); readln; write(a); End. Explain Program line by line

This declares ‘a’ ‘b’ and ‘c’ as integers. Simple Programs This declares ‘a’ ‘b’ and ‘c’ as integers. It then adds ‘a’ to ‘b’ and assigns the result to ‘c’. Then displays ‘c’ Program addition; Var a:integer; b:integer; c: integer; Begin writeln(‘Enter first number’); read(a); writeln(‘Enter second number’); read(b); writeln(‘Your numbers are:’ ,a ,b); c:= a + b; writeln(‘the sum of your numbers are/is:’); writeln(c); readln; End. Explain Program line by line

Comments Comments are used to give descriptions to sections of code so that other programmers can understand what is being done. A comment is activated by putting to forward slashes then typing the comment. Eg. Readln (x); //get value for x Readln (y); //get value for y A = x * y; //multiply x and y Writeln (a); //write value of a on screen

Practice Questions Write a pascal program to read two numbers, find the product. The program should display the two numbers entered and the product. Write a pascal program to read the costs of 5 grocery items. Find the sum of the items, display it. Multiply the sum by 15% to get the VAT and add the VAT to the sum of the items and display the total.

Practice Questions Write a program to get the scores of 11 players on a cricket team and find the average and display it. Write a program to get the dimensions of a square and a rectangle and find the area of the combined area of the two.

Selection Control Structure IF <condition true> THEN <perform action> ELSE IF age > 18 THEN You can get your drivers permit ELSE You cannot legally drive

Selection Pseudocode Example get x get y sum = x + y if sum < 20 then print “ the score is small” else print “the score is large”

Selection Pascal Example begin writeln('Please enter your mark'); readln(a); if a>50 then writeln ('You have passed') else writeln('You have failed'); readln; end.

Selection Practice Questions Write a pascal program to read three test scores and find the average. If the average is greater than 50 display pass, else display fail. Write a pascal program to read the price of two dresses. Display which dress costs more. Write a pascal program to read the shoe size of a student. If their shoe size is less than 7 print your foot is small else print your foot is big.

Practice questions 2 Write a pascal program to read three numbers a, b and c. Find the sum of a and b and the product of b and c. Display both the product and sum. If the sum is greater than the product display “The sum is bigger”. If not then display “the product is bigger”

Types of Errors Syntax Error Logic Error Run Time Error

Syntax errors They happen when the programing language used is not formatted according to the set standard. Eg 1. Readline (a) is a syntax error it should be: Readln (a) Eg 2. Writeln (‘Hello World’) is a syntax error it shouldbe: Writeln (‘Hello World’);

Logic Errors These occur when the calculations do make sense mathematically or logically. Eg1. C := a + b /2 should be C := (a+b) / 2 Eg2. Avg := (a + b + c +d ) / 2 should be Avg := (a + b + c + d) / 4

Runtime Errors These cause the program to crash or freeze while running preventing it from giving an output usually caused by bad calculations. Eg1. A = C / 0 It is a runtime error because you cannot divide by zero

Selection Practice Questions Write a pascal program to read three test scores and find the average. If the average is greater than 50 display pass, else display fail. Write a pascal program to read the price of two dresses. Display which dress costs more. Write a pascal program to read the shoe size of a student. If their shoe size is less than 7 print your foot is small else print your foot is big.