cmp 131 introduction to computer programming violetta cavalli-sforza week 5, lecture 1 (monday)
TRANSCRIPT
CMP 131Introduction 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).
– 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.
TODAY
• Pascal Data Types– Real– Ordinal Data Types
• Integer• Character• Boolean
• Expressions again• Assignment statement again• Input/Output and formatting
Predefined data types
• Ordinal: – integer– char– boolean
• Non-ordinal: – real– strings ( added to Turbo Pascal)
Appear directly as values of constants or in program without being defined or declared.
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.
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.– div and mod are 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…
Integer Data Type
• Examples:3 div 15 = 0 3 div -15 = 015 div 3 = 5 15 div -3 = -516 div 3 = 5 16 div -3 = -517 div 3 = 5 -17 div 3 = -518 div 3 = 6 -18 div -3 = 63 mod 5 = 3 5 mod 3 = 2 -5 mod 3 = -24 mod 5 = 4 5 mod 4 = 1 -5 mod 4 = -15 mod 5 = 0 15 mod 5 = 0 -15 mod 5 = 06 mod 5 = 1 15 mod 6 = 3 -15 mod 6 = -37 mod 5 = 2 15 mod 7 = 1 -15 mod -7 = -18 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
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.
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
Comparing CharactersPROGRAM 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.
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.
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
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.
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.
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
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
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
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
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.
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
Operand Priority• Integer:
1. ( ) : Evaluate from inside out2. Unary + unary - 3. * , MOD, DIV : Evaluate from left to right4. Binary + and - : Evaluate from left to right
• Real:1. ( ) : Evaluate from inside out2. Unary + and unary - 3. * , / : Evaluate from left to right4. + , - : Evaluate from left to right
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
**
/
+-
*
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 followingconst 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
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.
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:
Mathematical formula Pascal expression
b2 - 4aca + bc + d
a = r2
a -(b +c)
B * B - 4 * A * C(A + B ) / ( C + D )
A := PI * Radius * RadiusA * (- ( B + C) )
Simple Statements
• Assignment
• Input Operations: Read, Readln
• Output Operations: Write, Writeln
• Output formatting with write/writeln
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.
Assignment Statement
• Examples:
– Valid assignmentsX := Y + Z + 2.0;Sum := Sum + Item;NewX := - X;SqYards := MetersToYards * SqMeters;
– Invalid assignmentsCh := 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 }