Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.

Slides:



Advertisements
Similar presentations
Chapter 04 (Part III) Control Statements: Part I.
Advertisements

UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
CS0004: Introduction to Programming Repetition – Do Loops.
 Control structures  Algorithm & flowchart  If statements  While statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
sequence of execution of high-level statements
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Control Structures sequence of execution of high-level statements.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Application development with Java Lecture 6 Rina Zviel-Girshin.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Fortran: Control Structures Session Three ICoCSIS.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Program design Program Design Process has 2 phases:
Chapter 4 – C Program Control
JavaScript: Control Statements I
Chapter 4 - Program Control
JavaScript: Control Statements.
- Additional C Statements
Outline Altering flow of control Boolean expressions
CS1100 Computational Engineering
MSIS 655 Advanced Business Applications Programming
Chapter 4 - Program Control
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
Computing Fundamentals
Computer Science Core Concepts
2.6 The if/else Selection Structure
Chapter 4 - Program Control
Repetition (While Loop) LAB 9
Chap 7. Advanced Control Statements in Java
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Control Structures.
Presentation transcript:

Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8 SUM = X1+ X2 + X3 + X4 + X5 + X6 + X7 + X8 AVG = SUM / 8.0 PRINT*, ' THE AVERAGE = ', AVG END Read 100 real numbers and compute their average Read 1000 real numbers and compute their average REPETITION

What about if we can do the following: REAL X, SUM SUM = 0 repeat the following two statements 100 times READ*, X SUM = SUM + X REAL X, SUM SUM = 0 repeat the following two statements 1000 times READ*, X SUM = SUM + X READ*, X SUM = SUM + X REAL X, SUM SUM = 0 repeat the following two statements N times INTEGER N REAL X, SUM SUM = 0 N = 100 DO 5 K = 1, N, 1 READ*, X SUM = SUM + X 5 CONTINUE

The DO LOOP DO N index = initial, limit, increment block of FORTRAN statements N CONTINUE How Many Times? The number of times the loop is executed is known before the loop execution begins. The number of times (iterations) the loop is executed is computed as follows: [(limit - initial) / increment] + 1

Examples on The DO Loop: Write a FORTRAN program that reads the grades of 100 students and calculates and prints their average. REAL GRADE, SUM, AVG INTEGER K SUM = 0.0 DO 10 K = 1, 100, 1 READ*, GRADE SUM = SUM + GRADE 10 CONTINUE AVG = SUM / PRINT*, ' THE AVERAGE = ', AVG END

Increment Default increment is 1 The increment can be negative It could be called then a decrement What will be printed by the following do loop? DO 99 K = 15, 4, -2 PRINT*, K 99 CONTINUE END

Examples on The DO Loop: Write a FORTRAN program that evaluates the following series to the 7th term. INTEGER SUM, K SUM = 0 DO 11 K = 1, 7 SUM = SUM + 3 **K 11 CONTINUE PRINT*, 'SUM = ', SUM END

THE CONTINUE STATEMENT REAL GRADE, SUM, AVG SUM = 0.0 DO 3 I = 1, 100, 1 READ *, GRADE SUM = SUM + GRADE 3 CONTINUE AVG = SUM /100.0 PRINT*, ' THE AVERGE = ', AVG END IF, GOTO, RETURN, STOP or another DO statement can not replace CONTINUE statements REAL GRADE, SUM, AVG SUM = 0.0 DO 3 I = 1, 100, 1 READ * GRADE 3 SUM = SUM + GRADE AVG = SUM /100.0 PRINT*, ' THE AVERGE = ', AVG END

Notes on the DO loop: In the first iteration, the index of the loop has the value of initial. Once the last statement “CONTINUE“ is executed, execution is transferred to the beginning of the loop. Before each iteration, the index is checked to see if it has passed the limit. If the index passed the limit, the loop iterations stop. Otherwise, the next iteration begins. DO 15 K = 1, 5, 2 PRINT*, K 15 CONTINUE The loop above is executed 3 times. The value of K outside the loop is 7 If the increment is positive the initial must be less than or equal to the limit. Otherwise the loop will not be executed. If the increment is negative the initial must be greater than or equal to the limit. Otherwise the loop will not be executed. If the values of the initial and the limit are equal, the loop executes only once.

