Download presentation

Presentation is loading. Please wait.

Published byTracy Ford Modified over 2 years ago

1
Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured and Object-Oriented Design, 7e

2
Introduction In a SIMPLE SEQUENCE the steps are carried out in a sequential manner. It may be desirable to vary the sequence of processing steps carried out within a solution algorithm.

3
Billing Problem (Flowchart)

4
Billing Example Decision symbol The diamond-shaped symbol. A choice between two alternative paths, or sequences of instructions, is made.

5
Pseudocode Similar to some high-level programming languages, but it does not require that we follow strict rules as we would if actually writing a program.

6
Pseudocode—Introduction

7
IFTHENELSE Control Structure IFTHENELSE control structure A small, circular symbol, called a connector symbol, is used to represent the decision-making logic within the IFTHENELSE pattern

8
IFTHENELSE--Generic

9
Time Card Problem

10
Time Card Example No-function condition—null ELSE The no-function condition is represented by enclosing the key-word ELSE in parentheses.

11
Sample Problem 3.1 Payroll Problem Compute the pay for an employee. Regular pay will be computed as hours (through 40) times rate. Overtime pay (1.5 times hours times rate) for all hours worked over 40.

12
Payroll Problem (Flowchart)

13
Payroll Problem (Flowchart) cont.

14
Payroll Problem (Pseudocode)

15
Finding the Smallest Number (Flowchart)

16
Finding the Smallest Number (Pseudocode)

17
Sample Problem 3.3 Bank Problem Compute the new balance in a customer's bank account. A deposit (code of 1) or withdrawal (code of 2)

18
Bank Problem (Flowchart)

19
Bank Problem (Flowchart) cont.

20
Bank Problem (Pseudocode)

21
Character-String Constant The following information would be output as a result of 3 being input and the WRITE statement: Designate character-string by enclosing it in single or double quotation marks, distinguishing it from a variable name. If we included the variable name CODE within the quotation marks:

22
Character-String Constant Assign the value of a character-string constant to a variable by means of a statement. Use a character-string constant in a decision statement. Compared to the letter D. Compared to the value of the variable D.

23
Sample Problem 3.4 Sales Problem The commission rate is based on two factors, the amount of sales and the class to which the sales person belongs.

24
Sample Problem 3.4

25
Sales Problem (Flowchart)

26
Sales Problem (Pseudocode)

27
Sales Problem (Pseudocode) cont.

28
Chapter 4 DOWHILE Control Structure—Counter- Controlled Loops © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured and Object-Oriented Design, 7e

29
Introduction Program loop A sequence of processing steps that may be repeated.

30
Adding Six Numbers (No Loop)

31
Introduction Practical limitations of this approach What if you revise this to compute and output the sum of 100 numbers?

32
Calculator Simulation (No Loop) Using a calculator:

33
Calculator Simulation (Loop)

34
Adding Six Numbers

35
Adding Six Numbers cont.

36
Problem (Adding Six Numbers) Preparation symbol An operation performed on data in preparation for a major sequence of operations. Initialization step. Accumulator A variable holding the sum of a group of values. Counter is a special type of accumulator. Adds or accumulates by a constant amount. Partial sum—ACCUM is used to hold the sum of the numbers so far.

37
Adding Six Numbers--Simulation

39
The DOWHILE Loop Connector symbol A collector at the beginning of the loop. DOWHILE Loop Leading-decision program loop The test to determine whether the loop should be executed or exited is encountered immediately upon entering the loop. If the tested condition is not true the first time it is tested, the remaining steps in the loop are not executed at all.

40
The DOWHILE Loop Properly formed DOWHILE loop: Place the loop test before any other step within the loop (leading decision). Place the loop steps in the YES path of the loop test. Indicated that the loop will exit in the NO path of the loop test.

41
DOWHILE Loop-Generic

42
Counter-Controlled Loops Counter-controlled loop The algorithm clearly shows the number of times the loop steps will be done. The loop is controlled by a counter. The number of times the loop will be executed is known or preset. COUNT is often referred to as the loop control variable.

43
Simple Counter-Controlled Loops-- Generic

44
Payroll with Counter Loop Payroll problem from Chapter 3 except for 10 employees.

45
Payroll Problem—10 Employees (Flowchart)

46
Payroll Problem—10 Employees (Flowchart) cont.

47
Payroll Problem—10 Employees (Pseudocode)

48
Averaging Problem with Counter Loop Problem A computer program that will compute and print a student's term average. Each student has five scores.

49
Averaging Problem (Flowchart)

50
Averaging Problem (Flowchart) cont.

51
Averaging Problem (Pseudocode)

52
Header Record Logic Header record logic A more flexible approach to loop control The number of loop iterations can vary with each program execution. Header record Specifies how many additional input records will follow.

53
Header Record Logic--Generic

54
Payroll with Header Record Problem: Sample Problem 4.1 (Payroll) including a header record. Input record contains the number of employees.

55
Payroll Problem with Header Record (Flowchart)

56
Payroll Problem with Header Record (Flowchart) cont.

57
Payroll Problem with Header Record (Pseudocode)

58
Averaging Problem with Header Record Problem: The number of individual scores that must be added will depend on the number of assignments completed.

59
Averaging Problem with Header Record (Flowchart)

60
Averaging Problem with Header Record (Flowchart) cont.

61
Averaging Problem with Header Record (Pseudocode)

62
The No-Data Condition What if N is less than or equal to 0? No path will be taken immediately. The step following involves a division by N. A division by 0 will be the result.

63
Averaging Problem with No-Data Test (Flowchart)

64
Averaging Problem with No-Data Test (Flowchart) cont.

65
Averaging Problem with No-Data Test (Pseudocode)

66
Proper Programs Any solution algorithm can be expressed using the three basic patterns of logic. SIMPLE SEQUENCE IFTHENELSE DOWHILE

67
Proper Programs Building-block concept Each basic pattern is characterized by a single point of entrance and a single point of exit. Contained patterns are nested. A solution algorithm should have only one entry point and only one exit point. A program that can be viewed as a single statement is called a proper program. The algorithms presented thus far are all examples of proper programs.

68
Enrichment Visual Basic Graphical user interface for the problem involving the addition of six numbers.

69
Enrichment If the user clicks the Cancel button instead of the OK button, no value will be assigned to decNumber. This could cause an error. If the user enters a non-numeric value in the input box, another type of error may occur.

Similar presentations

OK

Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.

Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on biodegradable and non biodegradable materials landfill Download ppt on 2d and 3d shapes Ppt on energy giving food group Download free ppt on srinivasa ramanujan Ppt on event handling in java Ppt on biography of bill gates Ppt on fdi in india download Ppt on networking related topics to economics Ppt on diodes Ppt on green revolution and its impact on india