Sei sulla pagina 1di 7

Pseudocode syntax, descriptions and examples

Overview: This table provides a reference for commonly used pseudocode for introductory computer program design courses. You should use
this as your reference and copy and paste code examples into your projects to ensure you are using proper syntax. Be sure to indent your code
to make it more readable and use modify and enhance from the examples as needed. Also, capitalize the first letter of your pseudocode (e.g.
While, not while).

Pseudocode Description Flowchart equivalent Example usage


Write/Prompt Displays messages and other Write “What is your name?”
information on the screen Write “Your name is “ +YourName + “.”
Write “What is your Prompt for ItemName, Price, Quantity
name?”

Input Pauses execution, allowing the Input FirstName


user to enter data
Input FirstName Input Number1, Number2

Compute/Set Assigns a value to a variable Compute average value:


Set Avg= (num1 + num2)/2
Set
Avg=(num1+num2)/2 Compute total cost:
Set TotalCost= 1.25*Songs

Declare Declares a variable to be of a Declare FirstName As String


specific type
Declare FirstName as String Declare Num1, Num2 As Integer

Possible datatypes may include:


String

1
Character
Integer
Float
Call Requests a module, Main Module
subprogram, or function be Write “Enter 2 numbers”
executed Input Num1,Num2
Call WriteNums(num1,num2)
Write “Program complete”
Call WriteNums(num1, num2)) End Program
Subprogram WriteNums(Float num1,
Float num2)
Write “Num1 is “ + num1
Write “Num2 is “ + num2
End Subprogram

If Tests if a condition is met. If


Enter
End If the test condition is true, the Input Number
statements are executed. If Number < 5 Then
Write “OK”
End If
Is
No
Number <
5?

Yes

Write “OK”

Exit

2
Input Age
If Tests If a condition is true. If Enter If Age > 18 Then
Else the test condition is true one Write” You are an Adult”
End If set of statements are Else
executed, else, the other set of Write” You are Young”
statements are executed. Yes Is Age > No End If
18?

Write “You Write “You


are an Adult” are Young”

Then Clause Else Clause

Exit

Select Case Tests a condition against


Enter
End Case multiple values executing the
code with appropriate
matching value, skipping all
other code blocks. Value of Select Case Of Grade
A
Grade F Case A:Write “Excellent”
B
Case B: Write “Good”
Write Write Write
Case C: Write “Average”
“Excellent” “Good” “Failure”
Case D: Write “Poor”
Case2 Case n Case F: Write “Failure”
End Case
Exit

3
Repeats execution of code until
Repeat the condition is true. (Post-Test Enter
Until loop) Repeat
Input Number
Input
Until Number==0
Number Write “List Ended”

loop body

Is Number No
==0?

True?

Yes

Write “List Ended”

Exit

4
While Repeats execution of code as
End While long as the condition is true. Enter Set Number=0
(Pre-Test loop) While Number < 10
Set Number=Number+1
End While
Write “Program complete”
Is Number No
< 10?

Yes
Set Number = Write
Number + 1 “Program
Complete”
loop body

Exit

For Repeats execution of code as


Enter
End For long as the condition is true. Declare Num as Integer
(Pre-Test loop). Counter is For( Num=0; Num<10; Num++)
often included in the Write “Num is ” + Num
Set Num=0
declaration. End For

Is No
Num<10?

Yes

Write “Num is “ + Num


Exit

Num=Num+1

5
Main Groups programming Main
End Program statements into the main Enter Write “Hello World”
module Write “Welcome to Class”
End
Write “Hello World”

Write “Welcome to Class”

Exit

Subprogram Groups programming


Enter
End Subprogram statements into a Subprogram Main
that is often called from Write “Enter Words”
another Subprogram,function Input Words
or Module Write “Enter Words” Call WelcomeMessage (Words)
End Program
(Note: Flowchart for Subprogram WelcomeMessage (String
Programming statements for Input Words Words)
the WelcomeMessage Write “Words are ” + Words
Subprogram are not shown) End Subprogram
Call WelcomeMessage(Words)

Exit

6
Function Groups programming
End Function statements into a function that Enter Main
is often called from another Declare AreaofSquare as Float
Subprogram, function or Set AreaofSquare= Area(5)
Module Declare AreaofSquare as Float End Program
Function Area (Side) As Float
(Note: Flowchart for Set Area= Side * Side
Programming statements for End Function
Set AreaofSquare = Area(5)
the AreaofSquare function are
not shown)

Exit

Potrebbero piacerti anche