1 / 38

Review

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

Download Presentation

Review

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review Withpackagename; Usepackagename; ProcedureprocedurenameIs declaration of variables & constants Begin statements of program Endprocedurename;

  2. Chapter 3 The Basics of Ada

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

  4. 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

  5. Types of Data(Incomplete but a beginning)

  6. Numeric Data Integer & Float

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

  8. Storing Integer Data Using Binary Number System – 16 bits

  9. 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 Binary Number System vs. Decimal Number System

  10. Decimal Number System(Base 10) Place Value 4 x 103 + 3 x 102+ 0 x 101 + 2 x 100 = 4 x 1000 + 3 x 100 + 0 x 10 + 2 x 1 = 4000 + 300 + 0 + 2 = 4302

  11. Binary Number System(Base 2) Place Value 1 x 23 + 1 x 22+ 0 x 21 + 1 x 20 = 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1 = 8 + 4 + 0 + 1 = 1310

  12. You try one… Change this binary (base 2) integer to a decimal (base 10) number: Sign of number 00000000000100012

  13. Answer: (will be positive) 20 24 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

  14. Package for Integers With ada.integer_text_io; Use ada.integer_text_io; Declaring a variable Count : integer;

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

  16. 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

  17. 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

  18. Package for Float With ada.float_text_io; Use ada.float_text_io; Declaring a variable Salary : float;

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

  20. Text Data Character & String

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

  22. Package for Character With text_io; Use text_io; Declaring a variable Ch : character;

  23. 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’

  24. Data Type: String • Used to store more than one character (a string of characters) • Must declare how many characters are possible for the variable

  25. Package for String same as character With text_io; Use text_io; Declaring a variable Name : string(1..8);

  26. Indexing a String(only using part of it) Name(1..2) would only access the first two characters in the string variable Name

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

  28. Logical Data Boolean (True or False)

  29. 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;

  30. 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

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

  32. 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

  33. 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)

  34. Expressions Precedence (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

  35. Declaring Variables • Num_of_items : integer; • Address : string(1..30); • Symbol : character; • Temp, max_temp, min_temp : float; • Found : boolean

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

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

  38. 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”

More Related