204111 Introduction to Computer Programming Lecture 1 Introduction

  • Slides: 62
Download presentation
204111: Introduction to Computer & Programming Lecture 1: Introduction Dept. of Computer Engineering Faculty

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 n Computers n n n Devices for performing computations at high

Computer & Program n Computers n n n 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. Program n A set of instructions for a computer to follow, written in specific programming language 2

Computer Components n n n CPU (Central Processing Unit) Primary storage (memory) Secondary storage

Computer Components n n n 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 Components Secondary Memory Main Memory HD CPU Input Devices Output Devices 4

Computer Systems n n Hardware (HW) n Actual physical machines (equipment) that make up

Computer Systems n n Hardware (HW) n Actual physical machines (equipment) that make up the computer Software (SW) n Programs written for a specific application are often called softwares. 5

Computer Languages • Computer languages categories: § Machine language § § § Binary Code

Computer Languages • Computer languages categories: § Machine language § § § Binary Code Assembly language High-Level Language n n Uses English-like language Machine independent Portable Examples: Pascal, C, C++, Java, Fortran, . . . Computer can execute only the machine language 6

High-Level Languages n Procedural Language n n n Fortran Cobol Basic C Pascal Object-Oriented

High-Level Languages n Procedural Language n n n Fortran Cobol Basic C Pascal Object-Oriented Language n C++ n Functional Language n Lisp Logic Language n Prolog n 7

The translation Process Source Program Compiler Executable Program 8

The translation Process Source Program Compiler Executable Program 8

�������� 1. �������� 2 ��������� Hardware : Microprocessor , 2. ����� Software : Window

�������� 1. �������� 2 ��������� Hardware : Microprocessor , 2. ����� Software : Window XP , n Monitor, Keyboard , , Printer , etc Linux word, Internet Explorer, Adobe Photoshop, Hard disk , Mouse Microsoft 11

Computer Components Secondary Memory Main Memory HD CPU Input Devices Output Devices 12

Computer Components Secondary Memory Main Memory HD CPU Input Devices Output Devices 12

Secondary Memory n n n n ���������������������� Thumb Drive Hard disk Floppy disk Zip

Secondary Memory n n n n ���������������������� Thumb Drive Hard disk Floppy disk Zip drive Thumb drive CD-ROM 16

I/O Devices �������� n n n ����������������� �� Keyboard Mouse Monitor Printer Speaker 18

I/O Devices �������� n n n ����������������� �� Keyboard Mouse Monitor Printer Speaker 18

Computer Components Secondary Memory Main Memory HD CPU Input Devices Output Devices 19

Computer Components Secondary Memory Main Memory HD CPU Input Devices Output Devices 19

�������� n There are 3 kind of programming languages Machine language (0’s and 1’s)

�������� n There are 3 kind of programming languages Machine language (0’s and 1’s) n Assembly language (mov, and, or, etc…) n High-level language (nearly like human word) Computer itself understands only Machine language. n n 22

The translation Process Source Program Compiler /Interpreter Executable Program 23

The translation Process Source Program Compiler /Interpreter Executable Program 23

