CMP 131 Introduction to Computer Programming

1 / 28

# CMP 131 Introduction to Computer Programming - PowerPoint PPT Presentation

CMP 131 Introduction to Computer Programming. Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday). THIS WEEK. Homework #2: Turn in the non-programming part today (in class or by e-mail after class).

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## CMP 131 Introduction to Computer Programming

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1. CMP 131Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)

2. THIS WEEK • Homework #2: • Turn in the non-programming part today (in class or by e-mail after class). • Can keep working on the programming part through the March 29, but you must send it to me by March 29 midnight. This includes part b) Exercises 8 & 13. • Makeup session: • Tuesday March 27, 6:30-8pm, LAB 01 • Quiz #2 on Wednesday • Will cover material through today.

3. TODAY • Pascal Data Types • Real • Ordinal Data Types • Integer • Character • Boolean • Expressions again • Assignment statement again • Input/Output and formatting

4. Predefined data types • Ordinal: • integer • char • boolean • Non-ordinal: • real • strings ( added to Turbo Pascal)

5. Integer Data Type • A type Integer object represent only whole numbers. • Not all integer range can be represented inside a computer. • On each system, the predefined constant MaxInt is the largest possible integer. The smallest integer is -MaxInt-1 • What kinds of Objects can be Integers? • Variables • Constants • Literals Have names associated with them. They are defined (constants) or declared (variables) in the program declaration section. Appear directly as values of constants or in program without being defined or declared.

6. Operations in Integer • Arithmetic: + - * / div mod • div computes the integral part of the result of dividing the first operand by the second. • mod returns the integer remainder of the result of dividing its first operand by its second. • divandmodare integer operators only! • they expect integer operands • they return an integer result • Comparison Pperators: =, <>, <=, >=, < , > • Assignment: := • Standard Procedures: ReadLn, WriteLn, Read, Write • Standard Functions: pred, succ, others…

7. Integer Data Type • Examples: 3 div 15 = 0 3 div -15 = 0 15 div 3 = 5 15 div -3 = -5 16 div 3 = 5 16 div -3 = -5 17 div 3 = 5 -17 div 3 = -5 18 div 3 = 6 -18 div -3 = 6 3 mod 5 = 3 5 mod 3 = 2 -5 mod 3 = -2 4 mod 5 = 4 5 mod 4 = 1 -5 mod 4 = -1 5 mod 5 = 0 15 mod 5 = 0 -15 mod 5 = 0 6 mod 5 = 1 15 mod 6 = 3 -15 mod 6 = -3 7 mod 5 = 2 15 mod 7 = 1 -15 mod -7 = -1 8 mod 5 = 3 15 mod 8 = 7 15 mod 0 = ?? • ?? Means “Undefined” • For div, the sign of the result depends on the signs of the operands, just like for regular division • For mod, the sign of the result only depends on the sign of the dividend

8. Try running this program PROGRAM intdiv; VAR a, b: integer; BEGIN REPEAT write('Input a and b: '); readln(a,b); IF b <> 0 THEN writeln('DIV = ', a DIV b, ' MOD = ', a MOD b); writeln; UNTIL b = 0; END.

9. Character Data Type • Used to store any single character value. • Values: 0 to 255, map to specific characters according to the ASCII coding system. • Ex. ‘a’, ‘A’, ‘.’, ‘,’, ‘#’,’1’, ‘0’, … • Character literals must be enclosed in apostrophes. • The blank characters is written as ' '. • You don't type apostrophes around character data entered at the terminal. • Blank character is entered by pressing the space bar. • Operations: • Assignment: := • Comparison Operators: =, <>, <=, >=, < , > • Standard Procedures: read, readln, write, writeln • Standard Functions: pred, succ

10. Comparing Characters PROGRAM CharComp; VAR firstchar, secondchar: char; BEGIN write('Enter a character, without quotes > '); readln(firstchar); write('Enter another character, without quotes > '); readln(secondchar); IF firstchar = secondchar THEN writeln(firstchar,' is equal to ', secondchar) ELSE IF firstchar < secondchar THEN writeln(firstchar,' is less than ', secondchar) ELSE writeln(firstchar,' is greater than ', secondchar); readln; END.

11. Character Ordering PROGRAM CharOrd; VAR ch: char; BEGIN write('Enter a character, without quotes > '); readln(ch); writeln('The successor of ',ch,' is ',succ(ch)); writeln('The predecessor of ',ch,' is ',pred(ch)); readln; END.

12. Boolean Data Type • Values: • true (1) & false (0) • Operations: • Assignment: := • Comparison Operators: =, <> • (the others are not useful) • Logical: AND, NOT, OR (in expressions) • Standard Procedurs: • Display with write, writeln • (Not read !!!) – ‘false’ and ‘true’ are string literals, not the boolean values false and true

13. Comparing Booleans PROGRAM BoolOrd; BEGIN writeln('The successor of ',false,' is ',succ(false)); writeln('The predecessor of ',false,' is ',pred(false)); writeln('The successor of ',true,' is ',succ(true)); writeln('The predecessor of ',true,' is ',pred(true)); readln; END.

