The structure of programming F452 AS Computing
The basic constructs of programming Assignment Sequence Selection Iteration
Assignment Assigning values to variables x = 5 text1 = “Hello” Paid = False Integer String Boolean
Sequence All instructions are executed once in the order in which they appear. Instructions are done sequentially
Selection There is an option of statements and a condition is used to determine which (if any) statement is executed. Eg. Using an IF statement
2 Types of Selection The IF statement The ELSE part is optional. 2. The CASE statement IF <condition> THEN ... ELSE END IF SELECT CASE <variable> CASE <value> ... CASE <value 2> END SELECT
Iteration - Loops Infinite loop Count-controlled loop “A group of statements is executed a set number of times or until a condition is met.” Infinite loop Condition-controlled loop Count-controlled loop
Infinite Loop 10 Print “Hello World” 20 Goto 10 Loop will never stop. No condition is coded within the loop to make it stop. 10 Print “Hello World” 20 Goto 10 Hello World
Condition-controlled loops WHILE loop x=5 WHILE x > 0 Do Begin Print x x = x – 1 End END WHILE Print “Done” 4 3 2 1 Done
Condition-controlled loops REPEAT UNTIL loop 1 2 3 4 5 6 7 8 9 10 REPEAT x = x + 1 Print x UNTIL x = 10
Count -controlled loop 1 4 9 16 25 36 49 64 81 100 FOR x = 1 TO 10 y = x * x Print y NEXT x
More about constructs Any CASE statement can be written as a series of IF statements. Any WHILE loop, REPEAT UNTIL loop or FOR loop can be rewritten as any of the others. Because the condition of a WHILE loop is at the beginning of the loop, the statements in the loop may never be executed.
Subroutines A subroutine is a set of statements which performs a specific task as part of a main program. When it is defined it is given an identifier (name). The name is used in the main program to ‘call’ the subroutine. This causes the subroutine to execute its statements before returning control to the main program.
Procedures Procedure Square Begin x = y * y End Main Program Begin Procedures are subroutines which just execute their statements and return. They are usually used as instructions in the main program. They can be coded to return a value, but don’t have to. Procedure Square Begin x = y * y End Main Program Begin square() Print x End
Function A sub routine or procedure that returns a value. E.g. Sum function in Excel
Parameters Subroutines (functions and procedures) may have parameters. Item of data that is given to a procedure or function Parameters will have a data type Parameter Procedure PrintName (Name:String, Copies:Integer) PrintName (solanki, 2) Parameter
Arguments The values that are passed to a parameter Procedure GetSquare (Number: Integer) SQNumber = Number * Number End num:=10 GetSquare(num) Argument
Recursion Recursion is when a subroutine (a procedure or a function) calls itself. Such a subroutine is said to be recursive. Every time the recursive subroutine is called, a new copy of the subroutine is created and executed. The copy of the subroutine which called it is suspended until the call returns. Every recursive algorithm can be rewritten as an iterative algorithm and vice-versa. Recursive algorithms can be easier to write and explain, but use may use memory less efficiently than the iterative version of the same algorithm.