ICS 101 Introduction to Computer Programming LAB 8 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

ICS 101 Introduction to Computer Programming LAB 8

Description:

ICS 101 Introduction to. Computer Programming. LAB 8. Karim Asif Sattar. karim_at_ccse.kfupm.edu.sa ... Information & Computer Science Department King Fahd ... – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 13
Provided by: ics78
Category:

less

Transcript and Presenter's Notes

Title: ICS 101 Introduction to Computer Programming LAB 8


1
ICS 101 Introduction toComputer ProgrammingLAB
8
  • Karim Asif Sattar
  • karim_at_ccse.kfupm.edu.sa
  • Information Computer Science Department King
    Fahd University of Petroleum Minerals

2
Lab 8
  • Reminder Quiz 4 will be next week
  • Repetition
  • Do Loop
  • While Loop
  • Do Loop
  • Example
  • Exercises

3
Repetition
  • Why is it needed?
  • Repetition is done in FORTRAN using
  • DO construct
  • WHILE construct
  • Single execution of the loop iteration
  • The loop must terminate after finite number of
    iterations
  • Each loop has termination condition

4
Repetition
  • Number of iterations is determined before loop
    begins execution
  • The termination condition checks the number of
    iterations
  • WHILE loop has different termination condition
  • We will see that later

5
DO Loop
  • General form
  • DO N index initial, limit, increment
  • BLOCK OF STATEMENTS
  • N CONTINUE
  • Increment field is optional, default value will
    imply

6
Do Loop (Contd)
  • Example
  • INTEGER K
  • DO 15 K 1, 5, 2
  • PRINT, K
  • 15 CONTINUE
  • END
  • This will print 1, 3 and 5

7
Example
  • Consider this program
  • 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

8
Example (Cont)
  • Using DO loop
  • REAL X
  • REAL SUM, AVG
  • SUM 0
  • DO 12 K 1, 6, 1
  • READ, X
  • SUM SUM X
  • 12 CONTINUE
  • AVG SUM / 6.0
  • PRINT, AVG
  • END

9
DO Loop Rules
  • Index must be of REAL or INTEGER type
  • Increment value can be positive or negative but
    not zero
  • It is possible to have DO loop without CONTINUE
    statement
  • Default value of increment is 1 or 1.0
  • DO loop can be executed once, many times, or zero
    time
  • Loop parameters are evaluated before loop
    execution begins

10
Nested DO Loops
  • DO 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

11
Exercises(1)
  • Write an integer FORTRAN function FACT that
    calculates the factorial of integer number
  • FACT(0) 1
  • FACT(1) 1
  • FACT(n) n FACT(n-1)
  • Use that function in FORTRAN program to calculate
    the factorial of number given by the user

12
Exercise(2)
  • Write a FORTRAN function SUMODD that calculates
    the summation of all odd numbers less than or
    equal N
  • Use that function in FORTRAN program. Ask the
    user for the value of N
Write a Comment
User Comments (0)
About PowerShow.com