What is the application of each step of an algorithm in the order in which the code statements are given?

What is the application of each step of an algorithm in the order in which the code statements are given?

NOTE: Learners are introduced to the concept of sequence in the world of Smeeborg. To learn more about sequence and other concepts, check out the full learning guide for Smeeborg --> click HERE

People are smarter than computers. We decide what actions we want a computer to carry out, and then we communicate in a language the computer understands in order to complete tasks. We call this language “code”, and we instruct the computer using specific commands.

Definition: 

Sequence, the order that commands are executed by a computer, allows us to carry out tasks that have multiple steps.

In programming, sequence is a basic algorithm: A set of logical steps carried out in order. Computers need instructions in the form of an algorithm in order to complete a desired task, and this algorithm must have the correct order of steps, or sequence.

Example: We can relate sequence to our everyday lives. Think about making a PB & J sandwich as a task, for example. If we want to make a delicious, peanut butter and jelly sandwich, we need to follow multiple steps in a logical order. We would begin by gathering our ingredients, getting a knife, spreading the peanut butter, spreading the jelly, and so forth.

If we wanted a robot to make a peanut butter and jelly sandwich for us, we would need to make sure we gave the robot instructions in the correct sequence to avoid missing a step, not having all the ingredients we need, or ending up with an inside-out sandwich.

Application: In programming, computers can only carry out tasks that are in the correct sequence. Computers are MACHINES and they have to do things the way they were built to do them.

In Kodable, the fuzz needs to be given instructions in the correct sequence to get through the maze. Using arrows as commands, we need to give the fuzz instructions in the correct sequence, or the fuzz will hit the wall of the maze.

Computers read in a certain order; similar to a typewriter which can only go left to right and top to bottom. A computer has to read code in order. If the sequence of the commands is incorrect, the computer won’t be able to follow the instructions.

Why is Sequence Important? In both programming and day to day tasks, if we don’t put every step in the right sequence, the end result isn’t what we wanted. Sequence is the most foundational concept in programming, and everything we learn moving forward will build on this concept.

A computer program is  a sequence of machine language instructions, expressing a meaningful sequence of statements possessing an order of execution and specifying a computer representation of an algorithm process.

An algorithm is a list of instructions specifying a sequence of operations  which will give the answer to any problem of a given type.  Generally speaking, an algorithm is a sequence of steps leading to the solution of a given problem or it is a step-by-step method of solving a problem or doing a task;  however, one feature distinguishes it from an ordinary procedure or list of instructions. The distinguishing feature is that an algorithm is designed to operate on data that are not known beforehand. In fact, the input of data may be a part of the algorithm itself. It is a single list of instructions defining a calculation which may be carried out on any initial data and which, in each case, gives the correct result. In other words, an algorithm tells how to solve not just one particular problem, but a whole class of similar problems.

* Algorithms can be expressed in several kinds of notation like; Pseudo Code, Flowcharts, Natural Language and Programming Language.  

Pseudocode  and Flowchart 

When programmers plan the logic for a solution to a programming problem, they often use one of two tools, pseudocode (“sue-doe-code”) or flowcharts.  Pseudocode is an English-like representation of the logical steps it takes to solve a problem.   A flowchart is a pictorial representation of the same thing.  

What is the application of each step of an algorithm in the order in which the code statements are given?


The old cliche, "a picture is worth a thousand words," has particular  truth for the person preparing a program for computer processing or designing a data processing system that uses a computer for one or more of its functions. A  flow chart is a picture of the steps which must be performed to accomplish a given job; it is useful during the planning stage and serves as a guideline for implementation as well. A flow chart is also an important part of the documentation of a program or system.

What is the application of each step of an algorithm in the order in which the code statements are given?


Pseudo is a prefix that means “false,” and to code a program means to put it in a programming language; therefore, pseudocode simply means “false code,” or statements that appear to have been written in a computer programming language but do not necessarily follow any syntax rules of any specific language. Another name for pseudocode is Programming Design Language (PDL).

Pseudocode is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language without any syntax rules  and can easily be converted into real programming language statements. Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language intended for human reading only.

 

