[ Table of Contents ]
A loop statement is a compound statement that controls repetitious execution of a sequence of statements. There are three kinds of iteration scheme:
Every loop uses the reserved words or combinations: loop and end loop. The reserved words for or while may appear as part of the "iteration scheme," and a "loop name" may optionally be used. The reserved words exit and when may appear in the statement sequence.
General Form |
Loop_Name: -- optional iteration-scheme loop -- statement_sequence; end loop Loop_Name; |
If a Loop_Name is given at the beginning, it must appear again after end loop. The statement_sequence may include one or more exit statements, as illustrated in the first of three examples below. An exit statement can be simply "exit;" or can be "exit when <condition>" where the <condition> is a Boolean expression that evaluates to true or false.
This diagram depicts a program that illustrates two kinds of loop
statement. Two packages from the predefined environment are withed in by the Assign_Grades procedure. A separate code fragment depicts an alternative to the first loop statement. The alternative is a "while loop."
|
The first loop statement in the following code contains an exit statement within it. The "while loop" statement provides an alternative way to achieve the same functionality without an exit statement.
The two arrays defined in the declaration part are initialized using aggregate expressions.
Source Code Listing
------------------------------------------------------------------- -- This program asks the user to enter a series of numerical -- (integer) grades, computes the class average, and then computes -- and displays letter grades. ------------------------------------------------------------------- with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO, Ada.Integer_Text_IO; procedure Assign_Grades is Number_Grade : array(1..25) of Integer -- 25 is max class size := (1..25 => 0); -- array aggregate Letter_Grade : array(1..25) of Character; := (1..25 => 'U'); -- array aggregate -- U = Unassigned Total : Integer := 0; Number_Of_Students : Integer := 0; I : Integer := 0; Class_Average : Integer; Finished : Boolean := False; Response : Character; begin -- basic loop with exit -- enter number grades and -- determine Number_Of_Students loop I := I + 1; Number_Of_Students := I; Put("Enter next grade (0 to 100) "); Get(Number_Grade(I)); -- if I>25, raises exception Total := Total + Number_Grade(I); Put("Are you finished? (Y or N) "); Get(Response); New_Line if (Response = 'Y') or (Response = 'y') then Finished := True; end if; exit when Finished; end loop; Class_Average := Total/Number_Of_Students; Put("Class Average = "); Put(Class_Average); New_Line; -- for (1..N) loop -- determine and display -- letter grades for J in 1..Number_Of_Students loop if (Number_Grade(J)> 90) or (Number_Grade(J) - Class_Average > 20) then Letter_Grade(J) := 'A'; elsif (Number_Grade(J)> 80) or (Number_Grade(J) - Class_Average > 10) then Letter_Grade(J) := 'B'; elsif (Number_Grade(J)> 70) or (Class_Average - Number_Grade(J) < 11) then Letter_Grade(J) := 'C'; elsif (Number_Grade(J)> 60) or (Class_Average - Number_Grade(J) < 21) then Letter_Grade(J) := 'D'; else Letter_Grade(J) := 'F'; end if; Put("Student "); Put(J); Put(" gets "); Put(Letter_Grade(J)); Put(" based on score of "); Put(Number_Grade(J)); End_Line; end loop; end Assign_Grades; ------------------------------------------------------------------ -- while <condition> loop -- alternative first loop -- (exit not required) ------------------------------------------------------------------ while not Finished loop I := I + 1; Number_Of_Students := I; Put("Enter next grade (0 to 100) "); Get(Number_Grade(I)); -- if I>25, raises exception Total := Total + Number_Grade(I); Put("Are you finished? (Y or N) "); Get(Response); New_Line; if (Response = 'Y') or (Response = 'y') then Finished := True; end if; end loop; ------------------------------------------------------------------ |
Here is a sample output from the above program:
Enter next grade (0 to 100) 88 Are you finished? (Y or N) n Enter next grade (0 to 100) 92 Are you finished? (Y or N) n Enter next grade (0 to 100) 71 Are you finished? (Y or N) y Class Average = 83 Student 1 gets B based on score of 88 Student 2 gets A based on score of 92 Student 3 gets C based on score of 71 |
Related Topics
[ Back to top of page ]