Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.

Slides:



Advertisements
Similar presentations
Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.
Advertisements

Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal   Less than.
Programming in Visual Basic
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Visual Basic Statements
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.
Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding.
Solving Quadratic Equations Section 1.3
Introduction to FORTRAN-90 University of Liverpool course.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Conditional Control Flow Constructs. Sequential Control Flow Execution order follows the textual order straight line flow Many simple problems can not.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
Copyright © Cengage Learning. All rights reserved. 4 Quadratic Functions.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
CPS120: Introduction to Computer Science Decision Making in Programs.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
1. 2. * Often times we are not able to a quadratic equation in order to solve it. When this is the case, we have two other methods: completing the square.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
The Quadratic Formula Students will be able to solve quadratic equations by using the quadratic formula.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Control statements Mostafa Abdallah
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
Copyright © Curt Hill The C++ IF Statement The most important decision statement Part 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Copyright © Cengage Learning. All rights reserved. 1 Equations, Inequalities, and Mathematical Modeling.
College Algebra B Unit 8 Seminar Kojis J. Brown Square Root Property Completing the Square Quadratic Equation Discriminant.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
1.2 Quadratic Equations. Quadratic Equation A quadratic equation is an equation equivalent to one of the form ax² + bx + c = 0 where a, b, and c are real.
Chapter 4 – C Program Control
Chapter 4: Making Decisions.
Solving quadratics methods
Multiple variables can be created in one declaration
The Selection Structure
Copyright © 2017, 2013, 2009 Pearson Education, Inc.
Chapter 4: Making Decisions.
Learning Resource Services
Introduction to C++ Programming
“Exploring Quadratic Functions”
Quadratic Equations and Functions
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
Chapter 3: Selection Structures: Making Decisions
Chapter 5 Decisions.
Chapter 3: Selection Structures: Making Decisions
Chapter 4 - Program Control
Presentation transcript:

Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006

Selection2 FORTRAN Selection Last lecture we introduced the IF…THEN..ELSE…END IF statement IF (logical-expression) THEN first statement block, s 1 ELSE second statement block, s 2 END IF

Fall, 2006Selection3 ! ! Solve Ax^2 + Bx + C = 0 ! PROGRAM QuadraticEquation IMPLICIT NONE ! **** Same old declarations and set up **** d = b*b - 4.0*a*c IF (d >= 0.0) THEN ! is it solvable? d = SQRT(d) root1 = (-b + d)/(2.0*a) root2 = (-b - d)/(2.0*a) WRITE(*,*) "Roots are ", root1, " and ", root2 ELSE ! complex roots WRITE(*,*) "There is no real root!" WRITE(*,*) "Discriminant = ", d END IF END PROGRAM QuadraticEquation

Fall, 2006Selection4 Semantics of IF…THEN…ELSE…END IF  Evaluate the logical expression. It can have value.TRUE. or value.FALSE.  If the value is.TRUE., evaluate s 1, the first block of statements  If the value is.FALSE., evaluate s 2, the second block of statements  After finishing whichever of s 1 or s 2 that was chosen, execute the next statement following the END IF

Fall, 2006Selection5 What’s Going On? What is a “logical expression” ?

Fall, 2006Selection6 What’s Going On? What is a “logical expression” ? Where do the values.TRUE. and.FALSE. come from?

Fall, 2006Selection7 What’s Going On? What is a “logical expression” ? Where do the values.TRUE. and.FALSE. come from? What are those periods around the words true and false?

Fall, 2006Selection8 Logical Data Type  FORTRAN has a LOGICAL data type, just like it has INTEGER and REAL types  Each type has its associated values

Fall, 2006Selection9 Logical Data Type  FORTRAN has a LOGICAL data type, just like it has INTEGER and REAL types  Each type has its associated values  There are only two values in the type LOGICAL, true and false

Fall, 2006Selection10 Logical Data Type  FORTRAN has a LOGICAL data type, just like it has INTEGER and REAL types  Each type has its associated values  There are only two values in the type LOGICAL, true and false  To enable the compiler to distinguish these values from variables, we represent them with periods around the words

Fall, 2006Selection11 Logical Data Type  We can declare variables of type LOGICAL LOGICAL :: positive_x, condition

Fall, 2006Selection12 Logical Data Type  We can declare variables of type LOGICAL LOGICAL :: positive_x, condition  We can assign values to them condition =.TRUE. positive_x = x > 0

Fall, 2006Selection13 Logical Data Type  We can declare variables of type LOGICAL LOGICAL :: positive_x, condition  We can assign values to them condition =.TRUE. positive_x = x > 0  These variables can only take on one of the two values of type logical

Fall, 2006Selection14 Logical Expressions Logical expressions, such as those that appear in IF statements, return a logical value That is, they are expressions which evaluate to.TRUE. or.FALSE. We have operators that return logical values.

