Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Contents
Introduction Sequence Assignment Input Display Selection If-Else Iteration (Repetition) For While Programs Subprograms: Subroutines and Functions Subroutines Functions Keywords
Introduction
Pseudo code (or Structured English) is used to specify program logic in a (somewhat) English like manner, that aims to be independent of any particular programming language. This simplifies program development by separating it into two main parts, logic design and coding. The use of pseudo code allows the programmer to focus on the logic of the program rather than implementation details such as how data is displayed Once the logic is developed, coding becomes the translation of the pseudo code into the required programming language.
Sequence
Assignment
Format
variable = expression
Examples
total = 0 y = x * x + z / 4 - 1 name = "Joan Smith"
validLength = True
Input
Format
Input variable, variable, ...
Examples
Input custName Input distance, speed
Display
Format
Display value, value, ...
Examples
Display "Hello World" Display totalPrice, taxPayable Display "Customer Number: ", custNum, "Name: ", custName
Selection
If-Else
Format
IF condition THEN statement statement ... ENDIF
IF condition THEN statement statement ... ELSE statement statement ... ENDIF
IF condition THEN statements ELSE IF condition THEN statements ELSE IF condition THEN statements ELSE statements ENDIF ENDIF ENDIF
IF condition THEN statements ELSE IF condition THEN statements ELSE IF condition THEN statements ELSE statements ENDIF
Note: the Else part is optional. Use it where required by the logic. Examples
IF lineNumber > 50 THEN Display "" Display "Student Number", "Student Name" lineNumber = 0 ENDIF
IF monthNumber >= 1 AND monthNumber <= 12 THEN Display "valid month" ELSE Display "invalid month" Display "Month must be between 1 and 12" ENDIF
IF mark >= 80 THEN grade = "A" comment = "Excellent" ELSE IF mark >= 70 THEN grade = "B"
ELSE IF mark >= 60 THEN grade = "C" ELSE IF mark >= 50 THEN grade = "D" ELSE grade = "N" comment = "Poor" ENDIF ENDIF ENDIF ENDIF
IF mark >= 80 THEN grade = "A" comment = "Excellent" ELSE IF mark >= 70 THEN grade = "B" ELSE IF mark >= 60 THEN grade = "C" ELSE IF mark >= 50 THEN grade = "D" ELSE grade = "N" comment = "Poor" ENDIF
Iteration (Repetition)
For
Format
FOR counter = start-value to end-value DO statement statement ... ENDFOR
Example
FOR x = 1 to 10 DO xSquared = x * x Display x, xSquared ENDFOR
While
Format
WHILE condition DO statement statement ... ENDWHILE
Example
count = 1 WHILE count <= 10 DO Display count Add 1 to count ENDWHILE
Programs
Format
program-name() statement statement ... STOP
Example
addTwoNumbers() Input number1, number2 sum = number1 + number2 Display sum STOP
Functions
Functions are also called non-void methods. Functions return a single value. Format - function calls
function-name() function-name(parameter, parameter, ...)
month = inputMonth() IF oddNumber(month) THEN ... ENDIF volume = 4 /3 * PI * cube(radius) max = maximumNumber(a, b) STOP
Format - function declarations The following are called functions. They return a single value.
function-name() statement statement ... RETURN with value function-name(parameter, parameter, ...) statement statement ... RETURN with value
Keywords
The following words have a special meaning in Pseudo Code (in this subject) and should only be used for the purposes described above. AND, DO, ELSE, ENDIF, ENDFOR, ENDWHILE, EXIT, FOR, IF, NOT, OR, RETURN, STOP, THEN, TO, WHILE. Other statements: Display, Input Operators: =, +, -, *, /, (), <, <=, >, >=, [] Basic Language Guide | Data Dictionary Guide | Defining Diagram Guide | Desk Check Guide | Java Language Guide | Unix Guide
Last modified 03-Jan-2005 by Tim Whitfort. Copyright 2003-2005 Tim Whitfort