DO loops rules Index of DO loop must be a variable of either INTEGER or REAL types. Initial, limit, and increment can be expressions of either INTEGER or REAL types. The value of the DO loop index cannot be modified inside the loop. The increment must not be zero, otherwise an error occurs. INTEGER M DO 124 M = 1, 10, 0.5 PRINT*, M 124 CONTINUE PRINT*, M END The index after the loop is the value that has been incremented and found to pass the limit. Branch into a DO loop is not allowed. Branch out of a DO loop before all the iterations are completed must not be used unless necessary.

DO loops rules (cont): The parameters ( initial, limit, and increment ) of the loop are evaluated before the loop execution begins.Once evaluated, changing their values will not affect the executing of the loop. REAL X, Y Y = 4.0 DO 10 X = 0.0, Y, 1.5 PRINT*, X Y = Y PRINT*, Y 10 CONTINUE This loop is executed [( ) /1.5)] + 1 = 3 times The output For an example, consider the following segment

Nested DO Loops Example: Nested DO Loops INTEGER M, J DO 111 M = 1, 2 DO 122 J = 1, 6, 2 PRINT*, M, J 122 CONTINUE 111 CONTINUE END The output of the above program is:

Nested DO Loops Example: Consider the following program. The output of the above program is: INTEGER M, J DO 111 M = 1, 2 DO 122 J = 1, 6, 2 PRINT*, M, J 122 CONTINUE PRINT*, M, J 111 CONTINUE PRINT*, M, J END INTEGER M, J DO 111 M = 1, 2 DO 122 J = 1, 6, PRINT*, M, J 111 PRINT*, M, J PRINT*, M, J END

Exercises DO 1 K = 2, 3 DO 2 M = 1, 4, 2 2 PRINT*, K, M 1 PRINT*, K, M PRINT*, K, M END The output ??? DO 1 K = 2, 3 DO 2 M = 1, 4, -2 2 PRINT*, K, M 1 PRINT*, K, M PRINT*, K, M END DO 1 K = 2, 3, -1 DO 2 M = 1, 4, 2 2 PRINT*, K, M 1 PRINT*, K, M PRINT*, K, M END

What is the output of the following program? INTEGER K, M, N N = 0 DO 10 K = -5, 5 N = N + 2 DO 20 M = 3, 1 N = N CONTINUE N = N CONTINUE PRINT*, N END The output 33

What is the output of the following program? INTEGER K, M, N N = 0 DO 10 K = 20, 2, -3 N = N + 5 DO 20 M = 13, 5, 4 20 N = N N = N + 3 PRINT*, N, K, M END The output

The WHILE LOOP DO WHILE ( condition ) Block of statements END DO

Examples on The WHILE LOOP: Example 1: Write a FORTRAN program that reads the grades of 100 students in a course. The program then computes and prints the average of the grades. Solution: REAL GRADE, SUM, AVG INTEGER NUM NUM = 0 SUM = 0.0 DO WHILE (NUM.LT. 100) NUM = NUM + 1 PRINT*, 'ENTER GRADE NUMBER', NUM READ*, GRADE SUM = SUM + GRADE END DO AVG = SUM / NUM PRINT*, ' THE AVERAGE OF THE GRADES = ', AVG END

Example 2: A class has a certain number of students Read the grades of the students, the last input would include a negative grade Compute and print the average and the number of students in the class. Solution: REAL GRADE, SUM, AVG INTEGER NUM NUM = 0 SUM = 0.0 PRINT*, 'ENTER A GRADE' READ*, GRADE DO WHILE (GRADE.GE. 0) NUM = NUM + 1 SUM = SUM + GRADE PRINT*, 'ENTER A GRADE' READ*, GRADE END DO AVG = SUM / NUM PRINT*, ' THE AVERAGE OF THE GRADES = ', AVG PRINT*, 'NUMBER OF STUDENTS IN THE CLASS = ', NUM END

Example 3:Series Summation using a DO loop: Question: Write a FORTRAN program which calculates the sum of the following series: Solution: REAL N, SUM SUM = 0.0 DO 100 N = 1, 99 SUM = SUM + N / (N + 1) 100CONTINUE PRINT*, 'SUM = ', SUM END

