Algorithmic structures

11.5.3.6 use various algorithmic structures in script language

Algorithmic structures 

An algorithm is a sequence of actions to achieve a result in a certain number of steps.

When you write lines of code, there are three ways you can control the order these lines will be executed by the computer:

Sequencing - This means that the computer will run your code in order, one line at a time from the top to the bottom of your program. It will start at line 1, then execute line 2 then line 3 and so on till it reaches the last line of your program.

Example,

 

firstname = INPUT ("What is your firstname?")
lastname = INPUT ("What is your lastname?")
PRINT ("Hello " + firstname + " " + lastname + "!")

Selection - Sometimes you only want some lines of code to be run only if a condition is met, otherwise you want the computer to ignore these lines and jump over them. This is achieved using IF statements. e.g. If a condition is met then lines 4, 5, 6 are executed otherwise the computer jumps to line 7 without even looking at line 4,5 and 6.

Example,

IF goalsIn == goalsOut:
     PRINT "It's a draw"
ELSE IF goalsIn > goalsOut:
     PRINT "Player 1 wins!"
ELSE:
     PRINT "Player 2 wins!"

Iteration - Sometimes you want the computer to execute the same lines of code several times. This is done using a loop. There are three types of loops: For loops, while loops and repeat until loops. That’s handy as it enables you not to have to copy the same lines of code many times.

Example,

WHILE (timer > 0):
     PRINT "Carry on playing..."
     timer = timer - 1

OR

PRINT ("---- 7 Times Table ----")
FOR number FROM 1 TO 10:
     PRINT 7 * number

Questions:

  1. Give definition for term "algorithm".
  2. Explain what does "loop" mean.
  3. Describe differences between linear algorithm and algorithm which has a condition.
  4. Explain why a programmer should use iteration in his code.

Exercises:

Ex. 1

Quiz "Sequencing, Selection & Iteration"

Ex. 2

Exam questions:

 

 

Категория: Programming languages | Добавил: bzfar77 (09.02.2021)
Просмотров: 4606 | Теги: Algorithmic structure, Programming, Selection, Iteration, Sequencing, Loop, Condition | Рейтинг: 0.0/0
Всего комментариев: 0
avatar