1 / 29

Conditional Control Flow Constructs

Conditional Control Flow Constructs. Sequential Control Flow. Execution order follows the textual order straight line flow Many simple problems can not be solved only with such sequential flow Here is an example Problem: Computation of a maximum of two numbers. Solution. Program max2

iram
Download Presentation

Conditional Control Flow Constructs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditional Control Flow Constructs

  2. Sequential Control Flow • Execution order follows the textual order • straight line flow • Many simple problems can not be solved only with such sequential flow • Here is an example • Problem: Computation of a maximum of two numbers

  3. Solution Program max2 Implicit none integer num1,num2, max read *, num1,num2 if (num1 > num2) then !control comes here if num1 > num2 max = num1 !control jumps to the end of the if statement (endif) else !control comes here if num1 < = num2 max = num2 endif print *, max end program max2

  4. Another Problem • Input two numbers • Compute the quotient and remainder of the bigger number divided by the smaller number • The numbers are input in unknown order

  5. Solution Program Quo_Rem1 Implicit none integer num1,num2, quotient, remainder read *, num1,num2 if (num1 > num2) then quotient = num1/num2 ! / is integer division remainder = num1 – (quotient * num2) else quotient = num2/num1 remainder = num2 – (quotient * num1) endif print *, quotient, remainder endprogram Quo_Rem1

  6. Problems with the solution What happens if num1 or num2 is negative? • Problem Specification needs to be clarified What if one of the numbers is 0? • Divide by zero leads to overflow • Should be avoided • need to test it before dividing

  7. Program Quo_Rem2 integer:: num1,num2, temp, quotient, reminderread *, num1,num2if (abs(num1) < abs(num2)) then! abs function returns the absolute value temp = num2 num2 = num1 ! swapping the contents num1 = temp ! of num1 and num2end ifif (.not.(num2 == 0)) then!num2 contains a nonzero value quotient = num1/num2 remainder = num1 - quotient * num2 print *, quotient, remainderelse print *,"cannot divide! one of the numbers is zero"end if

  8. The program Quo_Rem2 • The body of if-then-else is simpler • The else clause is missing • Complex conditions can appear in the if-condition • Successive if-statements allowed

  9. Another Problem • %Nesting of ifsProblem: Compute Maximum of three numbersSolution: ?

  10. A Strategy Let num1,num2,num3 store the three numbers. • Compute the maximum of num1 and num2. Name it max12 • Compute the maximum of num2 and num3. Name it max23 • Compute the maximum of max12 and max23, which is the maximum of num1.num2,num3

  11. Another Strategy • Compute the maximum of num1 and num2, say max12 • Compute the maximum of max12and num3, which is the required maximum? Which is the better strategy? The latter - less number of steps Now we are ready to write the program

  12. Program max3 integer num1,num2,num3,maxread *, num1,num2,num3if (num1 > num2) thenif (num1 > num3) then!num1 > num2,num1 > num3 max = num1else! num1 > num2 and num1 <= num3 max = num3endifelseif (num2 > num3) then !num1 <=num2 > num3 max = num2else! num1 <=num2<=num3 max = num3endifprint *, maxend program max2

  13. Nested If statements • If statements in the then clause or else clause • elseif construct • Arbitrary series of nesting permitted • elseif and else corresponds to the innermost if for which the endif is yet to come • if and endif are like left and right brackets.

  14. Further Observations • else clause can be missing • endif can be dropped if there is only one statement • long series of ifs can be confusing (at most 20 levels allowed) • indentation improves readability • use indentation and comments

  15. Ifconditions • Control flow branches in conditional statements • Branching decided by evaluating the conditions • conditions are expressions of a new type called LOGICAL • Examples: (x > 0), (z == 1), .NOT. (num == 2) • All these involve relational operators: >,== • Relational operators are defined over many data types to compare values

  16. Relations over Arithmetic • Given e1,e2 expressions over integer (or real), • e1 == e2 ( equality ) • e1 < e2 ( less than ) • e1 <= e2 ( less than or equal ) • e1 > e2 ( greater than ) • e1 >= e2 ( greater than or equal ) • e1 /= e2 ( not equal )

  17. LOGICAL VARIABLES • Fortran 90 has a built in LOGICAL DATA TYPE • Expressions involving relational operators are of type LOGICAL • Variables can be declared to have type LOGICAL • Declarations: LOGICAL ::found, goodness • Logical variables assume just two values • .TRUE., .FALSE. • They can be used in if conditions, eg. • if (found) then stop • if (goodness) then x = 0

  18. LOGICAL OPERATORS • Operators over logical type values • They are .not., .and., .or., .eqv., .neqv. • .not. p is .true. iff p is.false. • p .and. q is .true. iff both p and q are .false. • p .or. q is .true. iff one of (or both) p,q .true. • p .eqv. q is .true. iff both p and q has the same truth value • p .neqv. q is .true. iff both p and q have different truth values

  19. Operator precedence • A general logical expression may include arithmetic, relational and logical operators • operator precedence defined to specify order of evaluation • arithmetic operators are evaluated first followed by relational operators • logical operators are evaluated last • precedence amongst logical operators .not. , .and. , .or. , .eqv. and .neqv.

  20. If condition • The condition in an `if' statement is a logical expressions, eg. 1. if((a>=b) .eqv. x) then ... • a,b arithmetic variables, x logical variable 2. if(((rate*prin)> 100) .AND. .NOT. (closed))then ...

  21. Quadratic Equation Solving • Roots of quadratic equation: ax2 + bx + c = 0 • Can have • exactly one root • two real roots • two complex conjugate roots • Type of roots depends upon the discriminant (b2 - 4ac)

  22. A Program to solve the equation program quadraticimplicit nonereal :: a, b, c, disc, x_r, x_r1, x_r2, x_im1, x_im2real, parameter :: eps = 1.0e-6read *, a, b, cif (a == 0.0) then! a is 0, not a quadratic equation print *, "equation is not quadratic"else disc = b*b – 4.0*a*c x_r = -b/(2.0*a)

  23. if ( abs(disc) < eps ) then ! discriminant nearly zeroprint *, "double real root", x_r elseif ( disc > 0 ) then ! two distinct real roots disc = sqrt(disc)/(2.0*a) x_r1 = x_r + disc ! disc temporary variable x_r2 = x_r - discprint *, “two real roots”, x_r1, “ and”, x_r2 else ! disc is negative, complex conjugate roots x_im1 = sqrt(-disc)/(2.0*a) x_im2 = - x_im1print *, "complex conjugate roots", x_r, "+", & x_im1, "i and", x_r, "-", x_im2, "i"endif endif end program quadratic

  24. Comparison of Real Numbers • instead of disc == 0.0 we have checked abs(disc) < eps as condition for double root • misleading results occur otherwise, eg. 0.1x2 - 0.3 x + 0.225 = 0 • has 1.5 as double root • errors in representation give disc > 0.0 • roots obtained are 1.5004526 and 1.4995474

  25. Comparing Real Numbers • double roots undesirable in many applications • two roots close to each other may be treated as double • if abs(disc) is small, roots are close • a parameter eps (epsilon) is usually used for comparing reals • two reals are treated as equal if absolute value of difference is < eps

  26. Quadratic Equations • numerical problems in solving quadratic equations by this method • if two roots differ by orders of magnitude, smaller root cannot be found accurately x2 – 1000.001x + 1.0 = 0 • two real roots 1.0000000e+03, 1.0070801e-03 • what happens if actual roots are 1.0e+4 and 1.0 e-4 ?

  27. Strategies • Strategies are high level descriptions of computations • They are intuitive and understandable to humans • Easier to write, analyze, explore and change compared to programs • Develop strategies first • Only when a strategy is finalized, write the programs • Precise statement of strategies is called Algorithm

  28. Algorithm • Is a sequence of steps • Each step is precise and unambiguous to people • Each step is a high level instruction, that can be carried out by mechanically • Each step can be translated into `low level' programs • Is at a much higher level than HLL itself

  29. Analyzing algorithms • Before writing the program, analyze and choose the efficient algorithm • Metrics for algorithm • Number of steps • Complexity of steps • number of primitive operations (like addition, multiplication, comparison)

More Related