Basic Language Structures

 

Linear Structure

          This is the simplest of structures and some might argue that this no structure at all but if there were no structures at all, then the statement could go in any order, and this is obviously not so! A linear structure is a simplest type of structure.

 

 

                                                           

 

                                                            Begin

 

 

 

 

 

 

 

 

 

 

Conditional Statement

          Decision making in many language can be made using the ‘If then conditional Statements. Simple Boolean Conditions are normally used as a basic on which to make the decision.

 

Example

 

* Ruwinda age > 8

* Sri Lankan population  < India’s population

* ( X < 40 ) or ( y = 10 )

If the expression or condition is true control is divided to one part of the program, if the condition is false then control is diverted to another.

 

1st statement

2nd statement

3rd statement

4th  statement

5th  statement

Write (‘Type in three numbers’)

Read (X,Y,Z)

Total := X+Y+Z

Average :=Total/3;

Write In (‘the average of X,Y and Z is’, average)

Count := count + 1 ;

Count := count - 1 ;

Boolean Statement

 

 

 

 

 

 

 

 

   THEN                                                                                ELSE

 

 

                   

 

 

 

If X  > 10 then count := count + 1 else count := count - 1

Example : single line condition statement

 

If X > 10 then

          Begin

 

          End

 

Else

          Begin

 

          End

 

Examples : multiple - line conditional Statement

 

Nested if then Statement

Most programming language, allows the nested if then statements, and we will use a simple example to illustrate these techniques. In the days when the author took his exams, you failed if you got less than 40% to 59% gave you a pass, 60%  to 70%gave you a credit, and 80% or more gave you a distinction. The following code, making use of nested if - then statements, would analyses the marks (exam_ mark) and print the appropriate grade on the computer’s monitor.

 

If exam mark > =40 then

          If  exam mark > =60 then

                    If exam mark > =80 then

                              Write 1n (‘Distinction’)

                    Else write 1n  (‘Merit’)

          Else write 1n  (‘Pass’)

Else write 1n (‘fail’);

 

 

 

 

 

 

 

 

 

 

 

 

GOTO    STATEMENT

The Goto Statement is a statement which might be more usefully described as an out of control  statement rather than a control statement! Ever since the late 1960s the go to statement ( or unconditional branch statement ) has been considered awful programming style as misused if often leads to spaghetti code.  This is not some unique pasta recipe, but refers to the control statements getting so tangled up with each other that it resembles a plate of spaghetti.

 

 

 

 

 

Repetition Structures

 

       It has ability to repeat sequence many time this is also commonly known as iteration.

 

 

While loop

 

    The while loop , also known as a do while  loop , or do loop , checks a Boolean condition before carrying out any of the statements that would be executed if the condition is true. It should  therefore be noted that if  ‘white loops”  are used , the statements may never  be executed if the Boolean expression is false.

 

* The simples form of loop  is the while statement.

 

White (expression)

Statement

 

 

If the expression evaluates to true, the statement is executed and then the expression and then the expression is reevaluated (if it is true, the

 body of the loop is executed, and so on). The loop exits . When the expression evaluates to false. As an example here's some code that adds the whole numbers from 1 to 10.

 

While count <10 do

          Begin 

                    Count:= count + 1,

                    Write 1n (count sill less than 10)

          End;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<?php

$total=O;

$ 1= 1;

        While ($1<=10)]

        $ total = ++$ 1;

         [

        Echo $ total;

        ?>

For loops

The for loop , or for to next loop as it is sometimes know, appears in many high level languages . It is often  compiled more easily than the repeat or while loops mentiones above , because of the close relation ship between the  control variables used and registers in the machine.

 

For count := 1 to 10 do

          Begin

 

 

          End

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Flowchart: Decision: Exam_Mark
>59
Flowchart: Decision: Exam_Mark
>39

fail

pass

There are few reasons to make use of the go to statement in modern programming . Avoid it if you possibly can, it makes your programs easier to understand by other people.

Flowchart: Decision: Exam_Mark
>59

credit

Diamond: Boolean 
Condition

1st statement

1st statement

exit

while

1st statement

2nd statement

Set Count

Diamond: Does count +final value
?

1st statement

2nd statement

Count =count + increments

Execute these statements

False

exit

True

[HOME]  [2]  [3]  [4]  [5]  [6]  [7]  [8]  [9]  [10]  [11]  [12]  [13]  [14]  [15]  [16]  [17]  [18]  [19]  [20]  [21]  [22]  [23]  [24]  [25]  [26]  [27]  [28]  [29] [30]  [31]  [32]  [33] 

<<[PREVIOUS]                   [NEXT]>>

 

 

www.kiddiesnet.com