How to be a Programmer | Part 4 : Control Structures

CtechF - Featured How to be a programmer part 4

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

Also Read :

1) Control Structures

1.1) Sequence Statements.

CtechF - flow - how to be a programmer part 4

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.

CtechF - loop - how to be a programmer part 4

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)
CtechF - Start - end
  • Input / Output – To represent the input of data for processing or output of processed Information.
CtechF - Input - Output
  • Process – To represent an operation or a set of operations or a set of operational function of a program or initialization of variables.
CtechF - Process
  • Decision – To make logical construction to check some condition or decision making and branching.
CtechF - Decisions
  • Flow lines – flow lines.
CtechF - Flow line
  • Connector – To link one part of the flowchart to another.
CtechF - Connector

# 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

CtechF - flowchart 1

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

Pseudo Code

BEGIN
  input the radius as r
  circumference = ((2 X 22 X r)/ 7)
  area = (22 X r2 / 7)
  print circumference, area
END

Flowchart

CtechF - flowchart 2

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
  Read marks as  m
  IF m > 50 THEN
    Print “PASS”
  END IF
END

Flowchart

CtechF - flowchart 3

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
  Read marks as m
  IF m > 50 THEN
    Print “PASS”
  ELSE
    Print “FAIL”
  END IF
END

Flowchart

CtechF - flowchart 4

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

CtechF - flowchart 5

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
  Read subject marks as M
  IF M < 0 OR M > 100 THEN
    Grade = “not valid input”
  ELSE
    IF M < 36 THEN
      Grade = “WEAK”
    ELSE
      IF M < 56 THEN
        Grade = “GOOD"
      ELSE
	IF M < 66 THEN
	  Grade = “VERY GOOD”
	ELSE
	  Grade = “EXCELLENT”
        END IF
      END IF
    END IF
  END IF
END

Flowchart

CtechF - flowchart 6

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

CtechF - flowchart 7

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

CtechF - flowchart 8

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

CtechF - flowchart 9

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

CtechF - flowchart 10

Also Read :


Sources:

Hi Friends,

Hope this post will help you to Learn something, If you enjoy my work then please share my posts with your buddies and anyone who might be interested in programming and don’t forget to subscribe.

If you had any problem with this post, please do mention it in the comment section.

Leave a Reply

18 Shares
Share
Tweet
Share
Pin
Share