Chapter 3. Control Structures and Program Design ► Two broad categories of control statement: ...

24
Chapter 3. Control Chapter 3. Control Structures and Program Structures and Program Design Design Two broad categories of control Two broad categories of control statement: statement: Branches Branches Loops Loops These will make the program more These will make the program more complex. To avoid programming complex. To avoid programming errors, a formal program design errors, a formal program design procedure is needed, which is based procedure is needed, which is based on the technique known as top-down on the technique known as top-down design. design.

Transcript of Chapter 3. Control Structures and Program Design ► Two broad categories of control statement: ...

Page 1: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

Chapter 3. Control Structures Chapter 3. Control Structures and Program Designand Program Design

► Two broad categories of control statement:Two broad categories of control statement: BranchesBranches LoopsLoops

► These will make the program more These will make the program more complex. To avoid programming errors, a complex. To avoid programming errors, a formal program design procedure is formal program design procedure is needed, which is based on the technique needed, which is based on the technique known as top-down design.known as top-down design.

Page 2: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

►3.1 Top-down design techniques3.1 Top-down design techniquesprogram to be solved

design an algorithm (step-by-step procedure) for finding solution

substak

substak

substak

substak

substak

pseudocode

Fortan statement

unit test each program

final progam

pseudocode

pseudocode

pseudocode

pseudocode

Fortan statement

Fortan statement

Fortan statement

Fortan statement

unit test each program

unit test each program

unit test each program

unit test each program

Page 3: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

► 3.2 Pseudocode and Flowcharts3.2 Pseudocode and Flowcharts Stands forms to describe the algorithmStands forms to describe the algorithm Pseudocode: hybrid mixture of English & FortrPseudocode: hybrid mixture of English & Fortr

anan Flowchart: graphical descriptionFlowchart: graphical description

Page 4: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

►3.3 Control Constructs: Branches3.3 Control Constructs: Branches Block IF constructBlock IF construct

Logical IF statementLogical IF statement

if (logical expression) then statement blockend if

logical expression

statement block

.false.

.true.

if (logical expression) statement

Page 5: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

ELSE and ELSE IF clausesELSE and ELSE IF clausesif (logical expression 1) then statement block 1else if (logical expression 2) then statement block 2else statement block nend if

logicalexpression 1

statement block 1

.false.

.true.

logicalexpression 2

.false.

.true.

statement block 2

Page 6: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

Example using Block if: Solve Example using Block if: Solve axax22+bx+c=0 for x given a, b, and c.+bx+c=0 for x given a, b, and c.

Design the algorithm:Design the algorithm:►Read the input data a, b, and cRead the input data a, b, and c►Calculate the rootsCalculate the roots

►Write out the rootsWrite out the roots

242

bacia

bx 042 acb

042 acb

042 acb

a

bx

2

a

acbbx

2

42

Page 7: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

FlowchartFlowchartstart

read a, b, c

echo a, b, c

b**2-4.*a*c < 0 b**2-4.*a*c = 0

write 'The equationhas complex roots.'

write 'The equation has two identical read roots.'

write 'The equation hastwo distinct real roots.'

calculatereal_part, imag_part calcaulate x1 calcaulate x1, x2

writereal + i imagread – i imag

write x1 write x1, x2

stop

.false. .false.

.true. .true.

Page 8: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

progam roots! Purpose:! This program solves for the roots of a quadratic equation of the form! a*x**2 + b*x + c = 0. It calculates the answers regardless of the type of! roots that the equation possesses.implicit none! Declare the variables used in this programreal :: a ! Coefficient of x**2 term of equationreal :: b ! Coefficient of x term of equationreal :: c ! Constant term of equationreal :: discriminant ! Discriminant of the equationreal :: imag_part ! Imaginary part of equation (for complex roots)real :: real_part ! Real part of equation (for complex roots)real :: x1 ! First solution of equation (for real roots)real :: x2 ! First solution of equation (for real roots)

