Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pseucode Writing Procedure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670

Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

AS COMPUTER SCIENCE
BOOKLET FOR PAPER II PREPARATION

RESOURCE COMPILED BY:

Aqeel Ahmed
Contact Numbers: 92-21-333-2113627, 92-21-0314-2984670
Skype ID: aqeel.ahmed162
Facebook ID: hello_aqeel
E-mail: hello_aqeel@yahoo.co.uk
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

Chapter 14:
Programming and data representation
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

DECLARE
DECLARE Number1 : INTEGER // this declares Number1 to store a whole number
DECLARE YourName : STRING // this declares YourName to store a
// sequence of characters
DECLARE N1, N2, N3 : INTEGER // declares 3 integer variables
DECLARE Name1, Name2 : STRING // declares 2 string variables

CONSTANT
CONSTANT Pi = 3.14
Arithmetic Operators

OUTPUT
OUTPUT "Hello ", YourName, ". Your number is ", Number1 // newline
OUTPUT "Hello " // no new line
INPUT
INPUT Cost
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

DATA TYPES

Relational Operators
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

BOOLEAN OPERATORS

IF
EXAMPLE 1
IF x < 0
THEN
OUTPUT "Negative"
ENDIF
EXAMPLE 2
IF x < 0
THEN
OUTPUT "Negative"
ELSE
OUTPUT "Positive"
ENDIF
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

EXAMPLE 3
IF x < 0
THEN
OUTPUT "Negative"
ELSE
IF x = 0
THEN
OUTPUT "Zero"
ELSE
OUTPUT "Positive"
ENDIF
ENDIF

CASE
CASE OF Grade
"A" : OUTPUT "Top grade"
"F", "U" : OUTPUT "Fail"
"B".."E" : OUTPUT "Pass"
OTHERWISE OUTPUT "Invalid grade"
ENDCASE
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

FOR LOOP
EXAMPLE 1:
FOR x ← 1 TO 5
OUTPUT x
NEXT x
EXAMPLE 2:
FOR x = 2 TO 14 STEP 3
OUTPUT x
NEXT x
EXAMPLE 3:
FOR x = 5 TO 1 STEP -1
OUTPUT x
NEXT x
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

POST-CONDITION LOOPS
REPEAT
INPUT "Enter Y or N: " Answer
UNTIL Answer = "Y"
PRE-CONDITION LOOPS
Answer ← ""
WHILE Answer <> "Y" DO
INPUT "Enter Y or N: " Answer
ENDWHILE
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

LIST OF BUILT-IN FUNCTIONS


Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

MORE PSEUDO CODE SAMPLES

FUNCTION RESULT
OUTPUT CHR(65) A
OUTPUT ASC(“A”) 65
OUTPUT LEFT(“PAKISTAN”,3) PAK
OUTPUT LEFT(“PAKISTAN”,3) PAK
OUTPUT RIGHT(“PAKISTAN”,3) TAN
OUTPUT MID(“PAKISTAN”,3,2) KI
OUTPUT LCASE(“PAKISTAN”) pakistan
OUTPUT UCASE(“PAKISTAN”) PAKISTAN
OUTPUT TO_LOWER(“PAKISTAN”) pakistan
OUTPUT TO_UCASE(“PAKISTAN”) PAKISTAN
OUTPUT (“PAK” & “ISTAN”) PAKISTAN
OUTPUT RAND() ANY NUMBER BETWEEN 0 AND 1
OUTPUT RAND()*100 ANY NUMBER BETWEEN 0 AND 99
OUTPUT NUM_TO_STRING(23) “23”
OUTPUT STRING_TO_NUM(“23”) 23
OUTPUT INT(13/2) 6
OUTPUT TODAY() 3
(1 MON, 2 TUE, 3 WED)
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

USER DEFINED PROCEDURES

PROCEDURE InputOddNumber

REPEAT

INPUT "Enter an odd number: " Number

UNTIL Number MOD 2 = 1

OUTPUT "Valid number entered"

ENDPROCEDURE

PROCEDURE IS CALLED

CALL InputOddNumber
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

USER DEFINED FUNCTIONS

FUNCTION InputOddNumber() RETURNS INTEGER

REPEAT

INPUT "Enter an odd number: " Number

UNTIL Number MOD 2 = 1

OUTPUT "Valid number entered"

RETURN Number

ENDFUNCTION
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

1D ARRAY
ONE DIMENSIONAL ARRAY
Pseudocode example:

DECLARE List1 : ARRAY[1:3] OF STRING // 3 elements in this list

DECLARE List2 : ARRAY[0:5] OF INTEGER // 6 elements in this list

DECLARE List3 : ARRAY[1:100] OF INTEGER // 100 elements in this list

DECLARE List4 : ARRAY[0:25] OF STRING // 26 elements in this list

2D ARRAY
Aqeel Ahmed Contact: 92-21-333-2113627, 92-21-0314-2984670
Skype: aqeel.ahmed162 Facebook ID: hello_aqeel E-mail: hello_aqeel@yahoo.co.uk

TEXT FILES

You might also like