Fall, 2006Selection15 Relational Operators  Relational operators compare two values and return the result.TRUE. or.FALSE., >=, ==, /=

Fall, 2006Selection16 Relational Operators  Relational operators compare two values and return the result.TRUE. or.FALSE., >=, ==, /=  Relational operators are of lower precedence than all arithmetic operators >= 3 * 3 .TRUE.

Fall, 2006Selection17 Relational Operators  Relational operators compare two values and return the result.TRUE. or.FALSE., >=, ==, /=  Relational operators are of lower precedence than all arithmetic operators >= 3 * 3 .TRUE.  There is no associativity a < b < c  illegal

Fall, 2006Selection18 Danger Lurks

Fall, 2006Selection19 == or = ? Note that == is the FORTRAN (and the C) syntax for a relational operator meaning “is equal to” The expression x == y has the value.TRUE. if x and y are equal and.FALSE. if x and y are not equal A single equal sign (=) is the FORTRAN (and C) syntax for assignment The statement x = y means assign the value of y to the variable x

Fall, 2006Selection20 == or = ? In FORTRAN you will get an error message if you use either operator incorrectly Later on, when we study C, we will C that the program could work and give totally incorrect results if you confuse these operators

Fall, 2006Selection21 Is A Number Even or Odd? IF (MOD(number, 2) == 0) THEN WRITE(*,*) number, " is even" ELSE WRITE(*,*) number, " is odd" END IF

Fall, 2006Selection22 Is A Number Even or Odd? (alternate) IF (number/2*number == number) THEN WRITE(*,*) number, " is even" ELSE WRITE(*,*) number, " is odd" END IF

Fall, 2006Selection23 Find Absolute Value REAL :: x, absolute_x x =..... IF (x >= 0.0) THEN absolute_x = x ELSE absolute_x = -x END IF WRITE(*,*) “The absolute value of “,& x, “ is “, absolute_x Note the use of & to indicate “continue on next line”

Fall, 2006Selection24 Which value is smaller? INTEGER :: a, b, min READ(*,*) a, b IF (a <= b) THEN min = a ELSE min = b END IF WRITE(*,*) “The smaller of “, a, & “ and “, b, “ is “, min

Fall, 2006Selection25 The Missing ELSE The IF-THEN-ELSE-END IF form allows us to choose between two alternatives There is another simpler selection mechanism that allows us to choose whether or not to perform a single block of actions We either perform the actions and go on, or skip them and go on

Fall, 2006Selection26 IF-THEN-END IF Syntax: IF (logical expression) THEN block of statements, s 1 END IF

Fall, 2006Selection27 IF-THEN-END IF Syntax: IF (logical expression) THEN block of statements, s 1 END IF Semantics:  Evaluate the logical expression  If it evaluates to.TRUE. execute s 1 and then continue with the statement following the END IF  If the result is.FALSE. skip s 1 and continue with the statement following the END IF

Fall, 2006Selection28 Examples of IF-THEN-END IF absolute_x = x IF (x < 0.0) THEN absolute_x = -x END IF WRITE(*,*) "The absolute value of ", x, & " is ", absolute_x

Fall, 2006Selection29 Examples of IF-THEN-END IF INTEGER :: a, b, min READ(*,*) a, b min = a IF (a > b) THEN min = b END IF WRITE(*,*) "The smaller of ", & a, " and ", b, " is ", min

Fall, 2006Selection30 Logical IF An even simpler form is sometimes useful. Syntax: IF (logical expression) single-statement Semantics: This statement is equivalent to IF (logical expression) THEN single-statement END IF The single-statement cannot be an IF or we might end up with an ambiguous statement

Fall, 2006Selection31 Examples of Logical IF absolute_x = x IF (x < 0.0) absolute_x = -x WRITE(*,*) "The absolute value of ", x, & " is","absolute_x

Fall, 2006Selection32 Examples of Logical IF INTEGER :: a, b, min READ(*,*) a, b min = a IF (a > b) min = b WRITE(*,*) "The smaller of ",& a, " and ", b, " is ", min

Fall, 2006Selection33 Quadratic Roots Revisited The problem of finding the roots of a quadratic is a bit more complicated than we have been assuming If the discriminant is zero there is only a single root

Fall, 2006Selection34 ! ! Solve Ax^2 + Bx + C = 0 ! Detect complex roots and repeated roots. ! PROGRAM QuadraticEquation IMPLICIT NONE ! **** same old declarations and setup statements omitted **** d = b*b - 4.0*a*c IF (d > 0.0) THEN ! distinct roots? d = SQRT(d) root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root WRITE(*,*) 'Roots are ', root1, ' and ', root2 ELSE IF (d == 0.0) THEN ! repeated roots? WRITE(*,*) 'The repeated root is ', -b/(2.0*a) ELSE ! complex roots WRITE(*,*) 'There is no real roots!‘ WRITE(*,*) 'Discriminant = ', d END IF END PROGRAM QuadraticEquation

