Translating Pseudocode into computer languages Lecture 14
Data Types PseudocodePascal/DelphiC++ Integer Floating point Boolean Character String Integer Single, Double Boolean Char String int, long float, double bool char string
PseudocodePascal/DelphiC++ Name Statement/s END PROCEDURE Name(parameter if any); BEGIN Statement/s ; END; FUNCTION Name(parameter if any): datatype; BEGIN statement/s; Name := returnValue; or RESULT := returnValue; END; void Name(parameter if any); { statement/s; } ; datatype Name(parameter if any) { statement/s; return value; }; Module Declaration
Variable & Constant Declaration PseudocodePascal/DelphiC++ Variable DeclarationVAR variableName1,...:datatype;datatype variableName1,... ; CONSTNAME = valueCONST CONSTNAME = value;const datatype CONSTNAME = value;
Assigning Value PseudocodePascal/DelphiC++ SET variableName TO value or variableName = value variableName := value ;variableName = value ;
Conditinal Operators PseudocodePascal/DelphiC++ operand = operand operand NOT = operand operand < operand operand > operand operand <= operand (operand = operand) (operand <> operand) (operand < operand) (operand > operand) (operand <= operand) (operand == operand) (operand != operand) (operand < operand) (operand > operand) (operand <= operand)
Logical Operators PseudocodePascal/DelphiC++ operand AND operand operand OR operand operand NOT operand operand AND operand operand OR operand NOT (operand) (operand && operand) (operand || operand) ! (operand)
IF ELSE PseudocodePascal/DelphiC++ IF (condition) THEN True statement/s block ELSE False statement/s block ENDIF IF (condition) THEN BEGIN True statement/s block; END ELSE BEGIN False statement/s block; END; if (condition) { True statement/s block; } else { False statement/s block; };
CASE OF PseudocodePascal/DelphiC++ CASE OF single_variable value_1 : statement block_1 value_2 : statement block_2... value_n : statement block_n value_other : statement block_other ENDCASE CASE single_variable OF value_1 : statement block_1; value_2 : statement block_2;... value_n : statement block_n; ELSE statement block_other; END; SWITCH (single_variable) { case value_1 : statement/s ; break; case value_2 : statement/s; break;... case value_n : statement/s; break; default: statement/s; };
DO WHILE PseudocodePascal/DelphiC++ DO statement/s WHILE (condition) REPEAT statement/s; UNTIL NOT (condition); do { statement/s; } while (condition); DOWHILE (condition) statement/s ENDDO WHILE (condition) DO BEGIN statement/s; END; while (condition) { statement/s; };
REPEAT UNTIL PseudocodePascal/DelphiC++ REPEAT statement/s UNTIL (condition) REPEAT statement/s; UNTIL (condition); do { statement/s; } while !(condition);
COUNTER PseudocodePascal/DelphiC++ DO counter = begin TO end statement/s ENDDO FOR counter := begin TO end DO BEGIN statement/s; END; for (counter = begin; counter <= end; counter++) { statement/s; };
RECORD STRUCTURE PseudocodePascal/DelphiC++ Record structures recordName fieldname1 fieldname2... fieldnameN TYPE recordName = RECORD field1 : datatype; field2 : datatype;.... fieldN: datatype; END; struct recordNameType { datatype field1; datatype field2;.... datatype fieldN; };
Sequential Files -Reading PseudocodePascal/DelphiC++ Initial processing READ variableName DOWHILE NOT EOF.... READ variableName ENDDO Final processing Program Name(Input, Output, fileHandle) VAR fileHandle : TEXTFILE;... BEGIN ASSIGNFILE(fileHandle,´´sourcefile´´); RESET (fileHandle); {Open for Reading} WHILE NOT (EOF(fileHandle)) DO BEGIN.... READLN(fileHandle, variable); END;... CLOSEFILE(fileHandle); END. #include... Ifstream inFileHandle;... inFileHandle.open(``source file``);... inFileHandle >> variableName; while (!inFileHandle.eof()) {... inFileHandle >> variableName ; }; inFileHandle.close();
Sequential Files - Writing Pseudocod e Pascal/DelphiC++ WRITE variableName Program Name(Input, Output, fileHandle) VAR fileHandle : TEXTFILE;... BEGIN ASSIGNFILE(fileHandle,´´sourcefile´´); REWRITE (fileHandle); {Open for Reading} WRITELN(fileHandle, variable or value);... CLOSEFILE(fileHandle); END. #include... ofstream outFileHandle;... outFileHandle.open(``source file``);... outFileHandle << variableName<<`` ``; }; outFileHandle.close();
Arrays PseudocodePascal/DelphiC++ Declaring arrays One-dimensional SET arrayName(maxNumElements) VAR arrayName : ARRAY[begin.. end] OF datatype; datatype arrayName [maxNumElements]; Declaring arrays Two-dimensional SET arrayName (row, column) VAR arrayName : ARRAY[begin..maxrow, begin..maxcolumn] OF datatype; datatype arrayName [row][columns]; Processing Array Elements Assigning a value arrayName (index) = value Reading a value variableName = arrayName (index) arrayName [index] := value; variableName := arrayName [index]; arrayName [index] = value ; variableName = arrayName [index];
END OF THE CLASS Good Luck for The Examination 15 June 2005 E2, – 13.00