Ada LOOP statement allows for repetition of a sequence of statements Three forms of a LOOP statement FOR loop_specification LOOP...

Similar documents
Introduction to Computers and Programming

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

Different kinds of resources provided by a package

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Introduction to Computers and Programming

Chapter 6. Data Types

COP4020 Programming Assignment 1 - Spring 2011

Loops / Repetition Statements

CS110D: PROGRAMMING LANGUAGE I

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

Aerospace Software Engineering

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Lecture 7 Tao Wang 1

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Repetition Structures

Lab 09: Advanced SQL

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

Loops. CSE 114, Computer Science 1 Stony Brook University

Repetition CSC 121 Fall 2014 Howard Rosenthal

ECE 122. Engineering Problem Solving with Java

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

CSCI 1061U Programming Workshop 2. C++ Basics

Switch, Enumerations, Exception Handling, Recursion. Engineering 1D04, Teaching Session 12

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

G Programming Languages - Fall 2012

STUDENT LESSON A12 Iterations

ADA95 Tutorial Part 2

Repetition and Loop Statements Chapter 5

Motivation. Each distinct type needs its own I/O library INTEGER FLOAT CHARACTER BOOLEAN. Using pure numbers, we can write nonsense:

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

JVM (java) compiler. A Java program is either a library of static methods (functions) or a data type definition

Loops / Repetition Statements

Self-test Programming Fundamentals

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Chapter 14. Exception Handling and Event Handling 异常处理和事件处理. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Ada A Crash Course. Peter Chapin Vermont Technical College. Generated: July 27,

G Programming Languages - Fall 2012

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

CSCE 314 Programming Languages

Fundamentals of Programming Session 13

Chapter 6. Data Types ISBN

Chapter 14. Exception Handling and Event Handling ISBN

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Programming in Haskell Aug-Nov 2015

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

UNIT 3

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Introduction to Computers and Programming. Recap

Chapter 4: Making Decisions

Chapter 4: Making Decisions