Fall, 2006Selection35 IF-THEN-ELSE IF-END IF The nested IF statements in the last example are a bit complicated When we use IF to select between several (not just two) alternatives, we end up with more than a single END IF, one for each of the branches This can be simplified

Fall, 2006Selection36 Syntax of IF-THEN-ELSE IF-END IF IF (logical-exp, e 1 ) THEN statement block, s 1 ELSE IF (logical-exp, e 2 ) THEN statement block, s 2 ELSE IF (logical-exp, e 3 ) THEN statement block, s ELSE statement block, s e END IF

Fall, 2006Selection37 Semantics of IF-THEN-ELSE IF-END IF Evaluate e 1 If the result is.TRUE., execute s 1 and go on to the statement that follows the END IF If the result is.FALSE., evaluate e 2. If it is.TRUE., execute s 2 and go on to the statement that follows the END IF If the result of e 2 is false, repeat this process. If none of the expressions, e i evaluate to.TRUE., execute s e and then go on to the statement that follows the END IF

Fall, 2006Selection38 ! ! Solve Ax^2 + Bx + C = 0 ! Detect complex roots and repeated roots. ! PROGRAM QuadraticEquation IMPLICIT NONE ! **** same old declarations and setup statements omitted **** d = b*b - 4.0*a*c IF (d > 0.0) THEN ! distinct roots? d = SQRT(d) root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root WRITE(*,*) 'Roots are ', root1, ' and ', root2 ELSE IF (d == 0.0) THEN ! repeated roots? WRITE(*,*) 'The repeated root is ', -b/(2.0*a) ELSE ! complex roots WRITE(*,*) 'There is no real roots!‘ WRITE(*,*) 'Discriminant = ', d END IF END PROGRAM QuadraticEquation

Fall, 2006Selection39 Quadratic Roots Final Version The problem of finding the roots of a quadratic has some more complications What if a is zero. Dividing by 2a would cause an error.

Fall, 2006Selection40 Quadratic Roots Final Version The problem of finding the roots of a quadratic has some more complications What if a is zero. Dividing by 2a would cause an error. If a is zero, the equation is linear, not quadratic

Fall, 2006Selection41 Quadratic Roots Final Version The problem of finding the roots of a quadratic has some more complications What if a is zero. Dividing by 2a would cause an error. If a is zero, the equation is linear, not quadratic If a and b are zero but c isn’t there is no solution

Fall, 2006Selection42 ! ! Solve Ax^2 + Bx + C = 0 ! Now, we are able to detect the following: ! (1) unsolvable equation ! (2) linear equation ! (3) quadratic equation ! (a) distinct real roots ! (b) repeated root ! (c) no real roots ! PROGRAM QuadraticEquation IMPLICIT NONE REAL :: a, b, c REAL :: d REAL :: root1, root2 ! read in the coefficients a, b and c READ(*,*) a, b, c

Fall, 2006Selection43 IF (a == 0.0) THEN ! could be a linear equation IF (b == 0.0) THEN ! the input becomes c = 0 IF (c == 0.0) THEN ! all numbers are roots WRITE(*,*) 'All numbers are roots‘ ELSE ! Unsolvable WRITE(*,*) 'Unsolvable equation‘ END IF ELSE ! linear equation WRITE(*,*) 'This is linear equation, root = ', -c/b END IF ELSE ! ok, we have a quadratic equation d = b*b - 4.0*a*c IF (d > 0.0) THEN ! distinct root d = SQRT(d) root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root WRITE(*,*) 'Roots are ', root1, ' and ', root2 ELSE IF (d == 0.0) THEN ! repeated roots? WRITE(*,*) 'The repeated root is ', -b/(2.0*a) ELSE ! complex roots WRITE(*,*) 'There is no real roots!‘ WRITE(*,*) 'Discriminant = ', d END IF END PROGRAM QuadraticEquation

Fall, 2006Selection44 What Day is Tomorrow? Here is a new problem to solve. Given today’s date (day,month,year) Compute and output tomorrow’s date

Fall, 2006Selection45 What Day is Tomorrow? Here is a new problem to solve. Given today’s date (day,month,year) Compute and output tomorrow’s date What’s the problem?

Fall, 2006Selection46 What Day is Tomorrow? Here is a new problem to solve. Given today’s date (day,month,year) Compute and output tomorrow’s date What’s the problem? If the date is the last day of the month, we have to update the day and month If it is the last day of the year, we also have to update the year

