This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 4 Flow Control The programs presented so far are example of straightline code in which the executable statements are performed exactly once in the order they appear in. Oc casionally we need the computer to process the statement outoforder, depending on the value of the input or of intermediate calculations, or more then once. The process of controling the order of code execution is called flow control . We will visit two types of flow controls in the present lecture: branching and looping 4.1 Conditionals, Branching, and Logical 4.1.1 Quadratic Roots To make our discussion more concrete we will focus on the simple example of calculating the roots of a quadratic equation ax 2 + bx + c = 0 (4.1) The two roots of the equations are given by x = b b 2 4 ac 2 a (4.2) Clearly the nature of the two roots depend on the sign of the discriminant d = b 2 4 ac . For d > 0 there are two distinct real roots, for d = 0 there is a double root, and for d < 0 there are pairs of complex roots. Before we launch on how we code a solution of the quadratic equation we il lustrate the process of turning the mathematical steps into FORTRAN code. The original charting relied on visual diagrams. Here we will use the simple alternative of pseudo code . 1. input a,b,c 21 22 CHAPTER 4. FLOW CONTROL 2. form discriminant d = b 2 4 ac 3. (a) if d < 0 print a message that no real exist and exit. (b) if d = 0 print x = b/ 2 a (c) if d > 0 print x = b + d 2 a 4.1.2 IF statements Clearly the execution flow depends on the value of d and a test is required to determine which FORTRAN statements should be executed. The if statement is designed to do just that. It takes the form: if (logical_expression) then conditional execution statement 1 conditional execution statement 2 . . endif The logical_expression is the test to be performed. If the test returns a value of true the statements between the if and endif are executed, otherwise they are skipped and execution continues after the endif statement. Hence one code that can perform the above calculation is as follows: program quadraticroots implicit none real :: a,b,c,d,x1,x2 read *, a,b,c print *,a=,a, b=, b, c=,c d = b**2  4.0*a*c if (d > 0.0) then d = sqrt(d) x1 = (b+d)/(2.0*a) x2 = (bd)/(2.0*a) print *,x1=,x1 print *,x2=,x2 endif if (d == 0.0) then x1 = (b+sqrt(d))/(2.0*a) x2 = x1 print *,x1=x2=,x1 endif if (d < 0.0) then 4.1. CONDITIONALS, BRANCHING, AND LOGICAL 23 print *,No real root exist endif stop end program quadraticroots The if statement allows multiple forms to be used to build more complicated tests. A first alternative form is the simple if elseif else form....
View Full
Document
 Fall '08
 Staff

Click to edit the document details