ppt - DBS

advertisement

Introduction

In today’s session…

 What is programming?

 Why should I learn programming?

 Course Outline

 Introduction to Programming Language

 Introduction to Pascal

 ‘Hello world!’

 ‘Hello, <your name>!’

 Variables

What is programming?

 Process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs (From Wikipedia)

 To create a program (software) for usage of a particular sort, e.g. word processor (e.g. Microsoft Word,

OpenOffice.org Writer) for word processing (duh)

Why should I learn programming?

 Logic skills – very useful in

Maths/Physics/Chemistry/…

 Understand more about computer / information systems

 Learn algorithms

 Learn how to type – fast!

 Represent the school in HKOI/CCC

Course Outline

 Introduction

 Pascal Syntax – I

 Pascal Syntax – II

 Pascal Syntax – III (depends on schedule)

 Algorithms – I

 Algorithms – II (depends on schedule)

 HTML

 CSS

 PHP

3/5

6/5

10/5

13/5

17/5

20/5

24/5

27/5

31/5

Introduction to Programming

Language

 Derived from human language

 Syntax – grammatical rules

 Symbol-like

 Lots: Pascal, A+, A++, B, C, C--, C++, C#, D, D#, E, F,

F#, J, J++, J#, K, L, L++, M, Q, R, R++, S, T, X++, Y, Z,

VB, ActionScript, PHP, FORTRAN, Java, Javascript,

Python, etc.

 For different purposes and with different syntax / ideology behind programming

Introduction to Pascal

 ‘Educational’

 Syntax are human-like, easy to understand

 Important in programming

 Have practical use despite criticisms

Hello, world!

 Classic approach to any programming language

 Objective: Print ‘Hello, world!’

 Code: program hello; var a : integer; begin writeln(‘Hello, world!’); end.

Hello, world!

 Analysis

 program hello;

 ‘;’ : to separate different statements

‘program’ : a special keyword, to specify the name of the program

‘hello’: the name of the program

* this statement can be ignored totally.

* note any ‘name’ cannot be special keywords or start with numbers. E.g. program 3x; or program program; is not valid, but program _3x; or program _program; is valid.

Hello, world!

 Analysis (cont.)

 var a : integer;

 ‘var’ : a special keyword, to notify the following statements are declaration of variables

‘a’: the name of the variable

‘:’: declare ‘a’ is which type of variable

‘integer’: a type of variable, which is integers (with range

-32768..32767)

* this statement can be ignored totally since this program has no use of variables

* note program hello; var a : integer; is valid.

* note var a, b, c : integer; is valid.

Hello, world!

 Analysis

 begin writeln(‘Hello, world!’); end.

 ‘begin’ : to specify the beginning of the program

‘writeln’ : a function, to write the value then with a line break

* Try this out: writeln(‘Hello’); write(‘Hello’); writeln(‘Hello’);

(What is the difference between writeln() and write() ?) writeln; is valid (what does this do?)

‘’’: to specify that the worlds ‘Hello, world!’ is a string, i.e. a series of characters, but not a variable.

* writeln(‘Hello’); and writeln(Hello);

‘end.’: to specify the end of the program

Hello, <your name>!

 Objective: Print ‘Hello,’, then the name of the user who has inputted his/her name in the first place.

 Code: program hello; var name : string; begin write(‘Please input your name: ’); readln(name); writeln(‘Hello, ‘, name, ‘!’); end.

Hello, <your name>!

 Analysis

 var name:string;

 ‘string’ : a type of variable, which is the compilation of characters

‘name’: the name of variable of string

* Variables come in different kinds, e.g. for integers, we have integer, longint, int64; for characters, we have char; for string, we have string, ANSIstring.

* Each type of variables have different operations.

* Variable can be assigned by ‘:=‘. E.g. name := ‘This is my name’ or a := 3;

Note that ‘’ is added to state that the values inside is a string!

 a := a + 1 is possible!

Hello, <your name>!

 Analysis

 readln(name);

 ‘readln’ : to request input of variable to the user

‘name’: the name of variable of string

* Try read(name); instead of readln(name);, Or guess what’s the differences between readln and read.

Hello, <your name>!

 Analysis

 writeln(‘Hello, ‘,name,’!’);

 ‘,’ is used to separate strings and values in the variable ‘name’

 * writeln(‘H’,’e’,’l’,’l’,’o’,’,’,’ ‘,’); is equivalent to writeln(‘Hello,’);

Variables

 Integral types (0, 1, 2, 3, …)

 Shortint (-128..+127)

 Byte (0..255)

 Integer (-32768..+32767)

 Word0 (-65536..65535)

 Longint(-2146473648..+2146473647)

 Floating point types (0.1, 0.23, etc.)

 Real

 Double

Variables

 Operations of Integral / Floating point types

 ‘+’, e.g. c := a+b;

 ‘-’ e.g. c := a-b;

 ‘*’ e.g. c := a*b;

 ‘/’ e.g. c := a/b;

 ‘div’ e.g. c := a div b;

 ‘mod’ e.g. c := a mod b;

 ‘()’ e.g. c := (a + b)*a;

Variables

Non-numerical

Char (characters, e.g. ‘a’, ‘A’, ‘!’)

String (an array of characters, usually max. at 255)

Array (a collection of variable of specific types with index) <- covered later

Operations

 chr(x); ord(c); delete(str, x, y); fillchar(str, x, c);

Convert ASCII code (x) into character

Convert character (c) into ASCII code length(str); copy(str, x, y); pos(substr, str);

Find the length of a string (str)

Copy, starting from the x th element, y elements of string (str) val(str, x, e); str(x, str);

Find position of substr in str

Convert a string (str) into an integer (x)

Convert an integer (x) into a string (str) concat(str1, str2, …); Combine a string (str1) with another (str2, etc.) insert(str1, str2, x); Insert a string (str1) in the x th element of another (str2)

Delete, starting from xth element, y elements of string (str)

Fill the string (str) with a character (c) until string is (x-1) long

Last but not least…

 Pascal compilers:

 Free Pascal (Official HKOI/CCC compiler)

 Dev-Pascal

 Quick Pascal

 Useful software:

 Notepad++

 Useful website/contact:

 http://www2.dbs.edu.hk/club-soc/ecs

 Lin Yin Long (L6D)

Download