The purpose of using pseudocode is that it is easier for people to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications that are documenting various algorithms, and for sketching out the structure of the program before the actual coding takes place.

PseudoCode must be complete in describing the logic of the algorithm so that it will be an easy task of translating it line-by-line in to its equivalent Source Code (Program Coding).

The following five statements constitute a

pseudo code  representation of a number-doubling problem: 

start 
    input myNumber 
    set myAnswer = myNumber * 2 
    output myAnswer 
Stop


Pseudocode is fairly flexible because it is a planning tool, and not the final product. Therefore, From the example above , you might prefer any of the following: 

  • Instead of start and stop, some pseudocode developers would use other terms such as begin and end. 
  • Instead of writing input myNumber, some developers would write get myNumber or read myNumber. 
  • Instead of writing set myAnswer = myNumber * 2, some developers would write calculate myAnswer = myNumber times 2 or compute myAnswer as myNumber doubled. 
  • Instead of writing output myAnswer, many pseudocode developers would write display myAnswer, print myAnswer, or write myAnswer. 

Common Action Keywords are often used to indicate common input, output, and processing operations. 

Input: READ, OBTAIN, GET


Output: PRINT, WRITE, DISPLAY, SHOW
Compute: COMPUTE, CALCULATE, DETERMINE
Initialize: SET, INIT
Add one: INCREMENT, BUMP,INCREASE

Another example is to find the perimeter and area of a rectangle. To find the perimeter and area of a rectangle, you need to know the rectangle’s length and width.The perimeter and area of the rectangle are then given by the following formulas:perimeter = 2 * (length + width)
area = length * width

The pseudocode to find the perimeter and area of the rectangle is as follows:

Begin

Get the length of the rectangle.

Get the width of the rectangle.

Compute perimeter = 2 *  (length + width)

Compute area = length * width

End 

Each textbook and each individual designer may have their own personal style of pseudocode. Pseudocode is not a rigorous notation, since it is read by other people, not by the computer. There is no universal "standard" for the industry, but for instructional purposes it is helpful if we all follow a similar style. The format below is recommended for expressing your solutions in our class.

The "structured" part of pseudocode is a notation for representing six specific structured programming constructs:

    1. SEQUENCE
    2. WHILE
    3. IF-THEN-ELSE
    4. REPEAT-UNTIL
    5. FOR
    6. CASE. 
Each of these constructs can be embedded inside any other construct. These constructs represent the logic, or flow of control in an algorithm. For writing any computer program, it has been proven to be sufficient, that Pseudo Code is made up of the following Logic Structures.

1-Sequence Logic

2-Selection or Decision Logic 

3-Iteration or Repetition Logic

What is the application of each step of an algorithm in the order in which the code statements are given?


1-Sequence Logic 

What is the application of each step of an algorithm in the order in which the code statements are given?

2-Selection or Decision Logic 

What is the application of each step of an algorithm in the order in which the code statements are given?

3-Iteration or Repetition Logic.

  1. What is the application of each step of an algorithm in the order in which the code statements are given?

These three basic constructs for flow of control are sufficient to implement any "proper" algorithm.

1- SEQUENCE LOGIC is a linear progression where one task is performed sequentially after another. The instructions are written in an order in which they are performed. The logic flow is from top to bottom.


What is the application of each step of an algorithm in the order in which the code statements are given?


EXAMPLE: Get the Area of a Rectangle.

Begin

    READ height of rectangle 
    READ width of rectangle               or  ( READ height and width of rectangle )
    COMPUTE area as height times width        

    Display the area computed

End

2- SELECTION LOGIC is a Decision Logic for making decision or selection in which a proper choice is made between two or more alternative courses of action. Selection Logic is depicted as  IF ... THEN ... ELSE,  IF ... THEN  or a CASE constructs. 

If the condition is true, 

     Then  sequence 1 (process 1) is performed, 

Else  sequence 2 (process 2) is performed.  

IF ... THEN  Construct

IF condition THEN

    sequence 1 (process 1)

ELSE

    sequence 2 (process 2)

ENDIF

What is the application of each step of an algorithm in the order in which the code statements are given?

