Previous topic | Next topic | Ada Home Page | Index

While statement

Indefinite iteration is where the set of actions is performed an unknown number of times. In Ada, a WHILE statement is usually used for indefinite iteration.

We saw an example using a while loop, in the initial introduction to Ada.

Statement form:


	while test loop
		statement(s);
	end loop;

While loops may be designed as a repeat structure, to execute at least once


	j := -1;
	while (j < 0) loop
		PUT("Enter positive j: "); GET(j); SKIP_LINE;
	end loop;
A while loop may not execute at all

	tot := 0;
	PUT("Enter j (-1 to exit): "); GET(j); SKIP_LINE;
	while (j /= -1) loop
		tot := tot + j;
		PUT("Enter j (-1 to exit): "); GET(j); SKIP_LINE;
	end loop;
	PUT("Total is "); PUT(tot); NEW_LINE;

An example program shows the use of a while loop when a user is being asked to answer 'Y' or 'N' to a question. The loop executes continually until a suitable value is typed by the user.


Rules for while statements

Style Rules for while statements


Semantics of while statement

while != if


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 13 Feb 96