Title: BIL106E
1BIL106E
- Selective EXECUTION
- Control of EXECUTION
2Contents
- Prelude
- Logical Expressions Logical Variable
- IF, IF - ELSE IF constructs
- Comparing Numbers
- Comparing Character Strings
- Case construct
- Named constructs
3Prelude
- This chapter introduces the concept of comparison
between two numbers or two character strings, and
explains how such comparisons can be used to
determine which of two, or more, alternatives
sections of the code obeyed. - In everyday life we frequently encounter a
situation which involves several possible
alternative courses of action, requiring us to
choose one of them based on some decision-making
criteria. - The ability of a program to specify how these
decisions are to be made is one of the most
important aspects of programming
4Prelude
- EXAMPLE Q How do I get to Adana from
Istanbul ? - A It depends how you want to
travel. - if you are in hurry then
- ? you should fly from Atatürk airport to Adana
airport - but if you are a romantic or like trains
then - ? you should take the Toros Express from Istanbul
to Adana - but if you have plenty of time then
- ? you can travel on one of the boats which ply
along the shoreline. - otherwise
- ? you can always go by road
5Prelude
- F provides a very similar construction for this
decision-making problem as can be seen below
- in F syntax
- if (criterion_1) then
- action_1
- else if (criterion_2) then
- action_2
- else if (criterion_3) then
- action_3
- else
- action_4
- endif
- If criterion 1 then
- action 1
- but if criterion 2 then
- action 2
- but if criterion 3 then
- action 3
- otherwise
- action 4
What does this criterion mean and how to
implement it?
6- Prelude
- Logical Expressions Logical Variable
- IF, IF - ELSE IF constructs
- Comparing Numbers
- Comparing Character Strings
- Case construct
- Named constructs
7Logical Expressions Logical Variable
- Logical expressions may be either simple or
compound. - Simple logical expressions are logical constants
or variables or relational expressions of the
form. - expression1 relational-operator expression2
- your-choice "train"
- where both expression1 and expression2 are
numeric or character (or logical) expressions,
and the relational-operator may be any of the
following
8Simple Logical Expressions
- Relational operators and expressions()
- Symbol Meaning
- altb or a.LT.b a is less than b
- agtb or a.GT.b a is greater than b
- ab or a.EQ.b a is equal to b
- altb or a.LE.b a is less than or equal to b
- agtb or a.GE.b a is greater than or equal to b
- a/b or a.NE.b a is not equal to b
- b2 gt 4 a c
- b2 - 4 a c gt 0
- Current-year 2003
() Relational expressions are simple form of
logical expressions
9Compound Logical Expressions
- They are formed by combining logical expressions
by using the logical operators - .not. (negation)
- .and. (conjunction)
- .or. (disjunction)
- .eqv. (equivalence)
- .neqv. (nonequivalence)
- all possible values are displayed in truth tables
10The logical operators .or. .and.
- L1 L2 L1.or.L2 L1.and.L2
- true true true true
- true false true false
- false true true false
- false false false false
11The logical operators .eqv. .neqv.
- L1 L2 L1.eqv.L2 L1.neqv.L2
- true true true false
- true false false true
- false true false true
- false false true false
12Logical operator priorities
- The operations are performed in the following
order (priority rules) - 1.- arithmetic operations and functions
- 2.- relational operations
- 3.- logical operarions in the order mentioned
below - Operator Priority
- .not. Highest
- .and.
- .or.
- .eqv. and .neqv. Lowest
13Examples
- Valid Expressions
- (altb) .or. (cgtd)
- (xlty) .and. (yltz)
- .not. (altb) .eqv. (xlty)
- altb .neqv. xlty
- Invalid expressionsI 1.or.2 (A.and.B) /
0.0 x gt 0.0 .and. gt 1.0 0.0 lt x
lt 1.0
14Logical Variable
- An object of logical type has the valuetrue or
false, or (0 or 1), or (yes or no). - in F syntax.TRUE..FALSE.
- logical variable declaration in F logical
flag1, flag2 - Exampleflag1 .TRUE.if(flag1) then flag2
.FALSE. ... if(flag1 .and.
flag2)then print , "Both flags are TRUE" end
ifend if
15- Prelude
- Logical Expressions Logical Variable
- IF, IF - ELSE IF constructs
- Comparing Numbers
- Comparing Character Strings
- Case construct
- Named constructs
16Comparing numbers ()
- Accuracy/round-off
- Number of significant digits for real numbers
- Do not test whether two numbers are equal
- Test whether their difference is acceptably small
17- Prelude
- Logical Expressions Logical Variable
- IF, IF - ELSE IF constructs
- Comparing Numbers
- Comparing Character Strings
- Case construct
- Named constructs
18Comparing character strings
- F language lays down 6 rules for collecting
sequence of letters, digits and other characters
based on ASCII standard. - 26 upper-case letters can be collected in the
following order ABCDEFGHIJKLMNOPRSTUVWXYZ - 26 lower-case letters can be collected in the
following order abcdefghijklmnoprstuvwxyz - the 10 digits can be collected in the following
order 0 1 2 3 4 5 6 7 8 9 - a space (or blank) is collected before both
letters and digits - digits are all collected before the letter A.
- upper-case letters are collacted before any
lower-case letters.
19ASCII character codes
- ASCII uses codes in the range 0 through 255 (i.e.
A65, B66,Z90). - (See complete table of ASCII characters in
Appendix D of Elliss book)
20httpwww.asciitable.com
21ASCII charecter codes 32-126
22Comparing character strings
- When 2 character operands are being compared
there are 3 distinct stages in the process - 1.- If two operands are not the same length,
the shorter one is treated as though it were
extended on the right with blanks (spaces) until
it is the same length as the longer one. - 2.- The two operands are compared character by
character, starting with the left-most character,
until either a difference is found or the end of
the operand is reached. - 3.- If a difference is found, then the
relationship between these two different
characters defines the relationship between the
two operands, with the character which comes
earlier in collating sequence being deemed to be
the lesser of the two. If no difference is found,
then the strings are considered to be equal.
23Examples
- "A" lt "F" is a "true" logical expression
- "m" gt "b" is a "true" logical expression
- Comparisons of 2 strings is done character by
character, considering the numeric codes. If the
first characters of the strings are the same, the
second characters are compared, if these are the
same, then the third characters are compared,
etc. - "cat" lt "dog" ! is a "true" logical
expression. - "cat" gt "car" ! is a "true" logical
expression. - "June" gt "July" ! is a "true"
logical expression. - Note Comparision is always proceeded regarding
to the ASCII code of characters
24Examples
- Two strings with different lengths are compared
so that blanks are appended to the shorter
string. - "cat" lt "cattle"
- ! is a "true" logical expression, because
blank characters (b) preceds all letters
("catbbb" lt "cattle") - "Adam" gt "Eve"
- ! is false, because A comes before E, thus, less
than E - "Adam" lt "Adamant" ! "Adambbb" lt "Adamant"
- ! is true, because "Adam" has been extended
using 3 blanks a blank always comes before a
letter - "120" lt "1201" ! "120b" lt "1201"
- ! is true because a blank comes before a digit
- "ADAM" lt "Adam"
- ! is true because the second digit "D" lt "d"
(i.e. upper-case letters come before lower-case
letters.
25- Prelude
- Logical Expressions Logical Variable
- IF, IF - ELSE IF constructs
- Comparing Numbers
- Comparing Character Strings
- Case construct
- Named constructs
26IF Constructs
- In the simplest selection structure, a sequence
of statements (called a block of
statements) is executed or bypassed depending on
whether a given logical expression is true or
false. - If the logical expression is true, then the
specified sequence of statements is executed
otherwise it is bypassed. - In either case, execution continues with the
statement in the program following the end if
statement. -
if ( x gt 0.0 ) then y x x z sqrt
(x) end if
if (logical_expression) then statement
sequence end if
27Logical IF Statement
- Fortran also provides a simpler IF construct
called Logical IF statement - if ( logical-expression) statementif (1.5 lt X
.AND. Xlt 2.5) print , X
28General form of the IF construct
- if (logical expression 1) then
- block of F statements-1
- else if (logical expression 2) then
- block of F statements-2
- else
- block of F statements-3
- end if
- if 1.LE is true then 1. SB is executed, if 1. LE
is false and if 2.LE is true then 2. SB is
executed if neither of them is true then last SB
is executed...
29Example Code
- Example 5.5, p126Write a program to read the
coefficients of a quadratic equation and print
its roots.
30Code-1 Ellis, ex. 5.5, p 126
- program quadratic_by_block_if
- real a,b,c,d,sqrt_d,x1,x2
- print ,"Please type a,b and c"
- read ,a,b,c
- d b2 - 4.0ac
- if(dgt0.0) then
- sqrt_dsqrt(d)
- x1(-bsqrt_d)/(aa)
- x2(-b-sqrt_d)/(aa)
- print ,"Two roots",x1,x2
- else if (d0.0l) then
- x1-b/(aa)
- print ,"one root",x1
- else
- print ,"no real roots"
- end if
- end program quadratic_by_block_if
Direct comparision of two real number
31Code-2
Test of the diffrence between two real number
- program quadratic_by_block_if
- real, parameter SMALL1e-6
- real a,b,c,d,sqrt_d,x1,x2
- print ,"Please type a,b and c"
- read ,a,b,c
- db2-4.0ac
- if(dgtSMALL) then
- sqrt_dsqrt(d)
- x1(-bsqrt_d)/(aa)
- x2(-b-sqrt_d)/(aa)
- print ,"Two roots",x1,x2
- else if (dgt-SMALL) then
- x1-b/(aa)
- print ,"one root",x1
- else
- print ,"no real roots"
- end if
- end program quadratic_by_block_if
32Code 3
- !This program inputs a character
- !and test it is whether a UPPER or
- !LOWER letter or is a DIGIT
- program isValid
- character (len 1) ch
- print , "Input one character"
- read , ch
- if( "A" lt ch .and. ch lt "Z" )then
- print , ch , " is a UPPER char"
- else if("a" lt ch .and. ch lt "z") then
- print, ch, " is a LOWER char "
- else if("0" lt ch .and. ch lt "9") then
- print, ch, " is a DIGIT"
- else
- print ,ch , " is None of them"
- end if
- end program isValid
33- Prelude
- Logical Expressions Logical Variable
- IF, IF - ELSE IF constructs
- Comparing Numbers
- Comparing Character Strings
- Case construct
- Named constructs
34The CASE Construct
- A case construct uses an integer expression or a
character expression to determine the required
course of action. - The structure of a case construct
- select case (case expression)
- case (case selector)
- block of statements
- case (case selector)
- block of statements
- ...
- case default
- block of statements
- end select
- The case selector can take one of four forms
- case_value low_value high_value low_valuehigh_v
alue
35- Case expression
- either integer or character expression
- Case selector
- case_value case(10) case(10, 13, 18)
- case_expression case_value
- lower_limit case(0)
- low_limit lt case_expression
- higher_limit case(1)
- case_expression lt high_limit
- low_valuehigh_value
- lower_limit lt case_expression .and.
case_expression lt higher_limit case(010)
36Example
- !classcode.f
- program classcode_prg
- integer ClassCode
- print , "Input Class Code"
- read , ClassCode
- select case (ClassCode)
- case (1)
- print , "Freshman"
- case (2)
- print , "Sophomore"
- case (3)
- print , "Junior"
- case (4)
- print , "Senior"
- case (5)
- print , "Graduate"
- case default
- print , "Illegal class code", ClassCode
- end select
37Code-1
- program quadratic_by_block_case
- real, parameter small1e-6
- real a,b,c,d,sqrt_d,x1,x2
- integer selector
- print ,"Please type a,b and c"
- read ,a,b,c
- db2-4.0ac
- selectord/small
- select case (selector)
- case (1)
- sqrt_dsqrt(d)
- x1(-bsqrt_d)/(aa)
- x2(-b-sqrt_d)/(aa)
- print ,"Two roots",x1,x2
- case (0)
- x1-b/(aa)
- print ,"one root",x1
- case(-1)
- print ,"no real roots"
38Code-2
- !days.f
- !Example Days of the month
- program D17
- implicit none
- integer Month, Year, NDays
- write (unit , fmt ) " Please enter the
year (4 digits). " - read (unit , fmt ) Year
- write (unit , fmt ) " Please enter the
month number (1 to 12). " - read (unit , fmt ) Month
- select case (Month)
- case (9, 4, 6, 11) ! Sep, Apr, Jun, Nov
- NDays 30
- case (2) ! February
- if (modulo( Year, 4 ) 0) then
- NDays 29 !Leap year
- else
- NDays 28
- end if
- case default ! Jan, Mar, May, Jul, Aug,
Oct, Dec
39Code-3
- !Semesters.f
- ! Example Fall and spring semesters, by months.
- program D19
- implicit none
- integer M
- write (unit , fmt ) "Please enter a month
number between 1 and 12" - read (unit , fmt ) M
- select case (M)
- case (9 12, 1)
- write (unit , fmt ) " Month ", M, "
is in the fall semester. " - case (2 6)
- write (unit , fmt ) " Month ", M, "
is in the spring semester. " - case default
- write (unit , fmt ) " School is not
in session during month ", M - end select
- end program D19
40- Prelude
- Logical Expressions Logical Variable
- IF, IF - ELSE IF constructs
- Comparing Numbers
- Comparing Character Strings
- Case construct
- Named constructs
41Named Constructs
- if, if-else if, case constructs may be named by
attaching a label at the beginning and end of the
construct - name if ( logical-expression) then...else
name...end if name - name select case ( selector)...case( case
selector)...end select name - For exampleupdate if (x gt largest)
thenlargestxpositionNend if update
42Example
- !nested-if.f
- !verison2 Named if constructs
- !condition x
- !if x is greater than equal to 1 and y is equal
to 2 then assign 3 to z. - !if x is greater than equal to 1 and y is equal
to 2 and - !condition z
- ! z is equal to 3 or n is equal to 5 then
assign 8 to m. - !if x is greater than equal to 1 and y is equal
to 2 and - ! z is equal to 3 or n is equal to 5
and - !condition k
- ! k is in between 1 and 10 (is also equal to 1
and 10) or m is equal to 5 - ! or m is less than 0 then
- ! assign 22 to p.
- program NestedIfNamed
- integer x,y,z,n,m,k,p
43Cont'
- print , "x y"
- read , x, y
- CndX if( x gt 1 .and. y 2 ) then
- z 3
- print , "z" , z
- print , "n"
- read , n
- CndZ if( z 3 .or. n 5 ) then
- m 8
- print , "m" , m
- print , "k"
- read , k
- CndK if( ( 1 lt k .and. k lt 10 ) .or. m 5
.or. m lt 0 ) then - p 22
- print , "p" , p
- end if CndK
- end if CndZ
- end if CndX
- end program NestedIfNamed
44Homework2
- Write a program to calculate the following
piecewise continuous function by using if
construct. - Due Date 27/02/2003