How to write a Pseudocode?
Sometimes we need to explain how it works to other engineer, custormer or project manager.
But most of them is not expert to our area. How do we do explain the logic to them?
There is three way to explain the logic
- Natural language
- Flow chart
- Pseudo-code
Pseudo is a prefix that represent “not genuine” “imitating”.
Pseudo-code is not actual code but easy to write without programming background and knowledge.
Actually there is no standard syntax for writing the psuedocode, Pseudo-code is not an executable program,
it is methodology to explain the logic to others. Flow charts is graphical method of Pseudocode.
Even though it is not a actual code. We need to think about a few rule to communicate with others.
For example, if we are trying to get sum of numbers from 1 to 100.
It is simple so we can make it as actual code easily like this.
int sum = 0;
int number = 1;
do {
sum = sum + number;
number++;
} while(number != 101);
printf("the result is [%d]", sum);
We can express this logic as pseudo-code like this.
PROCEDURE SUM_NUMBER
BEGIN
SET SUM TO 0
SET NUMBER TO 1
WHILE NUMBER < 101
COMPUTE SUM AS SUM + NUMBER
INCREMENT NUMBER
ENDWHILE
PRINT SUM
END
If we need to add some logic inside of repeat, we can use the tap to make it clear.
PROCEDURE SUM_NUMBER
BEGIN
SET SUM TO 0
SET NUMBER TO 1
WHILE NUMBER < 101
COMPUTE SUM AS SUM + NUMBER
IF SUM > 3000 THEN
PRINT TEXT TO "I love you over 3000"
ENDIF
INCREMENT NUMBER
ENDWHILE
PRINT SUM
END
For practice, let’s think about ATM.
we offer three services to customer.
- checkBalance
- withdrawal
- deposit
Before we offer services we need to heck ATM machine status, card inserted and PIN number to validate the card.
- checkATMWorks
- checkInsertCard
- checkPIN
- services
PROCEDURE ATM_MAIN
BEGIN
WHILE
IF checkATMWorks is disabled
PRINT TEXT TO "ATM IS NOT AVAILABLE"
ELSE
IF checkInsertCard is true
call checkPIN
ELSE
PRINT TEXT TO "INSERT CARD"
ENDIF
ENDIF
ENDWHILE
END
Machine must be working and Card should be inserted to validate PIN.
PROCEDURE checkPIN
BEGIN
SET MAXPIN TO 3
SET PINCOUNT TO 0
STORE THE CardData FROM ReadCard
WHILE UNTIL PINCOUNT EQUAL TO MAXPIN
INPUT PIN FROM KEYPAD
INCREMENT PINCOUNT
ENDWHILE
IF PIN EQUAL TO CardData.PIN
call services
THEN
PRINT TEXT TO "WRONG PIN"
EXIT checkPIN
ENDIF
END
Before offer the services, we need to check the PIN.
PROCEDURE services
VALUEABLE
SET BALANCE AS CardData.BALANCE
BEGIN
INPUT SELECTION FROM KEYPAD
CASE OF SELECTION
WHEN checkBalance
PRINT BALANCE
WHEN withdrawal
INPUT WITHDRAWAL_MONEY FROM KEYPAD
IF WITHDRAWAL_MONEY > BALANCE
PRINT TEXT TO "Not enough balance"
ELSE
WITHDROWAL MONEY
ENDIF
WHEN deposit
INPUT DEPOSIT_MONEY FROM KEYPAD
GET INSERTTED_MONEY FROM MACHINE
IF INSERTED_MONEY EQUAL TO DEPOSIT_MONEY
SET BALANCE AS BALANCE + DEPOSIT_MONEY
ELSE
RETURN INSERTED_MONEY FROM MACHINE
ENDIF
DEFAULT
RETURN THE CARD TO CUSTOMER
ENDCASE
END
we can check the balance from card data, using this to show the balance and withdrawal.
This is just a example to show how to write a pseudocode.
It depends on that who is the audience, what is the pupose etc..
Write any simple logic as pseudo-code. it would be helpful to understand it
Cheers
Thank you