Docsity

Ada Programming: Input, Output, and Control Structures, Slides of Aeronautical Engineering

Toggle sidebar
bg1
Recap (2/3)
Assignment statement Computes the product of
Distance := Speed * Time; Speed and Time and
assigns it to Distance
Input Statements Enters data into the
Ada.Text_Io.Get character variable Initial
(Item =>Initial);
Input Statements … into the integer
Ada.Integer_Text_Io.Get variable Age
(Item => Age);
Input Statements … into the float variable
Ada.Float_Text_Io.Get PayRate
(Item => PayRate);
Recap (3/3)
Output Statements Displays the value of
Ada.Text_Io.Put (Item =>Initial); the character variable
Initial
Output Statements
Ada.Integer_Text_Io.Put
(Item =>HowMany, Width=>3);
… integer variable
HowMany, using five
columns on the display
Output Statements
Ada.Float_Text_Io.Put
(Item => GrossPay,
Fore => 4,
Aft => 2,
Exp => 0);
… float variable
GrossPay using four
columns before the
decimal point and two
columns after the
decimal point
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Ada Programming: Input, Output, and Control Structures and more Slides Aeronautical Engineering in PDF only on Docsity!

Recap (2/3)

Assignment statement Computes the product of Distance := Speed * Time; (^) Speed and Time and

assigns it to Distance

Input Statements Enters data into the Ada.Text_Io.Get (^) character variable Initial (Item =>Initial);

Input Statements … into the integer Ada. Integer _Text_Io.Get (^) variable Age (Item => Age);

Input Statements … into the float variable Ada. Float _Text_Io.Get (^) PayRate (Item => PayRate);

Recap (3/3)

Output Statements Displays the value of Ada.Text_Io.Put (Item =>Initial); (^) the character variable

Initial

Output Statements Ada. Integer _Text_Io.Put (Item =>HowMany, Width=>3);

… integer variable HowMany, using five columns on the display

Output Statements Ada. Float _Text_Io.Put (Item => GrossPay, Fore => 4, Aft => 2, Exp => 0);

… float variable GrossPay using four columns before the decimal point and two columns after the decimal point

Data types

String type

characters as a single unit of data

Max_Str_Length : constant := 26; Alphabet, Response:String(1..Max_Str_Length);

String Operations

Alphabet := “abcdefghijklmnopqrstuvwxyz” Response := Alphabet;

Alphabet(1..3) & Alphabet(26..26)

Alphabet & “.”);

• Assignment

