Structured programming

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
Concepts when Python retrieves a variable’s value Namespaces – Namespaces store information about identifiers and the values to which they are bound –
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Outline 4.1 Introduction 4.2 Program Components in Python 4.3 Functions 4.4Module.
 2002 Prentice Hall. All rights reserved. 1 Functions Purpose –Building blocks for the program (separate functionality in independant parts) –Avoid code.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
The University of Texas – Pan American
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Introduction to Python
Ch 7: JavaScript Control Statements I.
And now for something completely different . . .
PH2150 Scientific Computing Skills
Structured Program
3 Control Statements:.
Chapter 3 – Control Structures
2.6 The if/else Selection Structure
COMPUTER PROGRAMMING SKILLS
Presentation transcript:

Structured programming 3 types of control structure Sequential structure Built into Python Selection structure The if statement The if/else statement The if/elif/else statement Repetition structure The while repetition structure The for repetition structure All have one entry point and one exit point

Sequence Control Structure add grade to total add 1 to counter total = total + grade; counter = counter + 1; Fig. 3.1 Sequence structure flowchart with pseudo code.

if Selection Structure print “Passed” Grade >= 60 true false Fig. 3.3 if single-selection structure flowchart.

if/else Structure false true Grade >= 60 print “Passed” print “Failed” false true Fig. 3.4 if/else double-selection structure flowchart.

if/elif/else Selection Structure condition a true false . condition z default action(s) condition b case a action(s) case b action(s) case z action(s) if statement first elif statement last elif statement else statement Fig. 3.5 if/elif/else multiple-selection structure.

while Repetition Structure Repetition Structures Allow a program to repeat an action while a condition is true Using while Repetition Action(s) contained within the body of the loop Condition should evaluate to false at some point Otherwise infinite loop and program hangs

while Repetition Structure true false Product = 2 * product Product <= 1000 Fig. 3.8 while repetition structure flowchart.

Greatest common divisor algorithm Get two integers a, b > 0 from the user If a and b are the same, GCD = a, exit Otherwise, repeat: If a > b: Replace a with a % b. If a is now 0, GCD = b, exit. Case b > a analogous

gcd.py Note % operator for table if/else indentation: dangling else

Running the program Input first integer > 0: 81 Input second integer > 0: 45 a b 81 45 36 45 36 9 The greatest common divisor of 81 and 45 is 9

for Repetition Structure The for loop Function range is used to create a list of values range ( integer ) Values go from 0 up to given integer (i.e., not including) range ( integer, integer ) Values go from first up to second integer range ( integer, integer, integer ) Values go from first up to second integer but increases in intervals of the third integer This loop will execute howmany times: for counter in range ( howmany ): and counter will have values 0, 1,.. howmany-1

Makes the counter go from zero to nine 1 # Fig. 3.18: fig03_18.py 2 # Counter-controlled repetition with the 3 # for structure and range function. 4 5 for counter in range( 10 ): 6 print counter Fig03_18.py Program Output Makes the counter go from zero to nine 0 1 2 3 4 5 6 7 8 9

range function Fig. 3.19 Function range. Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> range( 10 ) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Fig. 3.19 Function range. Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> range( 10, 0, -1 ) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] Fig. 3.20 Function range with a third value.

Another example Fig03_22.py Program Output 1 # Fig. 3.22: fig03_22.py 2 # Summation with for. 3 4 sum = 0 5 6 for number in range( 2, 101, 2 ): 7 sum += number 8 9 print "Sum is", sum Loops from 2 to 101 in increments of 2 A sum of all the even numbers from 2 to 100 Sum is 2550

3.15 break and continue Statements The break statement Used to make a loop stop looping The loop is exited and no more loop code is executed The continue statement Used to continue the looping process All following actions in the loop body are not executed But the loop will continue to run