14. Boolean Ordering PROGRAM CharOrd; VAR ch: char; BEGIN write('Enter a character, without quotes > '); readln(ch); writeln('The successor of ',ch,' is ',succ(ch)); writeln('The predecessor of ',ch,' is ',pred(ch)); readln; END.

15. String Data Type • Sequence of characters enclosed in (‘) apostrophes (single quotes). • Literals Ex. : ‘Please enter a number > ‘ • Constants, Ex: CONST Border = ‘======‘ • Variable, Ex: VAR InputString : string; • Up to 255 characters. • For Turbo Pascal only • Operations: • Read • Store in memory • Compare • Display

16. Expressions • An expression is: • a single value (variable, constant, literal)E.g. MyVar, MyConst, ‘my literal’, 5 • 2 or more values are combined to produce a single value.E.g. MyVar – MyConst + 5

17. Expressions Occur • In constant definitions, but must use only constant arguments, E.g. • Three = 1 + 2; • CONST One = 1; Two = 2; Three = One + Two; • On the right hand side of an assignment statement, e.g. • Area := PI * Radius * Radius; • Sum := Sum + NewValue; • As arguments to write, writeln, and other standard and user-defined procedures and functions. • Elsewhere in different kinds of statements

18. Operands,Operators,Types • Expressions are made up of operands and operators. • Each operand in an expression has a type. • Operators expect operands of specific type(s) • Operators return a result of a specific type. • Rules govern: • priority of different types of operators and order of evaluation within the same priority class • what operand types may be combined with a specific operator • what the type of the result is

19. Expression Types • Depends on its operands. • Of type Integer only if all its operands are type integer and none of its operators is / (real division). • Mixed type expression: ex. 5.2 + 4 • Contains both Integer & Real operands. • Mixed-type assignment: • Assignment of an expression of one type to a variable of a different type. Ex. VAR r: Real; … r := 4 + 2; • Real expression can’t be assigned to a type Integer variable.

20. Expression Evaluation • Parenthesized subexpressions must be evaluated separately. • For nested parenthesized sub-expressions, the innermost sub-expression evaluated first. • Left associative rule: • Operators in the same subexpression and at the same precedence level are evaluated left to right. • / operator always yields a real value • mod and div can only be used with integers

21. Operand Priority • Integer: • ( ) : Evaluate from inside out • Unary + unary - • * , MOD, DIV : Evaluate from left to right • Binary + and - : Evaluate from left to right • Real: • () : Evaluate from inside out • Unary + and unary - • * , / : Evaluate from left to right • + , - : Evaluate from left to right

22. * / * * + - Rules for Expression Evaluation • Example: X * Y * Z + A / B - C * D ==> ((((X * Y) * Z) + (A / B)) - (C * D)) • Can also be drawn to show the sequence of evaluation • Nodes are for operators • Lines are for operands X * Y * Z + A / B - C * D

23. Self-Check • What are the results of the following expressions? 22 div 7 7 div 22 22 mod 7 7 mod 22 • What is valid & what is invalid in the following const PI = 3.14159; MaxI = 1000; var X, Y : Real; A, B, I : Integer; . . . I := A mod B; I := A mod Y; X := A / B; I := A / B; X := A / Y; I := B div 0; I := A mod 0; X := PI div Y; X := A mod (A / B); I := (MaxI - 990) div A; I := A mod (MaxI - 990); SEE TURBO PASCAL COMPILATION ERRORS

24. PROGRAM Types; CONST PI = 3.14159; MaxI = 1000; VAR X, Y : Real; A, B, I : Integer; BEGIN I := A mod B; { I := A mod Y; } X := A / B; { I := A / B; } X := A / Y; { I := B div 0; } I := A mod 0; { X := PI div Y; } { X := A mod (A / B); } I := (MaxI - 990) div A; I := A mod (MaxI - 990); END.

25. Pascal Mathematical Formulas • Rules: • Use the * operator to indicate multiplication. • Use parentheses when required to control the order of operator evaluation. • Never write two arithmetic operators in succession; they must be separated by an operand or an open parenthesis. • All assignment statements must be written in a linear form. Parentheses are often needed to separate numerator from the denominator involved in divisions. • Examples:

26. Simple Statements • Assignment • Input Operations: Read, Readln • Output Operations: Write, Writeln • Output formatting with write/writeln

27. Assignment Statement • Assignment statement: • Instruction that stores a value or a computational result in a variable • Used to perform computations • Syntax: <result> := <expression> • Example: Area := PI * Radius * Radius; • The variable specified by the result is assigned the value of the expression. • Previous value of result is destroyed. • Assignment symbol ‘:=‘ is not an algebraic equation. • Value being assigned must be assignment compatible with the variable receiving it.

28. Assignment Statement • Examples: • Valid assignments X := Y + Z + 2.0; Sum := Sum + Item; NewX := - X; SqYards := MetersToYards * SqMeters; • Invalid assignments Ch := 5; { Ch is of char type } Ch := Name; { Name is of string type } Name := True; { True is a Boolean literal } BoolVar := ‘False’; {‘ False’ is a string literal }