LAB-09 DO WHILE loop I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals
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)
ICS101-LAB09-Term073 3 Example INTEGER I PRINT*, (I, I = 1, 10, 2) END
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
ICS101-LAB09-Term073 5 Combining IF and GOTO NIF (condition) THEN BLOCK OF STATEMENTS GOTO N ENDIF
ICS101-LAB09-Term073 6 Example 1 SUM = 0 I = IF (I.LE. 99) THEN READ*, I SUM = SUM + I GOTO 100 ENDIF PRINT*, SUM END
ICS101-LAB09-Term073 7 Example 2 SUM = 0 I = 0 100IF (I.GT. 99) GOTO 110 READ*, I SUM = SUM + I GOTO PRINT*, SUM END
ICS101-LAB09-Term073 8 Using DO WHILE-END DO DO WHILE (condition) BLOCK OF STATEMENTS END DO
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
ICS101-LAB09-Term 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.
ICS101-LAB09-Term 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.