Example 4: Series Summation using a WHILE loop: Question: Write a FORTRAN program which calculates the sum of the following series: Solution: REAL N, SUM N = 1 SUM = 0.0 DO WHILE(N.LE. 99) SUM = SUM + N / (N + 1) N = N + 1 END DO PRINT*, 'SUM = ', SUM END

Example 5: Alternating Sequences/ Series: Alternating sequences, or series, are those which have terms alternating their signs from positive to negative. In this example, we find the sum of an alternating series. Question: Write a FORTRAN program that evaluates the following series to the 100th term Solution: It is obvious that the terms differ by 2 and start at the value of 1. INTEGER SUM, TERM, N SUM = 0 TERM = 1 DO 10 N = 1, 100 SUM = SUM + (-1) ** (N + 1) * TERM TERM = TERM CONTINUE PRINT*, 'SUM = ', SUM END

Example 6: Write a FORTRAN program that reads an integer number M. The program then computes and prints the factorial of M using a DO loop. Solution: INTEGER M,TERM, FACT PRINT*, 'ENTER AN INTEGER NUMBER' READ*, M PRINT*, 'INPUT: ', M IF (M.GE. 0) THEN FACT = 1 DO 100 TERM = M, 2, -1 FACT = FACT *TERM 100 CONTINUE PRINT*, 'FACTORIAL OF', M, 'IS', FACT ELSE PRINT*, 'NO FACTORIAL FOR NEGATIVES' ENDIF END

Example 7: Write a FORTRAN program that reads an integer number M. The program then computes and prints the factorial of M using a WHILE loop. Solution: INTEGER M, FACT PRINT*, 'ENTER AN INTEGER NUMBER' READ*, M PRINT*, 'INPUT: ', M IF (M.GE. 0) THEN FACT = 1 DO WHILE (M.GE. 2) FACT = FACT *M M = M - 1 END DO PRINT*, 'FACTORIAL IS ', FACT ELSE PRINT*, 'NO FACTORIAL FOR NEGATIVES' ENDIF END

Nested WHILE Loops Example: Consider the following program. INTEGER M, J M = 1 DO WHILE ( M.LE. 2) J = 1 DO WHILE (J.LE. 6) PRINT*, M, J J = J + 2 END DO M = M + 1 END DO END The output of the above program is: INTEGER M, J DO 111 M = 1, 2 DO 122 J = 1, 6, 2 PRINT*, M, J 122 CONTINUE 111 CONTINUE END

INTEGER X X = 5 DO WHILE ( X.GT. 0) PRINT*, X X = X + 1 END DO END Infinite loop

Implied Loops Implied loops are only used in READ and PRINT statements. READ*, (list of variables, index = initial, limit, increment) PRINT*, (list of expressions, index = initial, limit, increment) Example 1: Printing values from 100 to 87 The following segment prints the integer values from 100 down to 87 in a single line. PRINT*, (K, K = 100, 87, -1) Output:

Nested Implied Loops Example: Consider the following program INTEGER M, J DO 111 M = 1, 2 DO 122 J = 1, 6, 2 PRINT*, M, J 122 CONTINUE 111 CONTINUE END PRINT*, ((M, J, J = 1, 6, 2), M = 1, 2)

Nested Implied Loops Example: Consider the following program. INTEGER M, J DO 111 M = 1, 2 DO 122 J = 1, 6, 2 PRINT*, M, J 122 CONTINUE PRINT*, M, J 111 CONTINUE PRINT*, M, J END PRINT*, ((M, J, J = 1, 6, 2), M, J, M = 1, 2), M, J

Exercises PRINT*, ((K, M, M = 1, 4, 2), K, M, K = 2, 3), K, M PRINT*, ((K, M, M = 1, 4, -2), K, M, K = 2, 3), K, M PRINT*, ((K, M, M = 1, 4, 2), K, M, K = 2, 3, -1), K, M 2 ???

Repetition Constructs in Subprograms Exercise What will be printed by the following program? C FUNCTION SUBPROGRAM LOGICAL FUNCTION PRIME(K) INTEGER N, K PRIME =. TRUE. DO 10 N = 2, K / 2 IF (MOD(K, N).EQ. 0) THEN PRIME =. FALSE. RETURN ENDIF 10 CONTINUE RETURN END C MAIN PROGRAM LOGICAL PRIME PRINT*, PRIME (5), PRIME (8) END The output T F