Fall, 2006Selection47 First Validate the Data program nextday implicit none integer day, month, year integer lastday write (*,*) "Please enter the date, day month and year:" read (*,*) day, month, year ! validate month if (month 12) then write (*,*) "Invalid month" stop end if ! Validation of year and day omitted to save space

Fall, 2006Selection48 Compute the last day of the month if (month == 1.or. month == 3.or. month == 5.or. month == 7.or. month == 8.or. month == 10.or. month == 12) then lastday =31 else if (month == 4.or. month == 6.or. month == 9.or. month == 12) then lastday =30 else if ((mod(year,4) == 0.and. mod(year,100) /= 0).or. mod(year,400) == 0) then lastday = 29 else lastday = 28 end if

Fall, 2006Selection49 Compute Tomorrow’s Date ! The usual case day = day + 1 ! Handling the end of the month or year if (day > lastday) then day = 1 month = month + 1 if (month > 12) then month = 1 year = year + 1 end if write (*,*) day, month, year end program nextday

Fall, 2006Selection50 Logical Operators In addition to relational operators, more complex logical expressions can be formed using logical operators The Logical Operators listed in order of decreasing precedence are:.NOT..AND. (or &&).OR. (or ||).EQV. (or ==),.NEQV. (or /=) The precedence of all logical operators is lower than all relational operators They all associate from left to right

Fall, 2006Selection51 Area of a Triangle Heron’s formula gives the area of a triangle in terms of the lengths of its sides. Where a, b, and c are the lengths of the sides and

Fall, 2006Selection52 Area of a Triangle To use it, we must make sure that the sides form a triangle. There are two necessary and sufficient conditions: 1.All side lengths must be positive 2.The sum of any two sides must be greater than the third

Fall, 2006Selection53 Area of a Triangle (program preamble) ! ! Compute the area of a triangle using Heron's formula ! PROGRAM HeronFormula IMPLICIT NONE REAL :: a, b, c ! three sides REAL :: s ! half of perimeter REAL :: Area LOGICAL :: Cond_1, Cond_2 READ(*,*) a, b, c

Fall, 2006Selection54 Area of a Triangle (main body of program) Cond_1 = (a > 0.).AND. (b > 0.).AND. (c > 0.0) Cond_2 = (a+b > c).AND. (a+c > b).AND. (b+c > a)] IF (Cond_1.AND. Cond_2) THEN s = (a + b + c) / 2.0 Area = SQRT(s*(s-a)*(s-b)*(s-c)) WRITE(*,*) "Triangle area = ", Area ELSE WRITE(*,*) "ERROR: this is not a triangle!“ END IF END PROGRAM HeronFormula

Fall, 2006Selection55 Data Type Character (A Brief Digression) We have seen character string constants in examples. “Hello World”

Fall, 2006Selection56 Data Type Character (A Brief Digression) We have seen character string constants in examples. “Hello World” FORTRAN also allows us to declare variables that can hold character string values CHARACTER(LEN=5) :: message_1 CHARACTER(LEN=20) :: message_2

Fall, 2006Selection57 Data Type Character (A Brief Digression) We have seen character string constants in examples. “Hello World” FORTRAN also allows us to declare variables that can hold character string values CHARACTER(LEN=5) :: message_1 CHARACTER(LEN=20) :: message_2 We can assign values to these variables. message_1 = “Hello World” message_2 = “Hello World”

Fall, 2006Selection58 Data Type Character (A Brief Digression) What happens if we assign values that don't match the declared length CHARACTER(LEN=5) :: message_1 CHARACTER(LEN=20) :: message_2 message_1 = “Hello World” message_2 = “Hello World”

Fall, 2006Selection59 Data Type Character (A Brief Digression) What happens if we assign values that don't match the declared length CHARACTER(LEN=5) :: message_1 CHARACTER(LEN=20) :: message_2 message_1 = “Hello World” message_2 = “Hello World” If the length is too short, the string is truncated message_1 contains “Hello”

Fall, 2006Selection60 Data Type Character (A Brief Digression) What happens if we assign values that don't match the declared length CHARACTER(LEN=5) :: message_1 CHARACTER(LEN=20) :: message_2 message_1 = “Hello World” message_2 = “Hello World” If the length is too short, the string is truncated message_1 contains “Hello” If it is too long it is padded with extra blanks message_2 contains “Hello World_________”

Fall, 2006Selection61 Operations on Character Strings Comparison using relational operators The ordering for comparison is called lexicographic (or dictionary) ordering A string is less than another if it would come first in the dictionary Concatenation (//) We can join two strings together by concatenating them CHARACTER(len=21) :: instructor CHARACTER(len=8) :: surname surname = “Friedman” instructor = “Prof. ” // “Nathan ” // surname