clear; clc; % so that we start fresh disp('A very simplified ATM program with too many assumptions'); balance = 100; % assume my account (with card 1 and pin 123456) has initial balance 100$ while 1 % this is a famous statement for looping forever 'while true' % 1. loop until user enters a card (number 1) while 1 cardID = input('Enter card (press 1) or exit (press -1): '); if cardID == 1 | cardID == -1 % only stop when the card is entered or user wants to exit break; end end if cardID == -1 % one more check to stop the entire program break; end disp('Card inserted.'); % 2. loop until user enters the correct pin (123456) while 1 PIN = input('Enter PIN (6 digits): '); if PIN == 123456 % our simple ATM has this universal PIN break; end disp('PIN incorrect, try again until correct!'); end disp('PIN correct!'); % 3. the 'transaction' :D disp(sprintf('Current balance = %d$', balance)); amount = input('Withdraw how much money?: '); if amount <= balance % for protection disp(sprintf('ATM dispense %d$!',amount)); balance = balance - amount; else disp(sprintf('Cannot dispense %d$!',amount)); end disp(sprintf('Current balance = %d$', balance)); % this simple ATM directly go back to check card mode after one transaction disp('Have a nice day =)'); disp('------------------'); % separator for each transaction end % Ah, just some exit note disp('Thank you for using this not so useful ATM.');