## Revise Now

### GCE Subjects

Physics Chemistry CS Accounting

### Coding Lessons

Learn HTML Learn SEO

# Loops, Selections, Arrays & Loops

Basic algorithms won't be taught here

You need to know what an algorithm consists of:

1. Assignment

2. Sequence

3. Selection

4. Iteration

Sometimes assignment is not include as a separate point. So the Sequence , Selection and Iteration is the most important parts of an algorithm

## Structured English

This is a different form of representing an algorithm

The program is written using command words such as SET and DO

Let's see an example:

This is in pseudocode

```INPUT Number
Total←0
WHILE Number <> -1 DO
Total←Total+Number
INPUT Number
ENDWHILE
OUTPUT Total
```

In structured english:

```ENTER Number
SET Total to 0
WHILE Number Not equal to 0 INCREMENT TOTAL BY Number,ENTER Number
DISPLAY Total
```

You will need to know some points

For assignment

`SET X to 36`
`INCREMENT X BY 1`

For selection

```IF A IS GREATER THAN B
THEN
.....
ELSE
.....
```

For Repetion

```REPEAT UNTIL A IS GREATER THAN B...
```
`WHILE A IS GREATER THAN B ...`

For input and output

```     OUTPUT  A
DISPLAY A

INPUT A
ENTER A

```

## Assignment

When you assign a value to a variable this is called assignment

`x ← 20`

We use an arrow in pseudocode to show that it is an assignment, the equal sign (=) is used in conditions and for comparisons

Totalling is a specific part of assignment

As totalling has the same principle but, the value is incremented by a certain value

`x ← x + 1`

However, x must be first initialised to 0

## Identifier table

Most questions asks you to draw an identifier table for your algorithm or code

This means to draw a table which contains the list of variable used in the program and their corresponding purposes and datatypes

This is a simple example

Identifier Table
Variable Name Datatype explanation
Total REAL Used to total up the class marks
Count INTEGER Used to increment the loop by 1 for every iteration
DoB DATE Used to record the date of birth of the user
Studentlist ARRAY[1:10] OF STRING Used to store the names of 10 students in an array

If they ask you to draw an identifier table then you don't have to declare any variables

## Selection

Selection is when the output or code to performed depends on a certain condition being true

```
IF (condition)

THEN
OUTPUT (statement)

ELSE
OUTPUT (statement)

END IF
```

This is in psudeocode and this is known as the IF statement

Notice how it is indented and also the keywords like IF are uppercase

If statements can have many condition to compare - for example if you want to check condition 1 and if it is false then check the second condition

```
IF (condition1)

THEN
OUTPUT (statement)

ELSE
IF (condition2)

OUTPUT (statement)

ELSE

OUTPUT (statement)

END IF

END IF
```

So the program will check the next condition if the previous one is false. If condition 1 is true it will not check the 2nd condition.

Also this is not Nested if statement

## Nested If Statements

Is an If statement within the if statement

```
IF (condition1)

THEN

IF (Condition2)
THEN
OUTPUT (statement1)
ENDIF

ELSE

OUTPUT (statement2)

END IF
```

This ensures that both condition 1 and 2 must be true to do statement1

So first it checks the first condition then the second condition - Infact this is the same as an AND operator condition but, an order to it

## Case Statements

This is used when there are many conditions and a single output for each

```
CASE OF (Variable)
(value1):(OUTPUT)
(value2):(OUTPUT)
(value2):(OUTPUT)
.
.
.
OTHERWISE:(OUTPUT)
ENDCASE

```

The above code shows that the output to be performed depends on the value the variable contains

Below is more complex example

```
'A':OUTPUT"Excellent"
'A','B':OUTPUT"Good"
'C' TO 'E':OUTPUT"Moderate"
.
.
.
OTHERWISE:OUTPUT"Fail"
ENDCASE

```

Python doesn't have CASE statements but only elif

## Count controlled loop

This method is used when the loop is supposed to interate for a specified number of times and is use when the number of times is known

```FOR COUNT ← 1 TO 5
OUTPUT (Statement)
ENDFOR

```

If you want, you can use NEXT COUNT instead of ENDFOR

## Post conditioned loops

The code interated or loops until a certain condition is true and the code is run atlEast once

```REPEAT
OUTPUT (statement)
UNTIL (condition)

```

While loops are better especially when there is a rogue value

## Pre condition loops

First it checks whether the condition is true and then loops while the condition is true. The program doesn't have to run atlEast once

```WHILE (condition) DO
OUTPUT (statment)
ENDWHILE
```

Conditon means such as comparing variables or checking a variable is larger than a value. You need to know what are arithmetical operators

Also the conditions could have boolean operators such AND or OR or NOT

An example is WHILE X>3 OR X = -1 DO

## Rogue Value

When you need to loop a program but you don't know how times it will be running for and to loop it until you input all the values in an unknown list - we use a rogue value

A rogue value is a input value which terminates the loop when entered and this is usually a value which is not common in the set of input values - example (-1)

It is better to use a while loop rather than a repeat loop because the repeat loop will run atlEast once

So if the list is empty and so the first input value is a rogue value then this would either complicate the repeat loop or cause an error

Let's see a simple example

```INPUT Number
Total←0
WHILE Number <> -1 DO
Total←Total+Number
INPUT Number
ENDWHILE
OUTPUT Total
```

## FLOW-CHARTS

We will not discuss flowcharts as it is simple and just requires you to convert the psueduocode in to a graphical manner

You wiil need to know the shapes used for each type such as for conditions and outputs

## 1D Array

This is used to store data of the same data type, we need to know how to declare an array

```DECLARE (Arrayname):ARRAY[x:y] OF DATATYPE
```

Data type means whether it was REAL or STRING..

If we need to modify a value in the array:

```Arrayname[i] ← 20
```

More about arrays will be talked in the next chapter

## Linear search

This program must be remembered as they usually ask this

This is used to find a value in an array

```i = 0

FOUND = FALSE
MAX = 5 // This is actually the upper index
INPUT Search
REPEAT
i ← i+1
If Array[i] = Search
THEN
FOUND ← TRUE
END If

UNTIL FOUND = TRUE OR i = MAX
IF FOUND = TRUE
THEN
OUTPUT"The value is at position",i
ELSE
OUTPUT"The value is not in the array"
END If
```

This is infact an easy code to remember and we could use repeat loop because the program must run atlEast once to compare

The program checks whether the value is present in the array and loops until it is present or if it reaches the end of the array.

If found the value is displayed

## Binary search

This is an A2 topic so we will discuss this later

## Bubble Sort

In order for contents to be sorted in an array we could use some functions to do this, however we need to know the method used and so remember the code below

```NEW ← MAX-1

INPUT ArrayName
FOR COUNT ← 1 TO MAX-1

FOR i ← 1 TO NEW

If ArrayName[i]>ArrayName[i+1]
THEN
TempVariable ← ArrayName[i]
ArrayName[i] ← Arrayname[i+1]
ArrayName[i+1] ← TempVariable
END If

ENDFOR
NEW ← NEW-1

ENDFOR
```