Ada Programming: Understanding Visibility Rules, Loops, and Functions, Slides of Aeronautical Engineering

An introduction to ada programming, focusing on visibility rules, loops, and functions. It covers the concept of direct visibility, loop control variables, nested loops, scalar types, and functions. Examples and exercises to help readers understand these concepts.

Typology: Slides

2011/2012

Uploaded on 07/20/2012

savitha_48
savitha_48 🇮🇳

3

(1)

109 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
with Fact;
procedure Main is
procedure Hello is
begin
Ada.Text_Io.Put(“Hello”);
end Hello;
begin
for I in 1 .. Fact (4) loop
Hello;
end loop;
end Main;
function Fact (N : Integer) return Integer is
begin
if N <= 1 then
return 1;
elsereturn N * Fact (N-1);
end if;
end Fact;
Visibility Rules
determine which
declarations are visible and directly
visible at each place within a program.
and implicit declarations
immediate
use-visibility
with Fact; Main is
Hello is
begin
Ada.Text_Io.Put(“Hello”);
end Hello;
begin
for I in 1 .. Fact (4) loop
Hello;
end loop;
end Main; main.adb
function Fact (N : Integer) return Integer is
begin
if N <= 1
return 1;
return N * Fact (N-1);
end if;
end Fact; fact.adb
For Loop
•<loop_control_variable>
time through the loop.
•<lower_bound>
•<upper_bound>
The loop
variable = upper bound, then the loop terminates.
•<loop_body>
for <loop_control_variable> in <lower_bound>..<upper_bound> loop
<loop_body>
;
Loop Demo [CQ1]
procedure Loop_Demo is
begin
for Square_Integer in 1 .. 5 loop
Put (Item=>Square_Integer**2,
Width => 3);
New_Line;
;
end Loop_Demo;
•The visibility rules
The visibility rules apply to both explicit
• Direct Visibility
visibility
procedure
procedure
then
else
This is the name of the "variable that controls the loop".
The loop control variable is incremented by one each
The initial value given to the loop control variable.
The final value of the loop control variable.
body executes one more time when the loop control
The code that's executed each time through the loop.
end loop -- Square_Integer : Integer;
-- What does this do?
end loop
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Ada Programming: Understanding Visibility Rules, Loops, and Functions and more Slides Aeronautical Engineering in PDF only on Docsity!

with

Fact; procedure

Main

is

procedure

Hello

is

begin

Ada.Text_Io.Put(“Hello”); end

Hello;

begin

for

I^

in

1 .. Fact (4)

loop

Hello; end loop; end

Main;

function

Fact (N : Integer)

return

Integer

is

begin

if^

N <= 1

then return

else

return

N * Fact (N-1);

end if; end

Fact;

Visibility Rules

determine which

declarations are visible and directlyvisible at each place within a program.and implicit declarations–

immediate

use-visibility

with

Fact;

Main

is Hello

is

begin

Ada.Text_Io.Put(“Hello”); end

Hello;

begin

for

I^

in

1 .. Fact (4)

loop

Hello; end loop; end

Main;

main.adb

function

Fact (N : Integer)

return

Integer

is

begin

if^

N <= 1

return

return

N * Fact (N-1);

end if; end

Fact;

fact.adb

For Loop

• <loop_control_variable>

time through the loop.

• <lower_bound>

• <upper_bound>

The loop

variable = upper bound, then the loop terminates.

• <loop_body>

for

<

loop_control_variable

^

in

<

lower_bound

..<

upper_bound

^

loop

< loop_body

;

Loop Demo [CQ1]

procedure

Loop_Demo

is

begin

for

Square_Integer

in

loop

Put (Item=>Square_Integer**2,

Width => 3);

New_Line;

end

Loop_Demo;

• The

visibility rules

The visibility rules apply to both explicit

• Direct Visibility

visibility

procedure

procedure

then

else

This is the name of the "variable that controls the loop".The loop control variable is incremented by one eachThe initial value given to the loop control variable.The final value of the loop control variable.body

executes

one

more

time

when

the

loop

control

The code that's executed each time through the loop.

end loop

-- Square_Integer : Integer;

-- What does this do?

end loop

docsity.com

The Program Output Appears As

A, B or C? [CQ1]

A

B

C

Concept Question 1

Single Loop

A B C

Nested Loops [CQ2]

procedure

Nested_Loop_Demo

is

begin

for

I

in

loop

for

J

in

loop

Put (I);Put (" ");Put (J);Put (" = ");Put (IJ);New_Line;

end

Nested_Loop_Demo;

Concept Question 2

Nested Loops

iterations

iterations

iterations

iterations

1. The program output appears as2. The program output appears as3. The program output appears as4. I still don’t understand loops …

Outer loop-- Inner loop end loop

end loop

1. The program goes through2. The program goes through3. The program goes through4. The program goes through5. I still don’t understand nested loops

Functions

• <function_header>

• <local_variables>

• <function_body>

• <function_name>

function_header

local_variables_and_constants

begin

function_body

end

function_name

Function Header

function

(

: ; : ;... )

return

is

function

Fact

(N

Integer)

return

Integer

is

begin

if

N

then return

else

return

N

Fact

(N-1);

end

if;

end

Fact;

Procedures

• <procedure_header>

• <local_variables>

• <procedure_body>

• <procedure_name>

procedure_header

local_variables_and_constants

begin

procedure_body

end

procedure_name

Procedure Header

No Information Flow (No Parameters) procedure

is

procedure

(

... )

is with

Ada.Text_IO;

use

Ada.Text_IO;

procedure

Hello

is

begin

Put_Line (“Hello”);

end

Hello; with

Ada.Text_IO;

use

Ada.Text_IO;

procedure

Increment (X :

in out

Integer;

Y :

in out

float)

is

begin

x:= x + 1; y := y + 1.4; end

Hello;

contains the function name and parameters.variables used in the function (but nowhere else).the code the function executes.the name of the function. contains the procedure name and parameters.variables used in the procedure (but nowhere else).the code the procedure executes.the name of the procedure.

With Information Flow (With Parameters)

: ;

: ;

Procedure Calls

No Parameters ; With Parameters (

... );

with

Hello;

procedure

Main

is

begin

Hello; end

Main;

with

Increment;

procedure

Main

is

my_x

integer

my_y

float

begin

Increment(my_x,

my_y);

end

Main;

Arrays

, i 1

,..,i 2

)n

type

int_8_array

(1 .. 8)

of

Integer;

type

CUBE

(1..6, 1..6, 1..6)

of

Integer;

for

I

in

^1

loop

for

J

in

^1

N

loop

Put

(B(I,J));

Records

Rec2 := Rec1;

Rec1.my_boolean := Rec2.my_boolean;Rec1.my_integer := Rec2.my_integer;Rec1.my_Real

:= Rec2.my_real;

type My_Type_Record isrecord

my_integer : Integer;my_real

: Float;

end record;

type My_Other_Type_Record isrecord

my_integer : Integer;my_real

: Float;

end record;

Rec1 : my_type_record;Rec2 : my_other_type_record;

JK

=> ; => ,

• Access elements using Indices

– Single Dimension arrays A(I)– Two dimensional arrays A(I,J)– N dimensional array A(i

• Loops can be used to access elements.

is array

is array

N

end

loop

end

loop

my_boolean : Boolean;

my_boolean : Boolean;

• CP Review Session (NOT required)