FORM 2 (Please put your name and form # on the scantron!!!!)

CS1 Lecture 3 Jan. 22, 2018

Ch. 7: Control Structures

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture C11: FOR, WHILE and General Loops

12 CREATING NEW TYPES

IT 1033: Fundamentals of Programming Loops

PASCAL. PASCAL, like BASIC, is a computer language. However, PASCAL, unlike BASIC, is a Blocked Structured Language (BASIC is known as unstructured).

Our Strategy for Learning Fortran 90

Quiz Determine the output of the following program:

Expressions vs statements

Introduction to C Final Review Chapters 1-6 & 13

C++ Programming: From Problem Analysis to Program Design, Third Edition

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

(Refer Slide Time: 00:26)

Information Science 1

Flow Control. CSC215 Lecture

Maintaining the Central Management System Database

Chapter 6. Data Types ISBN

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Information Science 1

Industrial Programming

Count increments by 20 when I is even and decrements by 5 when I is odd.

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

Decision Making in C

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

COMP 202 Java in one week

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

G Programming Languages - Fall 2012

Score score < score < score < 65 Score < 50

APPM 2460: Week Three For, While and If s

Prepared by: Shraddha Modi

Repeating Instructions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

COMP 202. Java in one week

PLD Semester Exam Study Guide Dec. 2018

The for Loop. Lesson 11

Arrays. Systems Programming Concepts

Transcription:

! "# $ % Ada statement allows for repetition of a sequence of statements Three forms of a statement FOR loop_specification WHILE condition FOR loop is used when executing a specific number of iterations with a loop parameter taking in turn all values of a discrete range S, M : Integer := 0; FOR Loop Implementation FOR N IN 1..3 Text_IO.Put( Enter Test Score: ); Integer_Text_Io.Get(M); S = S + M; FUNCTION Factorial (N: Positive) RETURN Positive IS M: Positive := 1; FOR I IN 1..N M := M * I; RETURN M; END Factorial; N := 3; FOR I IN REVERSE 1..N Integer_Text_Io.Put(Item => I, Width => 5); Integer_Text_Io.Put(Item => N, Width => 5); N := 10; Integer_Text_Io.Put(Item => N, Width => 5); -- Sample output 3 3 2 10 1 10 10 Loops can be nested as in example below: N := 3; FOR I IN 1..N FOR J IN 1..I Text_Io.Put('*'); -- Sample output * I=1; J=1..1 ** I=2; J=1..2 *** I=3; J=1..3

FOR loops presented thus far have all had integers as their counter. Technically, FOR loop counters can be any discrete type SUBTYPE can be used to specify a range of values in a FOR loop TYPE Days IS (Mon, Tue, Wed, Thu, Fri, Sat, Sun); SUBTYPE Weekdays IS Days RANGE Mon.. Fri; FOR Today IN Days FOR Today IN Weekdays -- Loop through whole week -- Loop through weekdays FOR Today IN REVERSE Weekdays -- Loop through weekdays in reverse SUBTYPE can also be used to specify a range of values for a membership test within an IF statement TYPE Subframe_Data IS (SF1, SF2, SF3, SF4, SF5); SUBTYPE Almanac_Data IS Subframe_Data RANGE SF4.. SF5; Current_Subframe : Subframe_Data; -- Current_Subframe set by data from tracking loop IF Current_Subframe IN Almanac_Data THEN Store_Almanac_Data; END IF; FOR loops are quite useful but have limitations: Counter limited to discrete range Count up or down by 1 ('SUCC or 'PRED) General statement can be used to repeat a sequence indefinitely cosh x = 1 + x 2 /2! + x 4 /4! + x 6 /6! + Cosh_X, Term : Float := 1.0; X : Float := 2.0; I : Integer := 0; END Exiting the general loop requires an EXIT statement One approach IF Term < 1.0e-6 THEN EXIT; END IF; Another approach is to replace IF with WHEN EXIT WHEN (Term < 1.0e-6); Can exit loops when nested: Outer: Inner: EXIT Inner WHEN condition; END Inner; END Outer; Search: FOR I IN 1..N FOR J IN 1..M IF condition THEN I_VALUE := I; J_VALUE := J; EXIT Search; END IF; END Search;

If EXIT condition appears at the top of the loop, consider a WHILE statement WHILE Term >= 1.0e-6 Condition for WHILE statement is a compliment/opposite of that used in the EXIT statement A procedure encapsulates an activity A procedure implements an algorithm Procedure is called as a statement Procedures differs from a function in that Procedure starts with reserved word PROCEDURE Procedure name must be an identifier Procedure does not return a result Parameters, if present, may be of three different modes: in out in out Procedure specification tells you about the interface to the procedure Procedure body implements the algorithm! An in mode parameter can not be modified in a procedure Acts as a constant Any attempt to modify an in parameter will be caught by compiler and flagged as an error The value of an in parameter in a procedure call may be an expression or constant An out mode parameter is used for generated values The value of the actual parameter in the procedure call does not matter The actual parameter must be a variable The parameter must be set before it is read in the procedure An in out mode parameter is used when a procedure will modify the parameter The procedure may use the parameter assuming it has a valid value The parameter may be assigned a new value PROCEDURE proc_name (formal_parameters); PROCEDURE proc_name (formal_parameters) IS local declarations statement sequence END proc_name;

" # PROCEDURE Add(A, B : IN Integer; C : OUT Integer); PROCEDURE Add(A, B : IN Integer; C : OUT Integer) IS C := A + B; END Add; P, Q : Integer; Add(A => 2 + P, B => 37, C => Q); Add(2+P, 37, Q); PROCEDURE Increment(X : IN OUT INTEGER) IS X := X + 1; END Increment; I : Integer; Increment(X => I); ##$ %$ Packages used to encapsulate Package used to control access to resources gives the interface to the package Syntax: PACKAGE pack_name IS list of specifications of resources provided by package END pack_name; PACKAGE BODY pack_name IS sequence of function and procedure bodies that implement the resources identified in the package specification sequence of initialization statements, if any END pack_name; %$ #&# #' (#$) %$ PACKAGE Gps_Rcvr IS PROCEDURE Put_Rcvr_Ant(X, Y, Z : IN FLOAT); FUNCTION Is_Ant_Installed RETURN Boolean; END Gps_Rcvr; with Gps_Rcvr; -- specify packages we depend on use Gps_Rcvr; PACKAGE BODY Gps_Rcvr IS Loc_X, Loc_Y, Loc_Z : FLOAT := 0.0; Location_Set : Boolean := FALSE; PROCEDURE Put_Rcvr_Ant(X, Y, Z : IN FLOAT) IS Location_Set := TRUE; Loc_X := X; Loc_Y := Y; Loc_Z := Z; FUNCTION Is_Ant_Installed RETURN Boolean IS RETURN Location_Set; END Is_Installed; END Gps_Rcvr; procedure Initialize is begin -- If the receiver is not installed, then if not Is_Ant_Installed then end; Put_Rcvr_Ant(3.0, 4.0, 5.0); -- In meters from c.g. end if;

##$ Exceptions are used to tell the programmer that an error condition has occurred Examples: Value out of range of the type or subtype I/O error (such as trying to read a non-numeric character as a number Programmer defined exceptions Exception handlers allow the programmer to attempt to fix the problem and continue execution Exception handlers are associated with blocks sequence of statements -- Only statements executed if no exception -- If exception occurs, one of following -- handlers is executed WHEN exception_name 1 => -- handler for exception_name 1 sequence of statements 1. WHEN exception_name N => -- handler for exception_name N sequence of statements N &# # Common, predefined exceptions include: Constraint_Error Generally raised when storing a value in a variable that is out of range for the variable value exceeds variable type or subtype Program_Error Attempt to violate a control structure in some way such as running into the end of a function Storage_Error Occurs when the program runs out of storage space, perhaps through a recursive invocation of a function or procedure Ada.Text_IO.Data_Error Attempt to read a value which is invalid for the variable being read #"#*' # A trivial example: TYPE Days IS (Mon, Tue, Wed, Thu, Fri, Sat, Sun); FUNCTION Tomorrow(Day : IN Days) RETURN Days IS RETURN Days SUCC(Day); WHEN Constraint_Error => RETURN Days First; END Tomorrow; Example code is valid Ada and will work as expected Example is bad because the occurrence of the exception is not a rare condition ' WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PROCEDURE Get_Time_From_User IS SUBTYPE Time_T IS Integer RANGE 0..60*60*24; User_Time : Time_T; -- start block for exception handler Ada.Text_IO.Put(Item => Enter UTC time (seconds) of day between ); Ada.Integer_Text_IO.Put(Item => Time_T First, Width => 0); Ada.Text_IO.Put(Item => and ); Ada.Integer_Text_IO.Put(Item => Time_T Last, Width => 0); Ada.Integer_Text_IO.Put(Item => : ); Ada.Integer_Text_IO.Get(Item => User_Time); EXIT WHEN Constraint_Error => Ada.Text_IO.Put_Line(Item => Time out of range. Try again ); WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put_line(Item => Value not an integer. Try again ); END -- end block for exception handler END SAMPLE RUN Enter UTC time (seconds) of day between 0 and 86400: -90 Time out of range. Try again Enter UTC time (seconds) of day between 0 and 86400: asd Value not an integer. Try again Enter UTC time (seconds) of day between 0 and 86400: 2456