review with packagename ; use packagename ; procedure procedurename is declaration of variables...

38
Review With packagename; Use packagename; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename;

Upload: arline-ellis

Post on 20-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Review

With packagename;Use packagename;Procedure procedurename Is declaration of variables & constants

Begin statements of program

End procedurename;

Page 2: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Chapter 3

The Basics of Ada

Page 3: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Topics Standard Types Identifiers Literals Expressions Variables & Constants Errors in Programs

Page 4: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Data Typescharacterized by… Values that can be stored as the data type Operations that can be performed on them Attributes (properties) associated with

each type ’Last ’First ’Digits ’Pos ’Val

Page 5: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Types of Data(Incomplete but a beginning)

In teg er F loa t(R ea l N os .)

N u m eric

C h arac te r S trin g

Text(N on -n u m eric )

B oo lean(L og ica l)

A ll D a ta

Page 6: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Numeric Data

Integer&

Float

Page 7: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Data type: Integer No fractional part to the number Usually stored in two bytes (16

bits) Processed faster than float Stored as exact value in memory Limited to –32,768 to +32,767 (if

stored as two bytes)

Page 8: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Storing Integer Data

Using Binary Number System – 16 bits

Page 9: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Binary Number System vs. Decimal Number System

Base 2

Uses symbols 0-1

Place value based on powers of 2

Base 10

Uses symbols 0-9

Place value based on powers of 10

Page 10: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Decimal Number System(Base 10)

103 102 101 100

4 3 0 2

4 x 101033 + 3 x 101022 + 0 x 101011 + 2 x 101000

= 4 x 1000 + 3 x 100 + 0 x 10 + 2 x 1

= 4000 + 300 + 0 + 2

= 4302

Place Value

Page 11: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Binary Number System(Base 2)

23 22 21 20

1 1 0 1 2

1 x 2233 + 1 x 2222 + 0 x 2211 + 1 x 2200

= 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1

= 8 + 4 + 0 + 1

= 1310

Place Value

Page 12: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

You try one…

Change this binary (base 2) integer to a decimal (base 10) number:

Sign of number

00000000000100012

Page 13: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Answer: (will be positive)

100012

= 1 x 24 + 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20

= 1 x 16 + 0 + 0 + 0 + 1 x 1

= 16 + 1

= +17

2024

Page 14: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Package for Integers

With ada.integer_text_io;Use ada.integer_text_io;

Declaring a variable

Count : integer;

Page 15: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Attributes of Integer Data Type Integer’first – gives smallest possible

negative integer Integer’last – gives largest possible

positive integer Way to find out how your computer

stores integers (two bytes, four bytes, etc.)

PUT(“the biggest integer is: ”);PUT(integer’last);

Page 16: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Data type: Float Always stores fractional part to the

number Usually stored in four bytes (32 bits) Processed slower than integer Stored as approximate value in

memory Stores the sign (1st bit), exponent(8

bits), and then mantissa (23 bits)…from left to right

Page 17: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Data Type: FloatStoring the number – 32 bits

00000010010100000000000000000000

Sign exponent mantissa

0 00000100 10101000000000000000000

+ 4 .10101

= + .10101 x 24 = 1010.1 = 1x23+0x22+1x21+0x20+1x2-1

= 8 + 0 + 1 + 0 + ½ = 9.5

Page 18: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Package for Float

With ada.float_text_io;Use ada.float_text_io;

Declaring a variable

Salary : float;

Page 19: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Attributes of Float Data Type

Float’digits– gives number of significant digits Float’first – gives smallest possible positive

number that can be stored Float’last – gives largest positive number that

can be stored

PUT(“the biggest float number is: ”);

PUT(float’last);

Page 20: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Text Data

Character&

String

Page 21: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Data Type: Character

Can store only one letter, digit, special symbol, or non-printable control character

Value of character put in apostrophes – ’d’

Example: to assign the exclamation point to a variable called ch

ch := ’!’

Stored in one byte 01000010 (this is a ‘B’) Set of characters has order (enumerated) – see

appendix F Can compare one character to another (<, >, =)

Page 22: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Package for Character

With text_io;Use text_io;

Declaring a variable

Ch : character;

Page 23: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Attributes of Character Data Type ’Pos – gives the ASCII code

(position) of the character in the set Example: ’A’ has position 65

’Val – gives the character that goes with a particular code Example: Put(Character’val(65));

would display the ‘A’

Page 24: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Data Type: String Used to store more than one

character (a string of characters) Must declare how many characters

are possible for the variable

Page 25: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Package for String same as character

With text_io;Use text_io;

Declaring a variable

Name : string(1..8);

Page 26: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Indexing a String (only using part of it)

Name(1..2) would only access the first two characters in the string variable Name

Page 27: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Reading text of varying length

Make sure declaration of variable allows for as large a string as needed (Name : String(1..100)

Declare an integer variable to hold the number of characters read in (N)

Use the GET_LINE(Name,N) Then use indexing to get correct string you want

PUT(“the name is ”); PUT(Name(1..N));

Page 28: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Logical Data

Boolean(True or False)

Page 29: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Data Type: Boolean Can be a statement which can be

shown true or false Example: num_of_people > 10

Can be a variable that is of type boolean Example: If Found then

PUT(“I found it!”); End if;

Page 30: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Identifiers Way of naming things in the program

Name of procedure Name of variable Reserved words (Begin, If, For…etc.)

Must begin with letter of alphabet Remaining part of identifier must be

letters, digits (0 – 9), or underline symbol Examples: num_of_2nd_place_winners

Case does not matter

Page 31: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Valid or Invalid Identifiers ?? 7_up Number1 Number_1 My Name First-Name _MyName

Page 32: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Literalsactual (constant)values used in a program…must match variable type

25 integer literal 3E4 integer literal (same as

30000) 23.0 float literal 4.1E-3 float literal (same as 0.0041) ’R’ character literal ”Joshua” string literal false boolean literal

Page 33: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Expressions…Examples Gross_pay – 0.125 * Gross_Pay -k 13 mod 5 13 rem 5 abs k float(num_of_people) K>5 (boolean expression)

Page 34: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

ExpressionsPrecedence (order) of operators

** abs not * / mod rem + - (unary – sign of number) & + - (ordinary adding and

subtraction) = /= < <= > >= in not in And or xor and then or else

-5+4*6/2**3

Page 35: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Declaring Variables Num_of_items : integer; Address : string(1..30); Symbol : character; Temp, max_temp, min_temp :

float; Found : boolean

Page 36: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Initializing variables Found : boolean := false; School : string(1..3) := “MSU”; Rate : float := 0.05; I_rate : float := Rate * 2

Page 37: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Declaring Constantscannot be changed in program

Max_num : constant Integer := 100; Max_num : constant := 100; Int_rate : constant := 0.065; Title : constant string := “Welcome to

Murray!”

Page 38: Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename

Kinds of Errors Compile-time error –broke the rules

of syntax (grammar) – detected during compilation

Run-time error – illegal values occurred – detected during execution

Logic-error – may have mixed up the sequence of statements – a true “bug”