Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition (While Loop) LAB 9

Similar presentations


Presentation on theme: "Repetition (While Loop) LAB 9"— Presentation transcript:

1 Repetition (While Loop) LAB 9
ICS 101 Lab Hossain Arif ICS Dept

2 Repetition DO Loops Last LAB WHILE Loops This LAB

3 WHILE Loop Informal representation
WHILE condition EXECUTE THE following BLOCK OF STATEMENTS FORTRAN does not have explicit WHILE statement

4 WHILE Loop WHILE Loop can be written as follows: n IF (condition) THEN
Block of statements GOTO n ENDIF

5 Example Consider this program – calculates average of 6 numbers
REAL X1, X2, X3, X4, X5, X6 REAL SUM, AVG READ*, X1 READ*, X2 READ*, X3 READ*, X4 READ*, X5 READ*, X6 SUM = X1 + X2 + X3 + X4 + X5 + X6 AVG = SUM / 6.0 PRINT*, AVG END

6 Example (Cont) Using WHILE loop REAL X REAL SUM, AVG INTEGER K
15 IF ( K .LT. 6 ) THEN READ*, X SUM = SUM + X K = K + 1 GOTO 15 ENDIF AVG = SUM / 6.0 PRINT*, AVG END

7 Example(2) REAL WEIGHT READ*, WEIGHT 11 IF ( WEIGHT .NE. -1.0 ) THEN
IF ( WEIGHT .LT. 0 .OR. WEIGHT .GE. 400 ) THEN PRINT*, ‘Weight is OUT of range’ ELSEIF ( WEIGHT .LT. 65 ) THEN PRINT*, ‘LIGHT-WEIGHT’ ELSEIF ( WEIGHT .LT. 85 ) THEN PRINT*, ‘MIDDLE-WEIGHT’ ELSE PRINT*, ‘HEAVY-WEIGHT’ ENDIF GOTO 11 END

8 Nested WHILE Loops WHILE Loops can be nested
Inner loop must start after outer loop and must finish before it You can have as many nested loops as needed

9 Implied Loops Used in READ and PRINT statements
Implied Loop can be written as follows: READ*, (list of variables, index = initial, limit, increment) PRINT*, (list of expressions, index = initial, limit, increment) The same DO loops rules are applied

10 Implied Loop Example What will printed by this line:
PRINT*, (K, K = 1, 10, 1) Output:

11 Exercise(1) Using While loop, write a FORTRAN program that calculates the summation of all odd numbers less than or equal to NUM … + NUM Ask the user for the value of NUM.


Download ppt "Repetition (While Loop) LAB 9"

Similar presentations


Ads by Google