204111: Introduction to Computer & Programming
Lecture 1: Introduction
Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand http://kdl.cpe.ku.ac.th
Computer & Program
Computers
Program
Devices for performing computations at high speeds with great accuracy A machine that can be programmed to manipulate symbols. Can quickly store and retrieve large amounts of data. Physical components are known as "hardware". Hardware components: CPU, Memory, Mouse, Keyboard, etc.
A set of instructions for a computer to follow, written in specific programming language
2
Computer Components
CPU (Central Processing Unit) Primary storage (memory) Secondary storage (disks, tapes etc.) Input devices (mouse, keyboard etc.) Output devices (screen, printer, plotter, etc.)
3
Computer Components
Secondary Memory
Main Memory
HD CPU Input Devices
Output Devices
4
Computer Systems
Hardware (HW) Actual physical machines (equipment) that make up the computer Software (SW) Programs written for a specific application are often called softwares.
5
Computer Languages
•
Computer languages categories:
Machine language
Assembly language High-Level Language
Binary Code
Uses English-like language Machine independent Portable Examples: Pascal, C, C++, Java, Fortran, . . .
Computer can execute only the machine language
6
High-Level Languages
Procedural Language
Fortran Cobol Basic C Pascal
C++
Object-Oriented Language
Functional Language
Lisp Logic Language Prolog
7
The translation Process
Source Program
Compiler
Executable Program
8
รู้จักกับคอมพิวเตอร์
คอมพิวเตอร์ (Computer) คือ เครื่องคานวณอิเล็กทรอนิกส์ที่สามารถทางานคานวณผล และเปรียบเทียบค่าตามชุดคาสั่งด้วยความเร็วสูงอย่างต่อเนืองและอัตโนมัติ ่ การที่เครื่องคอมพิวเตอร์จะทางานได้ จาเป็นจะต้องมีชุดคาสั่งกากับ เพื่อให้คอมพิวเตอร์ทางาน ได้ผลตามที่ต้องการ
9
คอมพิวเตอร์ในงานด้านต่างๆ
การคานวณทางคณิตศาสตร์
การจัดการงานทั่วไป
เครื่องคิดเลข การจาลองสถานการณ์ (simulation) การแก้ไขปัญหาทางวิทยาศาสตร์ (scientific problem solving) ATM การจัดการประมวลผลบัญชีลูกค้า
การคานวณทางวิทยาศาสตร์
การทาเอกสาร การนาเสนอผลงาน
การสื่อสาร
โทรศัพท์มือถือ โทรคมนาคม ดนตรี ภาพยนตร์ Graphic เกมส์
ความบันเทิง
การควบคุมธุรกรรม
10
ส่วนประกอบของคอมพิวเตอร์
1.
ระบบคอมพิวเตอร์ ประกอบด้วย 2 ส่วนหลัก ได้แก่ ฮาร์ดแวร์ Hardware : Microprocessor , Monitor,
Hard disk , Keyboard , Mouse , Printer , etc ซอฟต์แวร์ Software : Window XP , Linux Microsoft word, Internet Explorer, Adobe Photoshop, Ragnarok Online
2.
11
Computer Components
Secondary Memory
Main Memory
HD CPU Input Devices
Output Devices
12
Microprocessor
เป็นเหมือนส่วนสมองหลักของ เครื่องคอมพิวเตอร์ ทาหน้าที่ประมวลผลทาง คณิตศาสตร์ (บวก ลบ คูณ หาร) และทางตรรกศาสตร์ ( and or not) Intel / AMD
13
Memory
เป็นที่เก็บข้อมูลในเครื่องคอมพิวเตอร์ ก่อนที่จะนาข้อมูลนั้นมา ประมวลผลใน microprocessor หน่วยความจาในเครื่องคอมพิวเตอร์แบ่งออกเป็น 2 ประเภท
หน่วยความจาหลัก (Main Memory) หน่วยความจาสารอง (Secondary Memory)
14
หน่วยความจาหลัก
เก็บชุดคาสั่งและข้อมูลต่างๆ ไมโครโปรเซสเซอร์จะอ่านข้อมูลจากหน่วยความจาชนิดนี้เป็น หลัก RAM (Random Access Memory)
สามารถเขียนหรืออ่านข้อมูลได้ เก็บข้อมูลไม่ถาวร ข้อมูลจะสูญหายหากขาดไฟเลี้ยง อ่านข้อมูลได้อย่างเดียว เก็บข้อมูลได้ถาวร แม้ว่าจะไม่มีไฟเลี้ยง
ROM (Read Only Memory)
RAM
15
Secondary Memory
ความจุสูงขึ้นกว่าหน่วยความจาหลัก แต่ความเร็วในการถ่ายเทข้อมูลน้อยกว่า
Hard disk Floppy disk Zip drive Thumb drive CD-ROM
Thumb Drive
16
Data Representation
ข้อมูลในคอมพิวเตอร์จะถูกเก็บอยู่ในรูปของ bit
bit = binary digit (เลขฐานสอง)
มีค่าเป็น 0 หรือ 1 (ตามสถานะของไฟฟ้า)
byte = 8 bits
ข้อมูล 1 byte ในเครื่องคอมพิวเตอร์อาจจะมีความหมายแทนข้อมูลได้หลากหลาย ประเภท
ตัวอย่างเช่น ข้อมูล 1 byte เก็บค่าดังนี้ 011000012 ข้อมูลดังกล่าวอาจจะแทนอักขระ „a‟ หรือแทนตัวเลข 97 ก็ได้ ขึ้นอยู่กับโปรแกรมคอมพิวเตอร์ ว่าจะตีค่า 1 byte นั้นเป็นอะไร
17
I/O Devices
เป็นอุปกรณ์ที่ทาหน้าที่ ติดต่อสื่อสารระหว่างคอมพิวเตอร์ กับโลกภายนอก Keyboard Mouse Monitor Printer Speaker
18
Computer Components
Secondary Memory
Main Memory
HD CPU Input Devices
Output Devices
19
การทางานของเครื่องคอมพิวเตอร์
ชุดคาสั่งและข้อมูลจะถูกเก็บอยู่ในหน่วยความจา CPU (microprocessor)ดึงข้อมูลและชุดคาสั่งมาจาก
หน่วยความจา อุปกรณ์อินพุต (Keyboard , Mouse , etc.)
CPU ประมวลผลข้อมูล CPU นาข้อมูลผลลัพธ์ที่ถูกประมวลผลแล้วไปยัง
หน่วยความจา อุปกรณ์เอาท์พุต (Monitor , Printer , etc.)
20
โปรแกรมคอมพิวเตอร์
เราไม่สามารถทางานต่างๆได้ โดยอาศัยเพียงแต่เครื่องคอมพิวเตอร์อย่างเดียว เครื่องคอมพิวเตอร์สามารถทางานได้รวดเร็ว แต่ไม่สามารถที่จะตัดสินใจหรือ แก้ปัญหาใดๆได้ด้วยลาพัง มนุษย์จะต้องเป็นผู้กาหนดและสั่งการคอมพิวเตอร์ให้ทางานให้ได้อย่างที่ มนุษย์ต้องการ
โปรแกรมคอมพิวเตอร์ หมายถึง ชุดลาดับของคาสั่งทีกาหนดให้คอมพิวเตอร์ทาการ ่ ประมวลผลข้อมูลเพื่อให้แก้ปัญหา และทางานได้อย่างที่ตองการ ้ Programming = Designing and writing a computer program.
21
ชนิดของภาษาในการเขียนโปรแกรม
There are 3 kind of programming languages
Machine language (0‟s and 1‟s) Assembly language (mov, and, or, etc…) High-level language (nearly like human word)
Computer itself understands only Machine language.
22
The translation Process
Source Program
Compiler /Interpreter
Executable Program
23
Language translator
Interpreter / Compiler
……
main: pushl movl subl andl movl subl subl pushl …….. Assembler %ebp %esp, %ebp $8, %esp $-16, %esp $0, %eax %eax, %esp $8, %esp $.LC0
….
00011000110001110
00110001110101111
00011111111110001 11011100001011011 …… Machine
class MainClass { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } }
High-level language
Assembly language
Machine language
Hello World! _
24
Interpreter
Read high-level language , translate , and then execute one command at a time.
0100100 0100101 0100110
…. writeln(„a‟); writeln(„b‟); writeln(„c‟); ….
Interpreter
a b c
Source
25
Compiler
Read all program at a time. Translate into executable file (machine language) library Compiler Linker object file
…. 0100100 0100101 0100101 .....
…. writeln(„a‟); writeln(„b‟); writeln(„c‟); ….
Source
a b c
Exe file
26
ขั้นตอนในการเขียนโปรแกรม
วิเคราะห์ปัญหา (Analysis)
ระบุปัญหาที่ต้องการจะแก้ไข แบ่งปัญหานั้นออกเป็นปัญหาย่อยๆ ค้นหาแนวคิดและวิธีการที่จะแก้ปัญหานั้นๆ เขียนโปรแกรมด้วยภาษาคอมพิวเตอร์ โดยอาศัยแนวคิดการแก้ปัญหาที่คิดไว้ก่อนหน้า Compile โปรแกรมที่เขียนขึ้นให้เป็นภาษาที่เครื่องอ่านได้
ทดลองรันโปรแกรมและตรวจสอบการทางาน หากโปรแกรมทางานไม่ถูกต้อง หาจุดผิดให้พบ แก้ไขจุดผิดพลาดและทดลองใหม่ จนกว่าโปรแกรมจะทางานได้ถูกต้อง
พัฒนาโปรแกรม (Implementation)
ตรวจสอบและแก้ไขจุดผิดพลาด (Testing & Debugging)
27
Analysis
Example : Circle area problem Input
circle radius (รัศมี) calculate circle area Circle area
28
Process
Output
Analysis
Pseudo-code (รหัสเทียม)
ใช้คาที่มีความหมาย คนอ่านแล้วเข้าใจ ขั้นตอนการทางานเป็นแบบคอมพิวเตอร์
Program Circle Begin
Example
read circle radius
area = Pi * radius * radius write circle area
End.
29
Test & Debug
Compile โปรแกรมที่เขียนขึ้นแล้วทดลองรัน ตรวจสอบผลการทางาน แก้ไขข้อผิดพลาด (bugs)
30
Implement
เขียนโปรแกรมโดยใช้ภาษา Assembly หรือภาษาระดับสูง เราสามารถใช้แนวคิดเดียวกัน เขียนโปรแกรมได้หลายภาษา Example
#define PI void main() { int float 3.1416f
class MainClass { public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area; radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area);
radius; area;
}
}
scanf(“%d”,&radius); area = PI * radius * radius; printf(“%f”,area);
}
31
Test & Debug
There are 2 kinds of bugs
class MainClass { public static void Main(string[] args) { const double pi = 3.1416;
class MainClass { public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area;
radius = int.Parse(Console.ReadLine()); area = pi * raddius * radius; Console.WriteLine(area); }
int radius; double area;
radius = int.Parse(Console.ReadLine()); area = pi * radius; Console.WriteLine(area);
}
}
}
Syntax bug
Semantic bug
32
Programming Languages
The computer really only knows its own machine language or assembly language. Any high level languages would have be translated into machine language. An assembler translates an assembly language program into machine language. A compiler translates a high-level language into machine language. A source program is written in a high-level programming language. (.cs, .pas, .c) An executable program is the machine language version of a source program. (.exe)
33
Why C# ?
provides a teaching language that highlights concepts common to all computer languages
34
C# Program
แดง ขาว
โฮ่ง
ด่าง
สุนัข class
C# is an object-oriented programming language. Everything must be in some class. A program is a set of class declarations.
35
A simple C# Program
36
Output
37
C# Program: What did you see?
Grouping using { } Statement ending with ; Various keywords: using, namespace, class, public, static, void "Hello World!"
38
C# Program: class declaration
Class name Member declarations
Data member Method (or functions)
class MainClass { public static void Main(string[] args) { const double pi = 3.1416; int radius; double area;
In the beginning of the class, we usually use only one method: Main --- which is where our program starts.
}
radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); }
39
Basic grouping { }
{ curly brackets are used to group things together in C# } { they come in pairs, and { they can { be nested } } } Example:
40
Semi-colon;
In C#, every statement must end in a semicolon. Compiler uses semicolon to find where one statement ends. It does not use line breaks or any textual markers. For example, the following are the same programs (for the compiler):
using System; namespace Hello { class MainClass { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } } } using System; namespace Hello{ class MainClass {public static void Main(string[] args) { Console.WriteLine( "Hello World!");}}}
41
Various keywords
Various keywords help the C# compiler to understand our programs
abstract catch default explicit foreach internal object public sizeof throw unsafe as char delegate extern goto is operator readonly stackalloc true ushort base checked do false if lock out ref static try using bool class double finally implicit long override return string typeof virtual break const else fixed in namespace params sbyte struct uint void byte continue enum float int new private sealed switch ulong volatile case decimal event for interface null protected short this unchecked while
42
Keywords
namespace - Programs are organized using “namespace.” using - specifies external namespaces that the program uses. class - defines class. (remember that our program is one of the classes.)
43
Keywords
Other keywords we are using:
void, public, static, etc.
At this point, it is difficult to understand them fully. You just have to remember the patterns that we use them.
44
/* comments */
Comments are inserted into C# program by enclosing it within /* and */ , or after // Comments are ignored by the computer, but helpful to explain the program
using System; class MyClass { // This is an example class, // and this is a line comment… static void Main() { /* Sometimes, we love to comment things, and anything inside thes markers are comments */ Console.WriteLine("Hello!"); // chill~ chill~~ } }
45
Method Main
is a method where the program for our class starts.
46
Main declaration
Here is how we declare our Main function (inside our class): static void Main() { // โปรแกรมของเราจะอยู่ต่อจากนี้ … }
Remark: there are 4 ways to define Main function.
We use the simplest one.
static void Main() {...} static void Main(string[] args) {...} static int Main() {...} static int Main(string[] args) {...}
47
Inside method Main
Variable declarations Statements
public static void Main(string[] args) { const double pi = 3.1416;
int radius; double area; radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area);
}
48
Variables
Example:
int radius; double area;
Variables are used to store “data.” They must be declared before used. To declare them, we must specify their “types.”
49
Data
DATA - information that must be supplied to a program before it can produce results.
Basic data types:
int - whole numbers 34 -90 0 112 double - numbers with fractions 34.345 0.0 bool true or false char - Computer character set string - strings
50
Data
Data has to be stored and referenced. Easiest way to refer to data is to give it a symbolic name or identifier Data in a program can be classified as Constant data and Variable data.
51
Variables
Variable is a container for a data value.
A variable is created via a declaration where both its name and type are specified Every variable has a name, type and value.
52
Variable declaration
Syntax:
;
Examples:
int radius; double area; int a,b,c; bool isokay;
We can also assign its initial value. E.g., int k = 200;
bool done = false;
53
Assignments
Syntax:
= ;
Examples:
area = radius*radius*3.14; a = b + c + d;
54
Constants
Constants can be used in programs. E.g., 5, 10.4, "Hello" are constants. There are also named constants, which are declared in the same way as variables, but with additional const keyword. Examples: const double pi = 3.1416;
const string message = "Hello";
55
Statements
Examples:
radius = int.Parse( Console.ReadLine() ); area = pi*radius*radius; Console.WriteLine(area);
The first two are assignments. The third one writes the value of “area” to the screen.
56
Methods (again)
We use various methods from various classes.
Console.WriteLine int.Parse
Console.ReadLine
Console and int are standard classes.
57
Example
using System; namespace Hello { class MainClass { static void Main(string[] args) { double price, money; int n; double leftover;
Console.Write("Enter candy price: "); price = double.Parse(Console.ReadLine()); Console.Write("Enter number of candies: "); n = int.Parse(Console.ReadLine()); Console.Write("Enter your money :"); money = double.Parse(Console.ReadLine());
leftover = money - price * n;
Console.WriteLine("You have {0} bahts left.", leftover);
}
}
}
58
WriteLine
Output formatting
Console.WriteLine("You have {0} bahts left.", leftover);
59
Identifiers
Symbolic names for program elements
Program name Variable name Data Type name Etc. Letters, digits, and under score (_) First character letter Can be long (63 char) Reserved words (keywords) are not allowed
60
Rules for constructing identifiers
Identifier examples
Valid examples
score, count, total score1, count99, total09 score_1, count_99, total_99 myStudentId my_student_id point&score total-number 9points
61
Invalid examples
Notes for Identifiers
Identifiers are case-sensitive
mystudentId Mystudentid MYSTUDENTID
62