Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB-09 DO WHILE loop I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.

Similar presentations


Presentation on theme: "LAB-09 DO WHILE loop I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals."— Presentation transcript:

1 LAB-09 DO WHILE loop I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

2 ICS101-LAB09-Term073 2 Implicit DO…LOOP Printing or asking for a series of numbers READ*, (list of variables, index = initial, limit, increment) PRINT*, (list of variables, index = initial, limit, increment)

3 ICS101-LAB09-Term073 3 Example INTEGER I PRINT*, (I, I = 1, 10, 2) END

4 ICS101-LAB09-Term073 4 Introduction How is it to do a calculation until user gives certain input? With DO LOOP we can only do the loop in specific number of iterations. For above purpose, we need to use a logical condition Combining IF and GOTO (old versions) Using DO WHILE…END DO

5 ICS101-LAB09-Term073 5 Combining IF and GOTO NIF (condition) THEN BLOCK OF STATEMENTS GOTO N ENDIF

6 ICS101-LAB09-Term073 6 Example 1 SUM = 0 I = 0 100 IF (I.LE. 99) THEN READ*, I SUM = SUM + I GOTO 100 ENDIF PRINT*, SUM END

7 ICS101-LAB09-Term073 7 Example 2 SUM = 0 I = 0 100IF (I.GT. 99) GOTO 110 READ*, I SUM = SUM + I GOTO 100 110PRINT*, SUM END

8 ICS101-LAB09-Term073 8 Using DO WHILE-END DO DO WHILE (condition) BLOCK OF STATEMENTS END DO

9 ICS101-LAB09-Term073 9 Example of DO WHILE-END DO INTEGER I, SUM SUM = 0 I = 0 DO WHILE (I.LE. 99) READ*, I SUM = SUM + I END DO PRINT*, SUM END

10 ICS101-LAB09-Term073 10 Exercises 1. Write a program that reads 10 real numbers from user and at the end, prints their sum, product, and average. Use DO-WHILE loop in this program. 2. Improve the above program of Q1 in the following way: Instead of just reading 10 numbers always, now the program should ask in advance from the user “How many numbers you want to enter?” Then the program should read as many numbers as user want. At the end, the program should print the sum, product and average of the entered numbers.

11 ICS101-LAB09-Term073 11 Exercises 3. Write a program that reads some numbers from users until –1 is entered. For each input number, the program should display its square. 4. There are M people in a town whose population increases by P percent each year. Write a program that displays the annual population and determines how many years (Y) it will take for the population to surpass S people.


Download ppt "LAB-09 DO WHILE loop I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals."

Similar presentations


Ads by Google