Put(Item => “The alphabet is “ &

• Used when representing a sequence of

  • How many characters?
  • String (1 .. Maxlen);
  • Example:

• Concatenation (&)

Control Structures

Selection statements

statements

  • if-then , when a single action might be done
  • if-then-else , to decide between two possible actions
  • if-then-elsif , to decide between multiple actions , also for deciding between multiple actions

if-then Statements

  • statement_before; if test then statement(s)_1; ; statement_after; (^) statement_before

test

statement(s)_

statement_after

true

false

end if

• Ada provides two types of selection

  • IF statements
  • Case statements

• Statement form

• Statement semantics

if-then-else Statements

  • statement_before; if test then statement(s)_1; else statement(s)_2; ; statement_after; statement_before

test

statement(s)_

statement_after

false true

statement(s)_

Multiple Selections

  • statement_before; if test_1 then statement(s)_1 ; elsif test_2 then statement(s)_2; else statement(s)_3; ; statement_after;

end if

end if

• Statement form

• Statement semantics

• Statement form

if_then_elsif Example (2/5)

• Decision table

  • A multiple alternative if may often be summarized by a decision table listing the alternatives

Balance after Action withdrawal

= 0 Accept withdrawal

= -50 and < 0 Overdraft

< -50 Refuse withdrawal

if_then_elsif Example (3/5)

Enter the withdrawal 50 Accepted. Balance is 50

76 Enter the withdrawal 150 Refused! Balance is 76

50 Enter the withdrawal 75 Overdraft! Balance is -

Enter balance of the account

Enter balance of the account

• Alternative user interfaces

  • Enter balance of the account

if_then_elsif Example (4/5)

  1. Get balance and withdrawal

  2. Calculate resulting balance

then

else if new balance between zero and overdraft limit

else

Algorithm

If new balance is >= zero

  1. Get balance
  2. Get withdrawal
  3. New balance = old balance – withdrawal
  4. Indicate transaction accepted
  5. Indicate overdraft is used
  6. Indicate transaction rejected

if_then_elsif Example (5/5)

• Data design

NAME TYPE Notes

Overdraft_Limit Integer -50 (for ease of change)

Zero Integer 0 (for readability only)

Balance Integer Balance in the account

Withdrawal Integer Amount requested by user

Resulting_Balance Integer Balance after withdrawal

Truth Tables

• Nested if statements

  • if test_1 then if test_2 then statement(s)_1; else statement(s)_2; end if ; else if test_3 then statement(s)_3; else statement(s)_4; end if ; end if ;

test_1 test_2 test_3 (^) s_1 s_2 s_3 s_

F F F * F F T * F T F * F T T * T F F * T F T * T T F * T T T *

Control Structures

Loop Statements

  • Definite iteration is where the set of actions is performed a known number of times. The number might be determined by the program specification, or it might not be known until

the iteration.

  • FOR statement for definite iteration.
  • Indefinite iteration is where the set of actions is performed a unknown number of

execution of the loop.

  • WHILE statement and general LOOP statement for indefinite iteration.

the program is executing, just before starting

Ada provides the

times. The number is determined during

Ada provides the

General Loop Statements

• loop

statements_1;

test;

statements_2;

statements_

exit condition satisfied ?

true

false statements_

CQ

For the given input, which way will the robot

behave?

1.Go back once and turn left

2.Turn right twice

3.Go back twice the distance and turn right

exit when

end loop

Hexadecimal

bin hex dec 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 … 1001 9 9 1010 A 10 1011 B 11 … 1111 F 15

Main Memory

units called: Cells

Byte

• Main memory arranged in manageable

  • Typically 8 bits:

Memory Cells

name, called its address

accessed in random

order

write

(^10100110 01111010 10100110 11010110 )

• Each cell is assigned a unique

• Stored data can be

• Read/

Cell 0 Cell 1 Cell 2 Cell 3 Cell 4 Cell 5 Cell 6 10111010 10010101

Little/Big-Endian

Address Big-Endian repr. of 1025

Little-Endian repr. of 1025 00 0000 0000 0000 0001 01 0000 0000 0000 0100

02 0000 0100 0000 0000 03 0000 0001 0000 0000

ASCII

0 1 2 3 4 5 6 7 8 9

10 11 12 13 14 15

0 1 2 3 4 5 6 7 8 9 A B C D E F NUL SOH STX ETX EOT ENQ ACK BEL BS TAB LF VT FF CR SO SI

ASCII Hex Symbol 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F

0 1 2 3 4 5 6 7 8 9 : ; < = >?

ASCII Hex Symbol 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F

` a b c d e f g h i j k l m n o

ASCII Hex Symbol

… …

Numeric Values

2510 using ASCII:

3 7 5

1 0 1 1

One

Eight One

25 24 23 22 21 20 32 16 8 4 2 1 1 0 1 1 0 1 32 + 0 + 8 + 4 + 0 + 1 = 45

• Storing the value of

• Binary notation:

Representation

Position's quantity

Representation

Position's quantity

Hundred

Ten

Four Two

Base ten system

Base two system

  1. Binary place
  2. Position's quantity
  3. Example binary pattern
  4. Total (2. x 3.)

Finding Binary Representation of

Large Values

  1. Divide the value by 2 and record the remainder
  2. As long as the quotient obtained is not 0, continue to divide the newest quotient by 2 and record the remainder
  3. Now that a quotient of 0 has been obtained, the binary representation of the original value consists of the remainders listed from right to left in the order they were recorded

0 R 1

1 R 1

0 R 0

6 R 1