close

Вход

Забыли?

вход по аккаунту

?

Elementary Programming

код для вставкиСкачать
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);
Convert ASCII code (x) into character
п‚— ord(c);
Convert character (c) into ASCII code
п‚— length(str);
Find the length of a string (str)
п‚— copy(str, x, y);
Copy, starting from the xth element, y elements of string (str)
п‚— pos(substr, str);
Find position of substr in str
п‚— val(str, x, e);
Convert a string (str) into an integer (x)
п‚— str(x, str);
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 xth element of another (str2)
п‚— delete(str, x, y);
Delete, starting from xth element, y elements of string (str)
п‚— fillchar(str, x, c);
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)
Документ
Категория
Презентации
Просмотров
2
Размер файла
573 Кб
Теги
1/--страниц
Пожаловаться на содержимое документа