Date: Mon, 11 Nov 1996 18:02:07 GMT
Server: NCSA/1.5
Content-type: text/html
Last-modified: Thu, 04 Apr 1996 23:45:25 GMT
Content-length: 9624
CS 110 Section 2 Lecture Notes - Week 3
Lecture Notes - Week 3
- Topic:
- Conditional execution and logical expressions. IF/THEN/ELSE/END IF statement.
- Text:
- Chp. 3.2, 3.4, 3.5, 3.8, 3.9
- Notes:
-
Conditional Execution
- So far, all statements are unconditionally executed, starting with the first and proceeding sequentially to the last.
- What if we don't want every statement to be executed every time we run the program?
- Want to conditionally execute some statements, depending on a condition which may change every time the program is run.
IF/THEN/END IF Statement
- Use the IF/THEN/END IF statement to conditionally execute one or more statements (Note: END IF is two words).
unconditional statements
IF (condition) THEN
conditional statements
END IF
- When the condition is true the conditional statements are executed; when it is false they are skipped.
IF/THEN/ELSE/END IF Statement
- Optional ELSE specifies another set of statements to be executed when the condition is false.
IF (condition) THEN
true conditional statements
ELSE
false conditional statements
END IF
IF (NUM . GE. 0) THEN
PRINT *, 'Positive'
ELSE
PRINT *, 'Negative'
END IF
- Indent conditional statements three spaces (i.e. column 10).
- IF, ELSE and END IF are on separate lines.
Logical IF Statement
- If only have one conditional statement and no ELSE part then can put everything on a single line.
IF (DAY .EQ. 1) PRINT *, 'Monday'
- Note: there is no THEN or END IF.
Nested IF Statements
- Can nest IF statements within the conditional sections of other IF statements.
IF (YEAR .LE. 4) THEN
STATUS = 'Undergraduate'
Fee = 4547.75
ELSE
STATUS = 'Graduate'
IF (YEAR . LE. 8) THEN
FEE = 5852.25
ELSE
FEE = 720.75
END IF
END IF
- The second IF statement is itself conditionally executed depending on the condition of the first IF statement.
- Indent each level another 3 spaces (i.e. 3, 6, 9, ... spaces). Use indentation to line up statements so that they are easy to read and understand.
General IF Statement
- Use the general IF statement when testing multiple conditions, each with their own set of statements to execute.
IF (YEAR .EQ. 1) THEN
PRINT *, 'Freshman'
ELSE IF (YEAR .EQ. 2) THEN
PRINT *, 'Sophomore'
ELSE IF (YEAR .EQ. 3) THEN
PRINT *, 'Junior'
ELSE IF (YEAR .EQ. 4) THEN
PRINT *, 'Senior'
END IF
- The conditions are checked sequentially until one is found that is true. The rest are skipped even if subsequent conditions are also true!
- An optional ELSE clause is executed when none of the conditions listed are true.
IF (LIGHT .EQ. 'R') THEN
PRINT *, 'Stop'
ELSE IF (LIGHT .EQ. 'O') THEN
PRINT *, 'Prepare to stop'
ELSE
PRINT *, 'Proceed'
END IF
- Use the general IF statement instead of multiple nested IF statements.
Logical Expressions
- The condition tested in IF statements is a logical expression surrounded by parentheses.
IF (logical-expression-1) THEN
...
ELSE IF (logical-expression-2) THEN
...
END IF
- Logical expressions are similar to arithmetic expressions except the result has only two possible values: .TRUE. and .FALSE. (Note the dots before and after).
LOGICAL Variables
- A variable can store .TRUE. and .FALSE. using the LOGICAL variable type.
LOGICAL RENEW
- To assign a LOGICAL value to a LOGICAL variable use the assignment statement, just like any other variable.
logical-variable = logical-expression
RENEW = .FALSE.
Relational Operators
- Relational operators compare two numbers together to produce .TRUE. and .FALSE. These can be used to create complex logical expressions.
.LT. - less than?
.LE. - less than or equal to?
.GT. - greater than?
.GE. - greater than or equal to?
.EQ. - equal?
.NE. - not equal?
DAY = 52
RENEW = DAY .GT. 14 (= .TRUE.)
- Relational operator names also start and end with a dot.
- Operands may be literals, variables or expressions of any type (except LOGICALs). e.g. INTEGERs, REALs or CHARACTER strings.
RENEW = EXP(X) .LT. (Y * 43.7 + Z)
- Both operands must be of comparable types; i.e. both numbers or both CHARACTER strings. You can compare "apples to oranges"!
Logical Operators
Operator Precedence
- VERY IMPORTANT - You must remember the precedence order of the logical and relational operators. This varies between different programming languages!
- First: arithmetic operators
- Second: relational operators (all have the same precedence)
- Third: .NOT.
- Fourth: .AND.
- Fifth: .OR.
- Last: .EQV. and .NEQV.
- Note the order of .AND. and .OR. - it is very easy to get it wrong, with disasterous results!
- Use parentheses to over-ride the default precedence if necessary.
- Example: if x and y are both greater than min then ...
- Right:
IF (X .GT. MIN .AND. Y .GT. MIN) THEN
- Wrong:
IF (X .AND. Y .GT. MIN) THEN
- When in doubt use parentheses.
Comparing CHARACTER Strings
- CHARACTER strings can also be compared using the relational operators.
IF (STATUS .EQ. 'Graduate') THEN
- If the strings are the same length then they are compared character by character. Both strings must have exactly the same characters.
- If one string is shorter it is automatically padded with blanks before comparing.
- Normally only use .EQ. and .NE. when comparing CHARACTER strings; using other relational operators can lead to unpredictable results.
- Right:
ANSWER = 'No'
...
IF (ANSWER .EQ. 'Yes') THEN
- Wrong:
ANSWER = 'No'
...
IF (ANSWER .LT. 'Yes') THEN
- Comparisons are case sensitive - upper and lowercase characters are very different!
CHARACTER *3 ANSWER
ANSWER = 'Yes'
...
IF (ANSWER .EQ. 'Yes') THEN (.TRUE.)
...
IF (ANSWER .EQ. 'YES') THEN (.FALSE.)
...