! Prompt the user for the coefficients of the equationwrite(*,*) 'This program solves for the roots of a quadratic 'write(*,*) 'equation of the form a*x**2 + b*x + c = 0. 'write(*,*) 'Enter the coefficients a, b, and c: 'read(*,*) a, b, c

! Echo back coefficientswrite(*,*) 'The coefficients a, b, and c are: ', a, b, c

! Calculate discriminantdiscriminant = b**2 - 4.0*a*c

>Continued on next page...

Page 9: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

...<

! Solve for the roots, depending upon the value of the discriminantif (discriminant > 0.0) then x1 = (-b + sqrt(discriminant)) / (2.0*a) x2 = (-b - sqrt(discriminant)) / (2.0*a) write(*,*) 'This equation has two real roots:' write(*,*) 'x1 = ', x1 write(*,*) 'x2 = ', x2else if (discriminant == 0.0) then x1 = (-b) / (2.0*a) write(*,*) 'This equation has two identical real roots:' write(*,*) 'x1 = x2 = ', x1else ! there are complex roots, so... real_part = (-b) / (2.0*a) imag_part = sqrt(abs(discriminant)) / (2.0*a) write(*,*) 'This equation has complex roots:' write(*,*) 'x1 = ', real_part, ' +i ', imag_part write(*,*) 'x2 = ', real_part, ' -i ', imag_partend if

end program

Page 10: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

"Name" Block IF Constructs"Name" Block IF Constructs[name:] if (logical expression 1) then statement block 1else if (logical expression 2) then [name] statement block 2else [name] statement block nend if [name]

Page 11: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

"Nested" Block IF Constructs"Nested" Block IF Constructs

A better program is:A better program is:

if (test 1) then statement block if (test 2) then statement block if (test 3) then statement block end if end ifend if

outer: if (test 1) then statement block middle: if (test 2) then statement block inner: if (test 3) then statement block end if inner end if middleend if outer

Page 12: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

Example Using Block IF and Nested Block Example Using Block IF and Nested Block IFIF

►Block IFBlock IFif (x > 95) then write(*,*) 'Grade is A'else if (x > 86) then write(*,*) 'Grade is B'else if (x > 76) then write(*,*) 'Grade is C'else if (x > 66) then write(*,*) 'Grade is D'else write(*,*) 'Grade is E'end if

95 < x95 < x AA

86 < x ≤ 9586 < x ≤ 95 BB

76 < x ≤ 8676 < x ≤ 86 CC

66 < x ≤ 7666 < x ≤ 76 DD

0 < x ≤ 660 < x ≤ 66 EE

Page 13: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

►Nested Block IFNested Block IFaif: if (x > 95) then write(*,*) 'Grade is A'else aif bif: if (x > 86) then write(*,*) 'Grade is B' else bif cif: if (x > 76) then write(*,*) 'Grade is C' else cif dif: if (x > 66) then write(*,*) 'Grade is F' else dif write(*,*) 'Grade is F' end if dif end if cif end if bifend if aif

Page 14: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

CASE constructCASE construct[name:] select case (integer case_expression)case (case_selector_1) [name] statement blockcase (case_selector_2) [name] statement block...case default [name] statement blockend select [name]

Page 15: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

►3.4 Control Constructs: Loops3.4 Control Constructs: Loops►while loopswhile loops►iterative loops (counting loops)iterative loops (counting loops)

WHILE LoopWHILE Loopdo statement block if (logical_expression) exit statement blockend do

logical expression

statement block

.false.

.true.

statement block

do while (logical_expression) statement blockend do

logical expression

statement block

.false.

.true.

Page 16: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

Examle: Recall in a set of numbers and calculate the Examle: Recall in a set of numbers and calculate the mean and standard deviation of the datamean and standard deviation of the data

N

iixN

x1

