# How to be a Programmer | Part 4 : Control Structures

How to be a Programmer (Part 4) –
Control Structures and Algorithms

## 1) Control Structures

### 1.1) Sequence Statements.

A simple definition of a sequence is that it is a set of instruction that is in order, one instruction after the other, once one instruction has been executed. All actions aligned with the same intent. (top to bottom)

### 1.2) Selection / Branching Conditions.

Selection is a situation where step(s) are executed depending on whether the condition of an algorithm is satisfied or not. There are two choices; if the condition is satisfied, one is selected and if it is not satisfied, the other selection is selected.

### 1.3) Iteration / Repetitions / Looping.

If one or more steps of an algorithm are repeated until a condition is true, it’s called repetition,

## 2) Problem-solving using Algorithm

### 2.1) What are Algorithms

An algorithm is a method to show the steps in solving a problem.

### 2.2) Develop an Algorithm

#### # Graphically —- Flow Chart

A flowchart is a diagrammatic/graphical/visual representation of an algorithm that illustrates the sequence of operations to be performed to get the solution of a problem Once the flowchart is drawn, it becomes easy to write the program in any high-level languages.

• Terminators – Start / End (To indicate start or end of a program)
• Input / Output – To represent the input of data for processing or output of processed Information.
• Process – To represent an operation or a set of operations or a set of operational function of a program or initialization of variables.
• Decision – To make logical construction to check some condition or decision making and branching.
• Flow lines – flow lines.
• Connector – To link one part of the flowchart to another.

#### # Textually   —- Pseudo Code

When an algorithm is presented in simple English terms it is called a pseudo code. Pseudo codes are independent of a computer language. Pseudo codes can be converted any programming language instructions.

Hence, pseudo codes make programming easier Using Pseudocode, it is easier for a non-programmer to understand the general working of the program.

Some words are used commonly in pseudo code

• BEGIN, START – to indicate a beginning
• END, STOP – to indicate an end
• IF, THEN, ELSE, ENDIF – Used to indicate a selection
• WHILE, ENDWHILE, REPEAT, UNTIL, LOOP – To indicate a repetition
• INPUT, READ, GET – To indicate an Input
• OUTPUT, DISPLAY, PRINT, WRITE, SHOW – To indicate an output
• CALCULATE, ADD, SUM, AVERAGE, COUNT, FIND – To indicate a process

Example:

1 – Pseudo code and Flowchart to find an area of a square.

Pseudo Code

```BEGIN
input the side value as a
area = (a x a)
print area
END```

Flowchart

2 – Pseudo code and Flowchart to find circumference and area of a circle.

Pseudo Code

```BEGIN
circumference = ((2 X 22 X r)/ 7)
area = (22 X r2 / 7)
print circumference, area
END```

Flowchart

3 – Pseudo code and Flowchart to read the marks of a subject from student and to print the grade as “PASS” if the mark is greater than 50.

Pseudo Code

```BEGIN
IF m > 50 THEN
Print “PASS”
END IF
END```

Flowchart

4 – Pseudo code and Flowchart to read the marks of a subject from student and print the grade as “PASS” if the mark greater than 50 and otherwise “FAIL”.

Pseudo Code

```BEGIN
IF m > 50 THEN
Print “PASS”
ELSE
Print “FAIL”
END IF
END```

Flowchart

5 – Pseudo code and flowchart to check if the number is POSITIVE or NEGATIVE or ZERO and display an appropriate message.

Pseudo Code

```BEGIN
Read a real number as N
IF N >= 0 THEN
IF N > 0 Then
M = “POSITIVE”
ELSE
IF N = 0 THEN
M = “ZERO”
END IF
END IF
ELSE
M = NEGATIVE”
END IF
Print “Number is “, M
END```

Flowchart

6 – Pseudo code and Flowchart to read subject marks of a student and to print the grade according to the following conditions.

• 00 – 35  — WEAK
• 36 – 55 — GOOD
• 56 – 65 — VERY GOOD
• 66 – 75 — EXCELLENT

Pseudo Code

```BEGIN
IF M < 0 OR M > 100 THEN
ELSE
IF M < 36 THEN
ELSE
IF M < 56 THEN
ELSE
IF M < 66 THEN
ELSE
END IF
END IF
END IF
END IF
END```

Flowchart

7 – Pseudo code and Flowchart to print the integer numbers from 1 to 5 and print “ctechf.com” finally. (FOR LOOP)

Pseudo Code

```BEGIN
FOR counter = 1 TO 5
Print counter
NEXT counter
Print “ctechf.com”
END```

Flowchart

8 – Pseudo code and Flowchart to read a series of random numbers form user and print the largest number of them.

Pseudo Code

```BEGIN
Input number of integers required as M
Input the first integer as N
Max = N
FOR i = 1 TO (M-1)
Input next integer as N
IF N > MAX THEN
MAX = N
END IF
NEXT i
Print “The largest number is”, Max
END```

Flowchart

9 – Pseudo code and Flowchart to print the integer numbers from 1 to 5 and print “ctechf.com” finally. (WHILE loop)

Pseudo Code

```BEGIN
n = 1
WHILE n <= 5
Print n
n = n + 1
END WHILE
Print “ctechf.com”
END```

Flowchart

10 – Pseudo code and Flowchart to print the integer numbers from 1 to 5 and print “ctechf.com” finally. (DO UNTIL)

Pseudo Code

```BEGIN
n = 1
REPEAT
Print n
N = n + 1
UNTIL n > 5
Print “ctechf.com”
END```

Flowchart