Language translator Interpreter / Compiler class Main. Class { public static void Main(string[] args)

Language translator Interpreter / Compiler class Main. Class { public static void Main(string[] args) { Console. Write. Line("Hello World!"); } } High-level language …… main: pushl movl subl andl movl subl pushl. . …… Assembler %ebp %esp, %ebp $8, %esp $-16, %esp $0, %eax, %esp $8, %esp $. LC 0 …. 000110001110101111 000111110001 11011100001011011 …… Machine language Assembly language Hello World! _ 24

Interpreter Read high-level language , translate , and …. 0100100 then execute one command

Interpreter Read high-level language , translate , and …. 0100100 then execute one command at a time. a writeln(‘a’); Interwriteln(‘b’); 0100101 b writeln(‘c’); 0100110 …. c preter Source n 25

Compiler n n Read all program at a time. Translate into executable file (machine

Compiler n n Read all program at a time. Translate into executable file (machine language) library …. writeln(‘a’); writeln(‘b’); writeln(‘c’); …. Source Compiler a b c object file …. 0100100101. . . Linker Exe file 26

Analysis n n Example : Circle area problem Input n n Process n n

Analysis n n Example : Circle area problem Input n n Process n n circle radius (����� ( calculate circle area Output n Circle area 28

Analysis n Pseudo-code (����� ( n n n ���������������� Example Program Circle Begin read

Analysis n Pseudo-code (����� ( n n n ���������������� Example Program Circle Begin read circle radius area = Pi * radius write circle area End. 29

Implement ���������� Assembly �������� n ������������������� class Main. Class { #define PI 3. 1416

Implement ���������� Assembly �������� n ������������������� class Main. Class { #define PI 3. 1416 f n Example public static void Main(string[] args) void main() { n const double pi = 3. 1416; { int float int radius; double area; } } radius; area; scanf(“%d”, &radius); area = PI * radius; printf(“%f”, area); radius = int. Parse(Console. Read. Line()); area = pi*radius; Console. Write. Line(area); } 31

Test & Debug n There are 2 kinds of bugs class Main. Class {

Test & Debug n There are 2 kinds of bugs class Main. Class { public static void Main(string[] args) { const double pi = 3. 1416; } } class Main. Class { public static void Main(string[] args) { const double pi = 3. 1416; int radius; double area; radius = int. Parse(Console. Read. Line()); area = pi * raddius * radius; Console. Write. Line(area); radius = int. Parse(Console. Read. Line()); area = pi * radius; Console. Write. Line(area); } Syntax bug } Semantic bug 32

Programming Languages n n n The computer really only knows its own machine language

Programming Languages n n n 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# ? n provides a teaching language that highlights concepts common to all

Why C# ? n provides a teaching language that highlights concepts common to all computer languages 34

C# Program C# is an object-oriented programming language. n Everything must be in ����

C# Program C# is an object-oriented programming language. n Everything must be in ���� some class. ���� n A program is a set of �����class declarations. n class 35

A simple C# Program 36

A simple C# Program 36

Output 37

Output 37

C# Program: What did you see? n n Grouping using { } Statement ending

C# Program: What did you see? n n Grouping using { } Statement ending with ; Various keywords: using, namespace, class, public, static, void "Hello World!" 38

C# Program: class declaration n n Class name Member declarations n n n Data

C# Program: class declaration n n Class name Member declarations n n n Data member Method (or functions) class Main. Class { public static void Main(string[] args) { const double pi = 3. 1416; In the beginning of the class, we usually use only one method: Main --- which is where our program starts. int radius; double area; radius = int. Parse(Console. Read. Line()); area = pi*radius; Console. Write. Line(area); } } 39

Basic grouping { } n n n { curly brackets are used to group

Basic grouping { } n n n { curly brackets are used to group things together in C# } { they come in pairs, and { they can { be nested } } } Example: 40

Semi-colon; n n n In C#, every statement must end in a semicolon. Compiler

Semi-colon; n n n 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 Main. Class} public static void Main(string[] args} ( Console. Write. Line("Hello World; ("! { { { using System; namespace Hello{ class Main. Class {public static void Main(string[] args) { Console. Write. Line) "Hello World{{{; ("! 41

Various keywords n Various keywords help the C# compiler to understand our programs abstract

Various keywords n 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 n namespace - Programs are organized using “namespace. ” using - specifies external

Keywords n 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 n Other keywords we are using: void, public, static, etc. n At this

Keywords n Other keywords we are using: void, public, static, etc. n 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 /*

/* 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 My. Class { // 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. Write. Line("Hello!"); // chill~~ } } 45

Method Main n is a method where the program for our class starts. 46

Method Main n is a method where the program for our class starts. 46

Main declaration n Here is how we declare our Main function (inside our class):

Main declaration n 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() {. . . } void Main(string[] args) {. . . } int Main(string[] args) {. . . } 47

Inside method Main n n Variable declarations Statements public static void Main(string[] args) {

Inside method Main n n Variable declarations Statements public static void Main(string[] args) { const double pi = 3. 1416; int radius; double area; } radius = int. Parse(Console. Read. Line()); area = pi*radius; Console. Write. Line(area); 48

Variables n Example: int radius; double area; Variables are used to store “data. ”

Variables n Example: int radius; double area; Variables are used to store “data. ” n They must be declared before used. n To declare them, we must specify their “types. ” 49

Data DATA - information that must be supplied to a program before it can

Data DATA - information that must be supplied to a program before it can produce results. Basic data types: n n n int - whole numbers n 34 -90 0 112 double - numbers with fractions n 34. 345 0. 0 bool n true or false char - Computer character set string - strings 50

Data q q q Data has to be stored and referenced. Easiest way to

Data q q q 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

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 n Syntax: <type> <name>; n n Examples: int radius; double area; int

Variable declaration n Syntax: <type> <name>; n n 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 n Syntax: <variable> = <value>; n Examples: area = radius*3. 14; a =

Assignments n Syntax: <variable> = <value>; n Examples: area = radius*3. 14; a = b + c + d; 54

Constants n n n Constants can be used in programs. E. g. , 5,

Constants n n n Constants can be used in programs. E. g. , 5, 10. 4, "Hello" are constants. There 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 n Examples: radius = int. Parse( Console. Read. Line() ); area = pi*radius;

Statements n Examples: radius = int. Parse( Console. Read. Line() ); area = pi*radius; Console. Write. Line(area); n n The first two are assignments. The third one writes the value of “area” to the screen. 56

Methods (again) n We use various methods from various classes. Console. Write. Line int.

Methods (again) n We use various methods from various classes. Console. Write. Line int. Parse Console. Read. Line n Console and int are standard classes. 57

Example using System; namespace Hello } class Main. Class } static void Main(string[] args(

Example using System; namespace Hello } class Main. Class } static void Main(string[] args( } double price, money; int n; double leftover; Console. Write("Enter candy price; (" : price = double. Parse(Console. Read. Line; (() Console. Write("Enter number of candies; (" : n = int. Parse(Console. Read. Line; (() Console. Write("Enter your money; (": money = double. Parse(Console. Read. Line; (() leftover = money - price * n; { { { Console. Write. Line("You have {0} bahts left. ", leftover; ( 58

Write. Line n Output formatting Console. Write. Line("You have {0} bahts left. ", leftover;

Write. Line n Output formatting Console. Write. Line("You have {0} bahts left. ", leftover; ( 59

Identifiers n Symbolic names for program elements n n n Program name Variable name

Identifiers n Symbolic names for program elements n n n Program name Variable name Data Type name Etc. Rules for constructing identifiers n n Letters, digits, and under score (_) First character letter Can be long (63 char) Reserved words (keywords) are not allowed 60

Identifier examples n Valid examples n n n score, count, total score 1, count

Identifier examples n Valid examples n n n score, count, total score 1, count 99, total 09 score_1, count_99, total_99 my. Student. Id my_student_id Invalid examples n n n point&score total-number 9 points 61

Notes for Identifiers n Identifiers are case-sensitive n n n mystudent. Id Mystudentid MYSTUDENTID

Notes for Identifiers n Identifiers are case-sensitive n n n mystudent. Id Mystudentid MYSTUDENTID 62