1

)1(

)( 2

11

2

NN

xxNs

N

ii

N

ii

x<0

n=n+1sum_x=sum_x+x

sum_x2=sum_x2+x

.false.

.true.

read x

x_bar = sum_x/nstd_dev = sqrt((n*sum_x2-sum_x**2) / (n*(n-1))

Page 17: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

progam stats_2! Purpose:! To calculate mean and standard deviation of input data set! containing an arbitrary number of input values.! implicit noneinteger :: n = 0 ! The number of input samplesreal :: std_dev ! The standard deviation of the input samplesreal :: sum_x = 0 ! The sum of the input valuesreal :: sum_x2 = 0 ! The sum of the squares of the input valuesreal :: x = 0 ! An input data valuereal :: x_bar ! The average of the input samples

! While loop to read input valuesdo ! Read in next value write(*,*) 'Enter number:' read(*,*) x write(*,*) 'The number is ', x

! Test of loop exit if (x < 0) exit ! Otherwise, accumulate sums n = n + 1 sum_x = sum_x + x sum_x2 = sum_x2 + x**2end do

>Continued on next page...

Page 18: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

...<

! Check to see if we have enough input dataif (n < 2) then write(*,*) 'At least 2 values must be entered!'else x_bar = sum_x / real(n) std_dev = sqrt((real(n)*sum_x2-sum_x**2)/(real(n)*real(n-1))

write(*,*) 'The mean of this data set is:', x_bar write(*,*) 'The standard deviation is: ', std_dev write(*,*) 'The number of data points is:', nend if

end program

Page 19: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

Iterative Loop (Counting loop)Iterative Loop (Counting loop)do index = istart, iend[, incr] statement blockend do

index*incr ≤ iend*incr

statements

.false.

.true.

index = index+incr

index=istart

1. istart, iend, incr can be contatns, variables or expression of integer.

2. If incr is not specified, incr=1.

Page 20: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

progam stats_3! Purpose:! To calculate mean and standard deviation of input data set,! where each input value can be positive, negative or zero.! implicit noneinteger :: i ! Loop indexinteger :: n = 0 ! The number of input samplesreal :: std_dev ! The standard deviation of the input samplesreal :: sum_x = 0 ! The sum of the input valuesreal :: sum_x2 = 0 ! The sum of the squares of the input valuesreal :: x = 0 ! An input data valuereal :: x_bar ! The average of the input samples

! Get the number of data to inputwrite(*,*) 'Enter number of points: 'read(*,*) n

! Check to see if we have enough input dataif (n < 2) then write(*,*) 'At least 2 values must be entered.'else ! Loop to read input values do i = 1, n write(*,*) 'Enter number: ' read(*,*) x write(*,*) 'The number is ', x sum_x = sum_x + x sum_x2 = sum_x2 + x**2

>Continued on next page...

Page 21: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

...<

! Calculate statistics x_bar = sum_x / real(n) std_dev = sqrt((real(n)*sum_x2-sum_x**2)/(real(n)*real(n-1))

write(*,*) 'The mean of this data set is:', x_bar write(*,*) 'The standard deviation is: ', std_dev write(*,*) 'The number of data points is:', n

end program

Page 22: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

►ExamplesExamplesdo i = 1, 10 ...end do

do i = 1, 10, 2 ...end do

do i = 1, 10, -1 ...end do

do i = 3, -3, -2 ...end do

i = 1, 2, 3, ..., 10

i = 1, 3, 5, 7, 9

Initially, 1*(-1) > 10*(-1)so exits

i = 3, 1, -1, -3

Page 23: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

Notes:Notes:do i = 1, 4 ... i = 2end do

do i = 1, 5 ... if (i >= 3) exit ...end do

do i = 1, 5 ...100 ...end dogoto 100

will produce infinite loops

branch out of a do loop is ok

but, branch into the loop is illegal

Page 24: Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

► CYCLE statementCYCLE statement

► EXIT statementEXIT statement

► Nesting loops (loop within loop)Nesting loops (loop within loop)

do i = 1, 5 if (i == 3) cycle write(*,*) iend do

do i = 1, 5 if (i == 3) exit write(*,*) iend do

do i = 1, 3 do j = 1, 4 write(*,*) i, j end doend do

the output will be 1, 2, 4, 5

the output will be 1, 2

1 1 1 2 1 3 1 4 2 1 2 2 ...