The structure of programming

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
CS0004: Introduction to Programming Repetition – Do Loops.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Program Design and Development
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
2015 Pre-Release Practice Click the button to try a random exam-style question. Click on the reveal button to check your answer.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Invitation to Computer Science, Java Version, Second Edition.
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)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
I Power Int 2 Computing Software Development High Level Language Constructs.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
I Power Higher Computing Software Development High Level Language Constructs.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
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.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Chapter 6 Questions Quick Quiz
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
CS314 – Section 5 Recitation 9
Identify the Appropriate Method for Handling Repetition
CIS199 Test Review 2 REACH.
Chapter 6: Loops.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion DRILL: Please take out your notes on Recursion
For loop, definite vs indefinite iteration, range, random
Programming Fundamentals
Counted Loops.
Chapter 5 Structures.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Recursion Chapter 11.
Algorithm Discovery and Design
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
3 Control Statements:.
Topics Introduction to Repetition Structures
` Structured Programming & Flowchart
Computer Science Core Concepts
Basics of Recursion Programming with Recursion
Introduction to Repetition Structures
ICT Programming Lesson 3:
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Flow of Control.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
The structure of programming
Thinking procedurally
REPETITION Why Repetition?
LOOP Basics.
COMPUTING.
Presentation transcript:

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.