What is the application of each step of an algorithm in the order in which the code statements are given?

Example: Compute Salary and Overtime
    IF HoursWorked > Normal THEN
        Compute Regular Salary with overtime
    ELSE
        Compute Regular Salary 
    ENDIF

The ELSE keyword and "sequence 2" are optional. 

What is the application of each step of an algorithm in the order in which the code statements are given?

What is the application of each step of an algorithm in the order in which the code statements are given?


Example: Compute the number of Male Students
  IF Student is a Male THEN
        Increase  MaleCount by 1

  ENDIF

There are times we may need to test several or multiple conditions and perform a specific task or process for each corresponding conditions as shown from the flowchart below.

What is the application of each step of an algorithm in the order in which the code statements are given?

What is the application of each step of an algorithm in the order in which the code statements are given?

 CASE construct indicates a multiway branch based on conditions that are mutually exclusive. The Four keywords ( CASE, OF, OTHERS, and ENDCASE ) and conditions are used to indicate the various alternatives. The general form is:

CASE expression OF

condition 1 : sequence 1 

condition 2 : sequence 2

...

condition n : sequence n

OTHERS:  default sequence

ENDCASE



What is the application of each step of an algorithm in the order in which the code statements are given?


What is the application of each step of an algorithm in the order in which the code statements are given?


The OTHERS clause with its default sequence is optional. Conditions are normally numbers or characters indicating the value of "expression", but they can be English statements or some other notation that specifies the condition under which the given sequence is to be performed. A certain sequence may be associated with more than one condition.

Example 1

        CASE  Title  OF                 Mr       : Print "Mister"                 Mrs     : Print "Missus"                 Miss    : Print "Miss"                 Ms       : Print "Mizz"                 Dr        : Print "Doctor"

        ENDCASE

Example 2

        CASE  grade  OF                 100 > X > 89:  points = 4                   90 > X > 79:  points = 3                   80 > X > 69:  points = 2                   70 >  X > 59:  points = 1                              X < 60:  points = 0

        ENDCASE

3- ITERATION LOGIC or REPETITION  is used to produce LOOPs where one or more instructions may be executed repeatedly several times depending on some of the conditions. It uses the structures of the WHILE,  FOR and the REPEAT-UNTIL constructs.

WHILE condition

    sequence

ENDWHILE

What is the application of each step of an algorithm in the order in which the code statements are given?


What is the application of each step of an algorithm in the order in which the code statements are given?


The loop is entered only if the condition is true.  The "sequence" is performed for each iteration. At the conclusion of each iteration, the condition is evaluated and the loop continues as long as the condition is true, else at false condition, the next execution is branch out of the loop.


Example-1
Get the product of the first 10 natural numbers.

Begin

SET  n = 1 , prod = 1, Limit = 10

End

Example-2
Count up: Read an integer number n and print the integers counting up from 1 to n

Start

Read n

Initialize .i to 1.

While i <= n, do;

Write i,

Increment . i 

end while

Stop.


FOR LOOP
This loop is a specialized construct for iterating a specific number of times, often called a "counting" loop. Two keywords, FOR and ENDFOR are used. The general form is:

FOR (iteration bounds)
    sequence


ENDFOR

What is the application of each step of an algorithm in the order in which the code statements are given?



Example:  

Find the product of the first 10 Natural Numbers.
Begin

        set prod = 1

FOR (n = 1 to 10)

prod = prod * n

ENDFOR

Display prod

End
REPEAT-UNTIL Loop
This loop is similar to the WHILE loop except that the test is performed at the bottom of the loop instead of at the top. Two keywords, REPEAT and UNTIL are used. The general form is:

REPEAT
    sequence (process 1 ... n)
UNTIL condition

What is the application of each step of an algorithm in the order in which the code statements are given?

What is the application of each step of an algorithm in the order in which the code statements are given?

The "sequence" in this type of loop is always performed at least once, because the test is peformed after the sequence is executed. At the conclusion of each iteration, the condition is evaluated, and the loop repeats if the condition is false. The loop terminates when the condition becomes true. 

Example:  

Find the product of the first 10 Natural Numbers.

Begin