Bài giảng Nhập môn lập trình - Tính toán cơ bản

pdf 32 trang hapham 40
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Nhập môn lập trình - Tính toán cơ bản", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_nhap_mon_lap_trinh_tinh_toan_co_ban.pdf

Nội dung text: Bài giảng Nhập môn lập trình - Tính toán cơ bản

  1. NHẬP MÔN LẬP TRÌNH TÍNH TOÁN CƠ BẢN Variables Expressions
  2. NHẬP MÔN LẬP TRÌNH MỤC TIÊU • Hiểu kiểu dữ liệu (data type) là gì • Khai báo hằng (constants) và biến (variables) của chương trình Basic Computations 2
  3. NHẬP MÔN LẬP TRÌNH NỘI DUNG • Biến và kiểu dữ liệu • Biểu thức – Data Types – Số học – Integral Types – Quan hệ – Floating-Point Types – Logical – Declarations – Phép gán – Mixing Data Types – Casting Basic Computations 3
  4. NHẬP MÔN LẬP TRÌNH Review • Computer program: A set of instructions that computer hardware will execute. • Issues for a program/software: Usability, Correctness, Maintainability, Portability • Computer software: A set of related programs • Steps to develop a software: Requirement collecting, Analysis, Design, Implementing, Testing, Deploying, Maintaining • Data: Specific values that describe something • Information: Mean of data • Fundamental Data Units: Bit, Nibble, Byte, KB, MB, GB, TB • Data Representation: Number systems: 2, 10, 8, 16 • Program Instructions: • Programming Languages: Machine language, Assembly, High-level languages Basic Computations 4
  5. NHẬP MÔN LẬP TRÌNH 1- Variables and Data Types Biến là một tên tham chiếu đến một vị trí trong bộ nhớ (address) 0000 1001 1100 0011 Chứa dữ liệu dạng nhị phân c Khi chương trình biên dịch, trình biên dịch sẽ xác định vị trí mà biến được phân bổ. b Questions: a (1) Nó ở đâu? It’s Address (2) Nó chiếm bao nhiêu byte nhớ? Data type Basic Computations 5
  6. NHẬP MÔN LẬP TRÌNH Variables and Data Types C has 4 primitive data types: Type Length Range int Word -32,768 to 32,767 (16 bit) (length of CPU -2,147,483,648 to 2,147,483,647 (32 bit) register) char byte -128 to 127 float 4 bytes 3,4 * 10-38 to 3,4 * 1038 double 8 bytes 1,7 * 10-308 to 1,7 * 10308 Basic Computations 6
  7. NHẬP MÔN LẬP TRÌNH Variables and Data Types c:2293623 ‘A’ Các biến được lưu trữ ở đâu 1 và chiếm bao nhiêu? i:2293616 l:2293612 1000 f:2293608 0.5 12.809 d:2293600 The operator & will get the address of a variable or code. The operator sizeof(var/type) return the size (number of byte) occupied by a variable/type Basic Computations 7
  8. NHẬP MÔN LẬP TRÌNH Variables and Data Types The ASCII table for characters Basic Computations 8
  9. NHẬP MÔN LẬP TRÌNH Variables and Data Types Exercises: • What is the ASCII encoding for '0' ___ 'a' ___ 'A' ___ • What is the EBCDIC encoding for '0' ___ 'a' ___ 'A' ___ • Convert the following binary notation to an ASCII character: 0110 1101 ___ 0100 1101 ___ • Convert the following decimal notation to an EBCDIC character: 199 ___ 35 ___ Basic Computations 9
  10. NHẬP MÔN LẬP TRÌNH Variables and Data Types Khai báo biến trong C: data_type identifier [= initial value]; • Ví dụ: char section; int numberOfClasses; double cashFare = 2.25; Quy định đặt tên: Tên chỉ 01 từ – Không phải là từ dành riêng cho C – Tên không dài hơn 31 ký tự Letter or Letters/digits/ ‘_’ ‘_’ Basic Computations 10
  11. NHẬP MÔN LẬP TRÌNH Variables and Data Types Exercises: Which of the following is an invalid identifier? whale giraffe's camel_back 4me2 _how_do_you_do senecac.on.ca digt3 register • Select a descriptive identifier for and write a complete declaration for: – A shelf of books___ – A cash register___ – A part_time student___ – A group of programs___ Basic Computations 11
  12. NHẬP MÔN LẬP TRÌNH Variables and Data Types Một số thao tác trên biến – Gán 1 giá trị cho biến – Gán giá trị của một biến khác cho biến, – Xuất giá trị của biến – Nhập giá trị cho một biến vào vị trị trong bộ nhớ. constant AnotherVar anotherVar Monitor Keyboard variable File File Netwwork Network Basic Computations 12
  13. NHẬP MÔN LẬP TRÌNH Questions as Summary • What is a variable? • What is a data type? • The size of the int data type is . Bytes. • Chọn khai báo sai: int n=10; char c1, c2=‘A’; int m=19; k=2; char c3; int t; float f1; f2=5.1; Basic Computations 13
  14. NHẬP MÔN LẬP TRÌNH 2- Literals • Constant values are specified directly in the source code. • They can be – Character literals (constant characters) – String literals(constant strings) – Number literals (constant numbers) Basic Computations 14
  15. NHẬP MÔN LẬP TRÌNH Literals: ký tự, chuỗi ký tự 4 cách biểu diễn cho chữ cái: • 04 cách: – Sử dụng dấu nháy đơn- ví dụ 'A', – Mã thập phân ASCII cho ký tự: 65 cho 'A‘ – Mã bát phân ASCII cho ký tự: 0101 cho'A', – Mã thập lục phân ASCII cho ký tự: 0x41 cho'A', Assign value to a variable: The operator = Basic Computations 15
  16. NHẬP MÔN LẬP TRÌNH Literals: Escape Sequences • Pre-defined literals for special actions: Basic Computations 16
  17. NHẬP MÔN LẬP TRÌNH Literals: Escape Sequences Error! Why? Modify then run it Change \ to \\ then run it Basic Computations 17
  18. NHẬP MÔN LẬP TRÌNH 3- Named Constants • Use the pre-processor (pre-compiled directive) #define or the keyword const Compiler will allocate memory location for constants that are declared using the keyword const Basic Computations 18
  19. NHẬP MÔN LẬP TRÌNH Input/Output Variables Chuyển đổi Basic Computations 19
  20. NHẬP MÔN LẬP TRÌNH Input/Output Variables 4210784 n main 4199056 2293620 m Format string scanf( “%d%d”, &n, &m) scanf( “%d%d”, 4210784, 2293620) means that get keys pressed then change them to decimal integers and store them to memory locations 4210784, 2293620. Basic Computations 20
  21. NHẬP MÔN LẬP TRÌNH Input/Output Variables Nhập giá trị vào một biến: scanf (“input format”, &var1, &var2, ) Xuất giá trị của biến ra màn hình: printf (“output format”, var1, var2, ) The function scanf receive the BLANK or ENTER KEYS as separators. Format string Data holders Basic Computations 21
  22. NHẬP MÔN LẬP TRÌNH Questions • Explain means of parameters of the scanf( ) and the printf( ) functions. • Use words “left” and “right”. The assignment x=y; will copy the value in the side to the Side. Basic Computations 22
  23. NHẬP MÔN LẬP TRÌNH Exercises 1- Develop a C program in which 2 integers, 2 float numbers and 2 double numbers are declared. Ask user for values of them then print out values of them. 2- Run the following program: Why user do not have a chance to stroke the ENTER key before the program terminate? Modify and re-run: getchar(); getchar(); Basic Computations 23
  24. NHẬP MÔN LẬP TRÌNH 5- Biểu thức - Expressions • Expression là một kết hợp hợp lệ các hằng số, các biến, toán tử và các hàm và trả về một kết quả. • Ví dụ: . 32-x+y/6 16.5 + 4/sqrt(15) * 17 – 8 . 45 > 5*x y = 17 + 6*5/9 –z*z Basic Computations 24
  25. NHẬP MÔN LẬP TRÌNH Expressions: Arithmetic Operators Op. Syntax Description Example + +x leaves the variable, constant or y = +x ;  y = x; expression unchanged - -x reverses the sign of the variable y= -x; + - x+y x-y Add/substract values of two z= x+y; t = x-y; operands * / x*y x/y Multiplies values of two operands z= x-y; Get the quotient of a division z = 10/3; 3 z = 10.0/3; 3.3333333 % x%y Get remainder of a integral division 17%3 2 15.0 % 3 ERROR ++ ++x x Increase/decrease the value of a Demo in the next slide. x++ x variable (prefix/postfix operators) Basic Computations 25
  26. NHẬP MÔN LẬP TRÌNH Expressions: Arith. Operators Explain yourself the output Basic Computations 26
  27. NHẬP MÔN LẬP TRÌNH Expressions: Arith. Operators ? Explain yourself the output Basic Computations 27
  28. NHẬP MÔN LẬP TRÌNH Expressions: Relational Operators • For comparisional operators. • = > != • Return 1: true/ 0: false Basic Computations 28
  29. NHẬP MÔN LẬP TRÌNH Expressions: Logical Operators • Operator for association of conditions • && (and), || (or) , ! (not) • Return 1: true, 0: false Basic Computations 29
  30. NHẬP MÔN LẬP TRÌNH Expressions: Assignments Operators • Variable = expression • Shorthand assignments: Basic Computations 30
  31. NHẬP MÔN LẬP TRÌNH Expressions: Mixing Data Types • Explicit Casting Chúng ta có thể tạm thời thay đổi kiểu dữ liệu của bất kỳ toán hạng nào trong bất kỳ biểu thức nào để có được kết quả của một kiểu dữ liệu nhất định. Basic Computations 31
  32. NHẬP MÔN LẬP TRÌNH Thank You Basic Computations 32