Previous topic | Ada Home Page | Index

While example

Specification

The user is to be prompted to enter 'Y' (or 'y') to indicate yes, or 'N' (or 'n') to indicate no. If either of these responses is provided, the program confirms it and terminates. Any other response results in the prompting and input being repeated.

User interface

Please enter 'Y' for yes or 'N' for no X

Please enter 'Y' for yes or 'N' for no N

You entered No.

Algorithm


	1. do init
		1.1 set response to 'x'
	2. get yes no
		2.1 while response is neither 'Y' or 'N'
			2.2 prompt for and get response
			2.3 check input
				2.3.1 convert 'y' to 'Y'
				2.3.2 convert 'n' to 'N'
	3. confirm input
		3.1 if response is 'Y'
			3.2 display yes confirmation
		   otherwise
			3.3 display no confirmation


Program

-- Fintan Culwin Sept '88 v1.0
-- program to illustrate indefinite iteration
-- b1s6p1 see text p69-70

with TEXT_IO; use  TEXT_IO;

procedure b1s6p1 is

    response : CHARACTER;      -- response from user

begin -- b1s6p1 
    -- init response
    response := 'x';  -- ensure loop runs at least once

    -- get yes no
    while ( response /= 'N' ) and ( response /= 'Y') loop
        PUT( "Please enter 'y' for yes or 'n' for no ");
        GET( response ); skip_line;

        -- check input
        if response = 'n' then response := 'N'; end if;
        if response = 'y' then response := 'Y'; end if;
    end loop;

    -- Assert: response is either 'Y' or 'N'

    -- confirm input
    if response = 'Y' then
        PUT_LINE("You entered Yes.");
    else
        PUT_LINE("You entered No.");
      end if;
end b1s6p1;

 

Note the Assert comment after the loop


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