CEE15 Introduction to Computing for Civil Engineers 3 - Control Structures

CEE15 Introduction to Computing for Civil Engineers 3 - Control Structures

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CEE15 Introduction to Computing for Civil Engineers 21 Chapter 3 Control Structures: Selective Execution 3.1. IF-THEN-ELSE-END IF Statement 3.1.1. IF-THEN-ELSE-END IF The most general form of the IF-THEN-ELSE-END IF statement is the following: IF (logical-expression) THEN statements-1 ELSE statements-2 END IF where statements-1 and statements-2 are sequences of executable statements, and logical-expression is a logical expression. The execution of this IF-THEN-ELSE-END IF statement goes as follows: the logical-expression is evaluated, yielding a logical value if the result is .TRUE. , the statements in statements-1 are executed if the result is .FALSE. , the statements in statements-2 are executed after finish executing statements in statements-1 or statements-2 , the statement following END IF is executed Examples: The following code first reads in an integer into INTEGER variable Number. Then, if Number can be divided evenly by 2 (i.e., Number is a multiple of 2), the WRITE(*,*) between IF and ELSE is executed and shows that the number is even; otherwise, the WRITE(*,*) between ELSE and END IF is executed and shows that the number is odd. Function MOD(x,y) computes the remainder of x divided by y. This is the remainder (or modulo) function INTEGER :: Number READ(*,*) Number IF (MOD(Number, 2) == 0) THEN WRITE(*,*) Number, ' is even' ELSE WRITE(*,*) Number, ' is odd' END IF The following program segment computes the absolute value of X and saves the result into variable Absolute_X. Recall that the absolute value of x is x if x is non-negative; otherwise, the absolute value is -x. For example, the absolute value of 5 is 5 and the absolute value of -4 is 4=-(-4). Also note that the WRITE(*,*) statement has been intentionally broken into two lines with the continuation line symbol &. REAL :: X, Absolute_X X = . .... IF (X >= 0.0) THEN Absolute_X = X ELSE Absolute_X = -X END IF
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CEE15 Introduction to Computing for Civil Engineers 22 WRITE(*,*) 'The absolute value of ', x, & & ' is ', Absolute_X The following program segment reads in two integer values into a and b and finds the smaller one into Smaller . Note that the WRITE(*,*) has also been broken into two lines. INTEGER :: a, b, Smaller READ(*,*) a, b IF (a <= b) THEN Smaller = a ELSE Smaller = b END IF Write(*,*) 'The smaller of ', a, ' and ', & & b, ' is ', Smaller 3.1.2. IF-THEN-END IF The IF-THEN-END IF form is a simplification of the general IF-THEN-ELSE-END IF form with the ELSE part omitted: IF (logical-expression) THEN statements END IF where statements is a sequence of executable statements, and logical-expression is a logical expression. The execution of this IF-THEN-END IF statement goes as follows: the logical-expression is evaluated, yielding a logical value if the result is .TRUE. , the statements in statements are executed, followed by the statement following the IF-THEN-END IF statement. if the result is
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

CEE15 Introduction to Computing for Civil Engineers 3 - Control Structures

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online