Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thinking procedurally

Similar presentations


Presentation on theme: "Thinking procedurally"— Presentation transcript:

1 Thinking procedurally
OCR Computing for A Level © Hodder Education 2009

2 What this chapter is about
In this chapter we learn about the ways in which programs are structured to control the way in which they are executed. This includes: The three main programming constructs: Sequence Selection Iteration Subroutines, including procedures and functions Recursion OCR Computing for A Level © Hodder Education 2009

3 The three basic constructs
Sequence: Execute each statement once, in the order in which it is given Selection: There is an option of statements and a condition is used to determine which (if any) statement is executed Iteration: A group of statements is executed a number of times OCR Computing for A Level © Hodder Education 2009

4 Two types of selection construct
The IF statement IF <condition> THEN ... ELSE END IF The ELSE part is optional. 2. The CASE statement SELECT CASE <variable> CASE <value> ... CASE <value 2> END SELECT OCR Computing for A Level © Hodder Education 2009

5 Types of Repetition Construct
Condition-controlled loops WHILE loop WHILE <condition> ... END WHILE 2. REPEAT UNTIL loop REPEAT UNTIL <condition> Count -controlled loop 3. FOR loop FOR <variable> = <start> TO <finish> ... NEXT OCR Computing for A Level © Hodder Education 2009

6 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. Constructs can be nested (one construct put inside another). They should be wholly nested. OCR Computing for A Level © Hodder Education 2009

7 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 / a 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. OCR Computing for A Level © Hodder Education 2009

8 Procedures and Functions
Procedures are subroutines which just execute their statements and return. They are usually used as instructions in the main program. Functions are subroutines which return a single value. They are usually used within expressions in the main program. The value returned replaces the function call in the expression. OCR Computing for A Level © Hodder Education 2009

9 Parameters Subroutines (functions and procedures) may have parameters.
The parameter describes some item of data which needs to be provided to the subroutine when it is called. When a parameter is defined it is given a name (it is usually also given a data type). When the subroutine is called, the value supplied is called the argument. OCR Computing for A Level © Hodder Education 2009

10 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. OCR Computing for A Level © Hodder Education 2009


Download ppt "Thinking procedurally"

Similar presentations


Ads by Google