Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Loops, Subprograms - Programming Languages - Lecture Slides, Slides of Programming Languages

Loops, Unconditional infinite loop, While and For Loops, Exit statement, Block Statement, Declaring and Handling Exceptions, Exception Handling, Subprograms, Nesting of subprograms are key points of this lecture. Programming languages is basic subject of computer science. Its not about any specific language but almost cover all of them.

Typology: Slides

2011/2012

Uploaded on 11/10/2012

omni
omni 🇮🇳

4.6

(9)

46 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Loops
Simple Loop unconditional infinite loop
While loop
For Loop
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Loops, Subprograms - Programming Languages - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Loops

  • Simple Loop – unconditional infinite loop
  • While loop
  • For Loop

Simple Loop

loop -- Loop body goes here end loop ;

loop -- Loop body goes here if condition then exit ; end if ; end loop ;

loop -- Loop body goes here exit when condition ; end loop ;

Exit statement

• exit ;

• exit when condition;

• Can only be used in a loop

• You can use labels:

Outer : loop Inner : loopexit Inner when Done; end loop Inner; end loop Outer;

Block Statement

• Block statement can be used anywhere

declare -- declare section optional declarations begin statements exception -- exception section optional handlers end ;

Declaring and Handling Exceptions

  • Declaring an exception Error, Disaster : exception ;
  • Raising an exception raise Disaster; -- strips stack frames till a handler is found
  • Handling an exception exception when Disaster => statements

More on Exception Handling

• Anywhere we have begin-end, we can do:

begin statements exception when exception1 => statements ; when exception2 => statements ; end ;

Subprograms

Procedure My_procedure(x, y : IN OUT integer) is … function aux_func(a : Integer) return integer is Temp : float; begin … return integer(temp) * a; -- retrun temp * a is not allowed end aux_func; … begin … aux_func(x); … end My_procedure;

Type Name No return type

Parameter List – IN, OUT, IN OUT

Default mode is ‘IN’

You have to convert/cast^ Return type ‘Temp’ to an integer

Packages

• Packages are the primary structure used

for encapsulation in Ada

• Packages are used to group data and

subprograms

• Packages can be hierarchical, allowing a

structured and extensible relationship for

data and code

• All the standard Ada libraries are defined

within packages

package STACK is procedure PUSH (x : INTEGER); function POP return INTEGER; end STACK;

Package Specification

package body STACK isMAX: constant := 100; S : array (1..MAX) of INTEGER;TOP : INTEGER range 0..MAX; procedure PUSH (x : INTEGER) isbegin TOP := TOP + 1;S(TOP) := x; end PUSH; function POP return integer isTOP := TOP - 1; end POP;return S(TOP + 1); begin TOP := 0; end STACK;

Package Body

exception is raised and need^ If value of TOP>100, an to be handled