chapter 3 program design and branching structures dr. ali can takinacı İstanbul technical...

17
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]

Post on 20-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

Chapter 3 Program Design and Branching Structures

Dr. Ali Can Takinacıİstanbul Technical University

Faculty of Naval Architecture and Ocean Engineeringİstanbul - Turkey

Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]

Page 2: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

Top-Down Design Techniques

•Top-down design is the process of starting with a large task and breaking it down into smaller, more easily understandable pieces (subtasks) that perform a portion of the desired task.

•Each subtask may in turn be subdivided into smaller subtasks if necessary.

•Once the program is divided into small pieces, each piece can be coded and tested independently.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 2

Page 3: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

1. Clearly state the problem that you are trying to solve.

For example: We were describing, a proper statement of the problem might have been: Design and write a program to solve a system of simultaneous linear equations having real coefficients and with up to 20 equations in 20 unknowns.

2. Define the inputs required by the program and the outputs to be produced by the program.

3. Design the algorithm that you intend to implement in the programAn algorithm is a step-by-step procedure for finding the solution to a problem. It is at this stage in the process that top-down design techniques come into play.

4. Turn the algorithm into Fortran statements5. Test the resulting Fortran program.

Alpha Release: First complete version of the programBeta Relase: The most serious bugs have been removed from the program

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 3

Page 4: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

USE OF PSEUDOCODE AND FLOWCHARTS

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 4

The constructs used to build algorithms can be described in two different ways: pseudocode and flowcharts.

Pseudocode is a hybrid mixture of Fortran and English. It is structured like Fortran, with a separate line for each distinct idea or segment of code, but the descriptions on each line are in English. Each line of the pseudocode should describe its idea in plain, easily understandable English.

For example:

Page 5: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

Flowcharts are a way to describe algorithms graphically. In a flowchart, different graphical symbols represent the different operations in the algorithm, and our standard constructs are made up of collections of one or more of these symbols. Flowcharts are very useful for describing the algorithm implemented in a program after it is completed. However, since they are graphical, flowcharts tend to be cumbersome to modify

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 5

Page 6: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

Common symbols used in flowcharts.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 6

Page 7: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

LOGICAL CONSTANTS, VARIABLES, AND OPERATORS

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

The logical data type contains one of only two possible values: TRUE or FALSE.

A logical constant can have one of the following values: .TRUE. or .FALSE.

(note that the periods are required on either side of the values to distinguish them from variable names). Thus, the following are valid logical constants:

.TRUE .

. FALSE.A logical variable is a variable containing a value of the logical

data type. A logical variable is declared using the LOGICAL statement: LOGICAL :: var1, var2, var3

Page 8: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

Like arithmetic calculations, logical calculations are performed with an assignment statement, whose form is

logical_variable_name = logical_expression

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 8

Some relational operations and their results are given

Page 9: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

Combinational Logic Operators

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 9

Page 10: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

CONTROL CONSTRUCTS: BRANCHES

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 10

Branches are Fortran statements that permit us to select and execute specific sections of code (called blocks) while skipping other sections of code.

They are variations of the IF statement, plus the SELECT CASE construct.

Page 11: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

The Block IF Construct

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 11

The commonest form of the IF statement is the block IF construct.

This construct specifies that a block of code will be executed if and only if a certain logical expression is true. The block IF construct has the form

Page 12: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES© 2010, Dr. ALİ CAN TAKİNACI Slide No: 12

Page 13: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES© 2010, Dr. ALİ CAN TAKİNACI Slide No: 13

Page 14: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

The ELSE and ELSE lF Clauses

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 14

Sometimes we may want to execute one set of statements if some condition is true, and different sets of statements if other conditions are true. If fact, there might be many different options to consider. An ELSE clause and one or more ELSE lF clauses may be added to the block IF construct for this purpose. The block IF construct with an ELSE clause and an ELSE lF clause has the form

Page 15: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES© 2010, Dr. ALİ CAN TAKİNACI Slide No: 15

Page 16: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES

The SELECT CASE Construts

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 16

The SELECT CASE construct is another form of branching construct. It permits a programmer to select a particular code block to execute based on the value of a single integer, character, or logical expression.

The general form of a CASE construct is:

Page 17: Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering

PROGRAM DESIGN AND BRANCHING STRUCTURES© 2010, Dr. ALİ CAN TAKİNACI Slide No: 17

The case_expr may be any integer, character, or logical expression. Each case selector must be an integer, character, or logical value or a range of values. No single value can appear in more than one case selector.

The CASE DEFAULT block is extremely important for good programming design. If an input value in a SELECT CASE statement does not match any of the cases, none of the cases will be executed.