Outline 204111 Computers and Programming อนันต ผลเพิ่ม Anan Phonphoem anan@cpe.ku.ac.th Intro to computer Computer components How can we load and run program? Programming languages Problem solving and programming Programming strategy Intro to Pascal Program structure Basic elements (reserved words, identifiers) 1 Categories of Computers Personal Computer, Desktop computer Notebook, Laptop, Palmtop Use by one person at a time Workstation Minicomputer Categories of Computers Microcomputer 2 Faster speed Many users at the same time CAD Workstation Unix Workstation Server Client /Server (Networks) Mainframe -> Supercomputer High computation power 3 Computer Components 4 Computer Components Secondary Memory Main Memory HD CPU 5 Input Devices Output Devices 6 1 Computer Memory 1. Main Memory 1. Main Memory 2. Secondary Memory Store information Memory Address 000 001 002 003 ... 100 -34.9 A 23 999 W Memory Cell 7 1. Main Memory 2. Secondary Memory (Storage) Memory Cell Æ Byte Æ Bit 1 Byte = 8 bits Can be divided into 2 Categories 8 RAM (Random Access Memory) ROM (Read Only Memory) Floppy disk Hard disk CD-ROM Tape Information called “file” (data file, output file, program file) 1 Kbyte = 210 = 1024 bytes 1 Mbyte = 220 = 1,048,576 bytes 1 Gbyte = 230 = 1,073,741,824 bytes 9 Main VS. Secondary Memory Computer Components Secondary Memory Main Memory 10 Much faster More expensive Volatile (RAM) Main Memory Secondary Memory HD Slower Less expensive Permanent CPU 11 Input Devices Output Devices 12 2 Central Processor Unit (CPU) How can we load and run program? Retrieve information from memory Calculate Put back results to memory Intel / AMD (Pentium / Athlon) Boot process Load OS into Memory Tell user to load and run Program “p1” User start using program Secondary Storage HW OS p1 13 14 How can a non-machine language program be executed? Programming Language Machine Language Assembly Language High-Level Language CPU can execute only the machine language 2 methods Interpretation Translation 15 16 The interpretation Process Translation Process Source Program Interpreter (on computer) USER Output Input Data Translation Phase Link Phase Execute Phase Source Program Translator Output 17 Library Object Program CPU Linker Execute Program Input Data 18 3 Translators High-Level Languages Assembler (assembly) Compiler (High-level language) Procedural Language Fortran Cobol Basic C Pascal Object-Oriented Language Functional Language Lisp Logic Language Prolog C++ 19 Problem Solving and Programming Programming Strategy Solve Problems Combine art and science Art Transform a description (word to equation) Difficult (problem is not clear, input, output) Science 20 Knowledge of problems Equation and formula 21 Introduction to Pascal Don’t rush to key in the computer Think carefully about the problem Try to solve by hand Plan your program Test by hand if possible Start programming Program Compilation Testing / Debug Documentation Maintenance 22 Pascal Program Structure Procedural programming language Developed by Prof.Niklaus Wirth (Switzerland, 1970s) Named in honor to mathematician, Blaise Pascal Most popular -> Turbo Pascal 23 Program Heading Declarations Main Routine (Program body) 24 4 Pascal Program Structure Basic Elements of Pascal Program Heading program myFirstProgram; const Declarations myStudentId = 1234567; var courseTaken: integer; begin Program Body write(‘Please Enter a number of courses’); read(‘courseTaken’); writeln(‘Student’, myStudentId, ‘ takes ’, courseTaken, ‘ courses in this semester’); end. Character set Reserved words Identifiers Constants / Variables Statement / Expression Function / Procedure 25 Reserved Words (คําสงวน) Character Set Three categories 26 Letter (A…Z, a…z) Digit (0…9) Special character (+ - * / _ = ! <> []{}) Predefined meaning and usage Cannot be redefined by the programmer Examples program begin / end const / var etc. 27 Reserved Words (คําสงวน) 28 Identifiers (คําบงชี้) program myFirstProgram; const myStudentId = 1234567; var courseTaken: integer; begin write(‘Please Enter a number of courses’); read(‘courseTaken’); writeln(‘Student’, myStudentId, ‘ takes ’, courseTaken, ‘ courses in this semester’); end. Symbolic names for program elements Rules for constructing identifiers 29 Program name Variable name Data Type name Etc. Letters, digits, and under score (_) First characterÆ letter Can be long (63 char) Reserved words are not allowed 30 5 Identifiers (คําบงชี้) Identifier examples program myFirstProgram; const myStudentId = 1234567; var courseTaken: integer; begin write(‘Please Enter a number of courses’); read(‘courseTaken’); writeln(‘Student’, myStudentId, ‘ takes ’, courseTaken, ‘ courses in this semester’); end. Valid examples score, count, total score1, count99, total09 score_1, count_99, total_99 myStudentId my_student_id Invalid examples point&score total-number 9points 31 Notes for Identifiers Standard Identifiers Identifiers are case-insensitive program myFirstProgram; const myStudentId = 1234567; var courseTaken: integer; begin write(‘Please Enter a number of courses’); read(‘courseTaken’); writeln(‘Student’, myStudentId, ‘ takes ’, courseTaken, ‘ courses in this semester’); end. mystudentId Mystudentid MYSTUDENTID Some identifiers are predefined meanings (Standard Identifiers) 32 interger, real write, writeln, read, readln 33 34 6