11.5.3.6 use various algorithmic structures in script language Algorithmic structures
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:
Exercises: Ex. 1 Quiz "Sequencing, Selection & Iteration" Ex. 2 Exam questions:
| |
| |
Просмотров: 4606 | | |
Всего комментариев: 0 | |