Thinking procedurally

Slides:



Advertisements
Similar presentations
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
Advertisements

Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Copyright © Texas Education Agency, Computer Programming For Loops.
Problem Solving and Algorithms
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
ITEC113 Algorithms and Programming Techniques
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
I Power Higher Computing Software Development High Level Language Constructs.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 6 Questions Quick Quiz
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction.
Identify the Appropriate Method for Handling Repetition
CIS199 Test Review 2 REACH.
Chapter 4 – C Program Control
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion DRILL: Please take out your notes on Recursion
Chapter 3: Decisions and Loops
Chapter 15 Recursion.
Introduction To Flowcharting
Chapter 5: Repetition Structures
Programming Fundamentals
JavaScript: Control Statements.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Doing things more than once
Recursion Chapter 11.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Recursion Data Structures.
Chapter 4 - Program Control
` Structured Programming & Flowchart
Faculty of Computer Science & Information System
Computer Science Core Concepts
Basics of Recursion Programming with Recursion
Introduction to Repetition Structures
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Recursion Taken from notes by Dr. Neil Moore
Java Programming: Chapter 9: Recursion Second Edition
Flow of Control.
The structure of programming
Vocabulary Memory Cards--Sample
Topics Introduction to Repetition Structures
The structure of programming
Presentation transcript:

Thinking procedurally OCR Computing for A Level © Hodder Education 2009

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

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

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

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

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

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

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

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

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