Previous topic | Ada Home Page | Index

If-then-else example

An if-then-else statement is needed when the program specification indicates alternative actions.

Specification

A program is required which will ask the user for the amount of money (positive integers only) in a bank account. It will then ask for the amount of money (integers greater than zero) to be withdrawn. If the amount to be withdrawn is greater than the amount in the account, the program is to display a message that the transaction is refused, and the unchanged balance is displayed. If the amount of money to be withdrawn is less than or equal to the amount in the account, the transaction is accepted and the new balance in the account is displayed.

Alternative user interfaces

Enter the amount on deposit 100

Enter the withdrawal 50

Accepted. Balance is 50

Enter the amount on deposit 76

Enter the withdrawal 120

Refused! Balance is 76

Data design

NAMETYPENotes
balanceINTEGERbalance in the account
withdrawalINTEGERamount requested by user

Algorithm


1. get balance & withdrawal
	1.1 get balance
	1.2 get withdrawal
2. if withdrawal amount less than balance
  then 
	2.1 indicate transaction accepted
  else
	2.2 indicate transaction rejected


Resulting Program


-- Fintan Culwin Sept '88 v1.0
-- program to illustrate simple selection
-- b1s4p1 see text p43-44

with TEXT_IO;
use  TEXT_IO;

procedure b1s4p1 is

   package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
   use int_io;

   balance,                  -- balance in the account
   withdrawl  : integer;     -- withdrawl from the account

begin -- of b1s4p1

   -- get balance & withdrawal
   PUT ( "Enter balance of the account ");
   GET ( balance ); SKIP_LINE;
   PUT ( "Enter the withdrawal         ");
   GET ( withdrawl ); SKIP_LINE;

   -- decide upon action
   if balance >= withdrawl then
      PUT ( "Accepted. balance is ");
      PUT ( balance - withdrawl ); NEW_LINE;
   else
      PUT ( "Refused! balance is ");
      PUT ( balance ); NEW_LINE;
   end if;

end b1s4p1;


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