Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables &...

Post on 20-Jan-2016

218 views 0 download

Tags:

Transcript of Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables &...

Review

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

Begin statements of program

End procedurename;

Chapter 3

The Basics of Ada

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

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

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

Numeric Data

Integer&

Float

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)

Storing Integer Data

Using Binary Number System – 16 bits

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

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

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

You try one…

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

Sign of number

00000000000100012

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

Package for Integers

With ada.integer_text_io;Use ada.integer_text_io;

Declaring a variable

Count : integer;

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);

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

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

Package for Float

With ada.float_text_io;Use ada.float_text_io;

Declaring a variable

Salary : float;

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);

Text Data

Character&

String

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 (<, >, =)

Package for Character

With text_io;Use text_io;

Declaring a variable

Ch : character;

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’

Data Type: String Used to store more than one

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

are possible for the variable

Package for String same as character

With text_io;Use text_io;

Declaring a variable

Name : string(1..8);

Indexing a String (only using part of it)

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

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));

Logical Data

Boolean(True or False)

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;

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

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

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

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)

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

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

float; Found : boolean

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

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!”

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”