When x equals 5 the loop breaks. Only up to 4 will be displayed 1 # Fig. 3.24: fig03_24.py 2 # Using the break statement in a for structure. 3 4 for x in range( 1, 11 ): 5 6 if x == 5: 7 break 8 9 print x, 10 11 print "\nBroke out of loop at x =", x The loop will go from 1 to 10 Fig03_24.py Program Output When x equals 5 the loop breaks. Only up to 4 will be displayed Shows that the counter does not get to 10 like it normally would have 1 2 3 4 Broke out of loop at x = 5

continue skips rest of body but continues loop Fig03_26.py Program Output 1 # Fig. 3.26: fig03_26.py 2 # Using the continue statement in a for/in structure. 3 4 for x in range( 1, 11 ): 5 6 if x == 5: 7 continue 8 9 print x, 10 11 print "\nUsed continue to skip printing the value 5" The loop will continue if the value equals 5 The value 5 will never be output but all the others will 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5

Calling/invoking a function Details Functions Purpose Building blocks for the program (separate functionality in independant parts) Avoid code repetition Calling/invoking a function functionName ( argument1, argument2 ) Details Variables created in a function are local to that function Arguments are also local variables A function may return a result using the return statement

Modules A module contains useful function definitions and other elements All of which are related in some way The import keyword is used to include a module A function from a module is invoked like this: moduleName.functionName( arguments )

Functions in math Module 2.3 (#1, Aug 4 2003, 10:37:16) [GCC 3.1.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> print sqrt( 900 ) Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'sqrt' is not defined >>> import math >>> print math.sqrt( 900 ) 30.0 Fig. 4.2 Function sqrt of module math.

User-defined functions Functions must be defined before they are used: def functionName ( paramList ): paramList is a comma separated list of parameters The actions of the function then follow They should all be indented appropriately The actions are also called the block or the function body

The function returns the passed value multiplied by itself 1 # Fig. 4.4: fig04_04.py 2 # Creating and using a programmer-defined function. 3 4 # function definition 5 def square( y ): 6 return y * y 7 8 for x in range( 1, 11 ): 9 print square( x ), 10 11 print Fig04_04.py Program Output This is a function definition, the function is called square and is passed the value y The function returns the passed value multiplied by itself This calls the square function and passes it the value x   1 4 9 16 25 36 49 64 81 100

This is a function that receives three values Fig04_05.py 1 # Fig. 4.5: fig04_05.py 2 # Finding the maximum of three integers. 3 4 def maximumValue( x, y, z ): 5 maximum = x 6 7 if y > maximum: 8 maximum = y 9 10 if z > maximum: 11 maximum = z 12 13 return maximum 14 15 a = int( raw_input( "Enter first integer: " ) ) 16 b = int( raw_input( "Enter second integer: " ) ) 17 c = int( raw_input( "Enter third integer: " ) ) 18 19 # function call 20 print "Maximum integer is:", maximumValue( a, b, c ) 21 print # print new line 22 23 d = float( raw_input( "Enter first float: " ) ) 24 e = float( raw_input( "Enter second float: " ) ) 25 f = float( raw_input( "Enter third float: " ) ) 26 print "Maximum float is: ", maximumValue( d, e, f ) 27 print 28 29 g = raw_input( "Enter first string: " ) 30 h = raw_input( "Enter second string: " ) 31 i = raw_input( "Enter third string: " ) 32 print "Maximum string is: ", maximumValue( g, h, i ) This is a function that receives three values Fig04_05.py The function determines the greater of three values and returns it Gets three integers, passes them to the maximumValue function, and displays the results to the user The same process is performed on float and string variables to show the diversity of the function Note: only possible because of dynamic typing! Function parameters not type declared

Fig04_05.py Program Output Enter first integer: 27   Enter first integer: 27 Enter second integer: 12 Enter third integer: 36 Maximum integer is: 36 Enter first float: 12.3 Enter second float: 45.6 Enter third float: 9.03 Maximum float is: 45.6 Enter first string: hello Enter second string: programming Enter third string: goodbye Maximum string is: programming Fig04_05.py Program Output