Pseudocode Conventions


Pseudocode 10 Conventions

In computational theory, we distinguish between an algorithm and a program. The latter does not have to satisfy the finiteness condition.



 For example, 

we can think of an operating system that continues in a "wait" loop until more jobs are entered. Such a program does not terminate unless thes ystem crashes. Since our programs always terminate, we use "algorithm"and "program" interchangeably in this text.

We can describe an algorithm in many ways. We can use a natural language like English, although if we select this option, we must make sure that the resulting instructions are definite. Graphic representations called flowcharts are another possibility, but they work well only if the algorithm is small and simple. In this text we present most of our algorithms using a pseudocode that resembles C.

1. Comments begin with // and continue until the end of line.

2. Blocks are indicated with matching braces: { and }. A compound statement (i.e., a collection of simple statements) can be represented as a block. The body of a procedure also forms a block. Statements are delimited by ;

3. An identifier begins with a letter. The data types of variables are not explicitly declared. The types will be clear from the context. Whether a variable is global or local to a procedure will also be evident from the context. We assume simple data types such as integer, float, char, boolean, and so on. Compound data types can be formed with Records.

Here is an example:

node=record

   datatype_1 data_1;

                .

                .

                .

   datatype_n   data_n;

   node        *link;

}

In this example, link is a pointer to the record type node. Individual data items of a record can be accessed with→ and period. For instance if p points to a record of type node, p→ data_1 stands for the value of The first field in the record. On the other hand, if q is a record of type Node, q.data_1 will denote its first field.

4. Assignment of values to variables is done using the assignment statement


    (variable):= (expression);

5.There are two boolean values true and false. ln order to produce these values, the logical operators and, or, and not and the relational operators <, S,=, ,2, and > are provided.

6. Elenments of multidimensional arrays are accessed using | and . For example, if A is a two dimensional array, the (2,I)th elenent of the array 1s denoted as Ali, j. Array indices start at zero.

7. The following looping statements are employed: for, while, and repeat-until. The while loop takes the following form:

while (condition) do

{

   (statement 1)

     .

    .

   (statement n)

}

As long as (condition) is true, the statements get executed. When (condition) becomes false, the loop is exited. The value of (condition) is evaluated at the top of the loop.

The general form of a for loop is

for variable := valuel to value2step step do

{

   (statement 1)

    .

    .

   (statement n)

}

Here value1, value2, and step are arithmetic expressions. A variable of type integer or real or a numerical constant is a simple form of an arithmetic expression. "The clause *step step" is optional and taken as+1 if it does not occur. step could either be positive or negative. variable is tested for termination at the start of each iteration. The for loop can be implemented as a while loop as follows:

variable := valuel;

fin:=value2;

incr:= step;

while ((variable-fin) * step<=0) do

{

    (statement 1)

      .

       .

    (statement n)

    variable := variable + incr;

}

A repeat-until statement is constructed as follows:

repeat

      (statement 1)

        .

        .

      (statement n)

until (condition)

The statements are executed as long as (condition) is false. The value of (condition) is computed after executing the statements.

The instruction break; can be used within any of the above looplng instructions to force exit. In case of nested loops, break; results in the exit of the inermost loop that it is a part of. A return statement within any of the above also will result in exiting the loops. A return statement results in the exit of the function itself.

8. A conditional statement has the following forms:

if (condition) then (statement)

if (condition) then (statement 1) else (statement 2)

Here (condition) is a boolean expression and (statement), (statement 1), and (statement 2) are arbitrary statements (simple or compound).

We also employ the following case statement:

case

{

        :(condition 1): (statement 1)

                    .

                    .

                    .

        :(condition n): (statement n)

        :else: (statement n +1)

}

Here (statement 1), (statement 2), etc. could be either simple statements or compound statements. A case statement is interpreted as follows. If (condition 1) 1s true, (statement 1) gets executed and the case statement is exited. II (statement 1) is false, (condition 2) 1s evaluated. If (condition 2) is true, (statenment 2) gets executed and the case statement exited, and so on. If none of the conditions (condition 1),..., (condition n) are true, (statement n+1) is executed and the case statememt is exited. The else clause is optional.

9.Lnput and output are done using the instructions read and write. No format is used to specify the size of input or output quantities.

10. There is only one type of procedure: Algorithm. An algorithm consists of a heading and a body. The heading takes the form

                                         Algorithm Name (<parameter list>)

where Name is the name of the procedure and ((parameter list)) is a listing of the procedure parameters. The body has one or more (Simple or compound) statements enclosed within braces and. An algorithm may or may not return any values. Simple variables to procedures are passed by value. Arrays and records are passed by reference. An array name or a record name is treated as a pointer to the respective data type.

As an example, the following algorithm finds and returs the maximum of n given numbers:

1.   Algorithm Max(A, n)

2.   // A is an array of size n.

3.   {

4.                Result:= A[l];

5.                for i:=2 to n do

6.                        if A[i]> Result then Result:= A1;

7.                return Result;

8.    }

In this algorithm (named Max), A and n are procedure parameters. Result and i are local variables.

Post a Comment

0 Comments