CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.

Slides:



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

Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to execute a sequence of statements more than once. Type of.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
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.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
CHAPTER 4 REPETITIVE EXECUTION. Counter-Controlled DO Loops DO Number = 1, 9 PRINT *, Number, Number**2 END DO DO Number = 9, 1, -1 PRINT *, Number, Number**2.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
Chapter 4: Control Structures II
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
sequence of execution of high-level statements
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Fortran: Control Structures Session Three ICoCSIS.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
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.
CSE123 - Lecture 4 Structured Programming- Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 9 Repetition.
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
Chapter 3: Decisions and Loops
JavaScript: Control Statements I
JavaScript: Control Statements I
Chapter 5 Repetition.
Structured Program Development in C
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Chapter 8: More on the Repetition Structure
Alternate Version of STARTING OUT WITH C++ 4th Edition
Loop Construct.
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Repetition (While Loop) LAB 9
REPETITION Why Repetition?
Presentation transcript:

CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the body of the loop. There are two basic types of repetition: 1. Repetition controlled by a counter 2. Repetition controlled by a logical expression

Counter-Controlled DO Loop General Form: DO Index = Initial value, Limit, Increment Statement 1. Statement n END DO -Index is a variable that could be real or integer. -Initial value or limit or increment may be constant, variables, or expressions that could be also real or integer types. -Increment can be either positive or negative, but not zero. -The number of repetitions is calculated as the larger of the value 0 and the value of

Example: The following DO-loop DO k=5, 83, 4 would be executed as the following number of times: but for the following DO-loop DO K=10, 2, 4 will not be executed (zero repetition) since the substitution in the formula will give a negative value.

Example: Write a DO-loop to execute the following summation: Solution: Sum=0 DO N=1, 50 Sum = Sum + N END DO

Nested DO Loop 1.A nested DO Loop cannot use the same index as the Loop that contains it. DOI=1,5DOI=1,5 DOI=1,3  DOK=1,3.. END DO  END DOEND DO 2.DO Loops that are independent of each other may have the same index. 3.When one loop in nested with another, the inside loop is completely executed each pass through the outer one. 4. Each DO Loop should be end by separate END DO statement as shown above.

General DO Loops General DO loop  number of iterations not determined DO-Exit Construct General form: DO statement-sequence 1 IF (logical-expression) EXIT statement-sequence 2 END DO statement-sequence 3 ! executed for true condition

Example (1) : Write a Fortran program segment to calculate the following summation: 2. Using If statement: Sum = 0.0 Number = 1 10 IF (Number <=50) THEN Sum = Sum + Number Number = Number + 1 GO TO 10 END IF 1. Using DO-Exit construct: Sum = 0.0 Number = 1 DO Sum = Sum + Number Number = Number + 1 IF (Number >50) EXIT END DO

Example (2): Write a Fortran program to calculate and print Y, where: Y = 5 Xm – 3 Xm X-1 ForM=5,X=0.1, 0.2, 0.3,……………, 10 M=4, X=0.1, 0.2, 0.3,……………, 10 M=3,X=0.1, 0.2, 0.3,……………, 10 DO M=5,3,-1 DO I=1,100 X=I/10.0 Y=5.*X**m-3.*X**(m-1)+2.*x-1.0 Open(1,File=‘d:\example2.txt) Write(1,10) M, X, Y 10Format (5X, ‘M=’, I2,2X, ‘X=’, F7.3, 2X, ‘Y=’, F9.4) END DO END

Named DO Construct DO construct can be named in this way: Name: DO………………… END DO Name Example: Outer: Do M=1, last_M Inner: Do N=1, Last_N Product=M*N PRINT *, M,” “,N,” “, Product END DO Inner END DO Outer