Loop Construct.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Repeating Actions While and For Loops
Computer Science 1620 Loops.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Control Flow IF Statement Visualisation of the IF Statement IF... THEN... ELSE Construct Visualisation of the IF... THEN Construct Visualisation of the.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
UNIT II Decision Making And Branching Decision Making And Looping
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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.
Algorithm Design.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Fortran: Control Structures Session Three ICoCSIS.
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.
JavaScript, Sixth Edition
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Computer Programming -1-
Unsur-unsur Algoritma
ECE Application Programming
Lesson #5 Repetition and Loops.
Chapter 4 – C Program Control
Fundamentals of PL/SQL part 2 (Basics)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 4 – Loops Dr. Larry G. Thomas – University of Toledo / LCCC
Lesson #5 Repetition and Loops.
Chapter 5 - Control Structures: Part 2
Problem Solving and Control Statements: Part 2
Chapter 5: Control Structures II
Control Structures II (Repetition)
JavaScript: Control Statements.
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
Chapter 13 Control Structures
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Repetition and Loop Statements
Arrays, For loop While loop Do while loop
Looping and Repetition
- Additional C Statements
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
CS1100 Computational Engineering
Structured Program Development in C
Additional Control Structures
Introduction to Object-Oriented Programming with Java--Wu
Iteration: Beyond the Basic PERFORM
Types of Flow of Control
Repetition Control Structure
CMPT 102 Introduction to Scientific Computer Programming
Chapter 6: Repetition Statements
Lab5 PROGRAMMING 1 Loop chapter4.
Objectives You should be able to describe: The while Statement
Lesson #5 Repetition and Loops.
Chapter 4 - Program Control
Programming Fundamental
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Chapter 13 Control Structures
Presentation transcript:

Loop Construct

Control flow constructs Control Flow constructs seen so far sequential flow two-way or multi-way branching flow These constructs are not enough!

Sum of Square roots Problem: Compute sum of square roots of numbers from 1 to 5 One solution: program sum_sqroot implicit none real :: sum,sum1,sum2,sum3,sum4 sum1 = sqrt(1.0) sum2 = sum1 + sqrt(2.0) sum3 = sum2 + sqrt(3.0) sum4 = sum3 + sqrt(4.0) sum = sum4 + sqrt(5.0) print *, sum end program

Summing Square roots What are the problems with this solution? No need for so many sum variables; use instead sum = sum + sqrt(..) what if 10, 20 or 100 numbers to be added? code repetition possible but undesirable what if an unknown numbers (given as input) to be added? code repetition not possible at all

Looping Loop constructs are designed to solve the above problems Loop constructs enable execution of same piece of code a specified number of time Two kinds of loop constructs: Controlled loop Uncontrolled loop The above problem can be solved using controlled loop

Example Program sum_sqrt implicit none real :: x,sum sum = 0.0 !initialization needed. why? see the loop !looping construct do i = 1,5 !loop body executed five times x = real(i) sum = sum + sqrt(x) !each time i incremented by 1 end do print *, sum end program

Controlled Loop Construct General form: do var = init, fin, inc loop_body end do var, called control variable, must be integer init,fin,inc are integer expressions inc must be non-zero, can be negative and default value is 1 loop_body must not modify value of var

Iteration loop_body is executed repeatedly each execution is called an iteration initially, var = init after every iteration inc is added to var loop is terminated when value of var exceeds fin (or smaller than fin) Loop body is executed a fixed number of times number of iterations may depend on values of variables

Controlled Loop number of iterations (for +ve inc) iter-count = (fin-init+inc)/inc iter-count is computed when the do statement is first encountered if iter-count <= 0 , loop is not executed if iter-count > 0, loop_body is executed, iter-count is decremented by 1 and var is incremented in every iteration loop terminates finally when iter-count <= 0, i.e., the control flows to the statement immediately following the loop statement

Controlled Loop iter-count is computed when the do statement is first encountered Its value depends upon the values at that point values of init, fin, inc should not be changed inside the loop var is incremented by the value of inc in every iteration It is assigned the value init when do is first encountered

Cycle and Exit statements loop_body can contain two special statements Cycle It terminates the current iteration and the next iteration is started, if there is one Exit This terminates the loop moving the control to the statement following the loop

Example Read a line of characters, Count the number of e occurring in the line Replace every a by # Lines of length 80 or ended by $ sign

program e_count character(len=80) :: line integer :: counter,index read *, line index=1 counter = 0 do index = 1, 80 if (line(index:index) = = "$“) exit !exit the loop if end of line if (line(index:index) == "e“) then counter = counter +1 cycle !skip the rest of the loop endif if (line(index:index) == "a“) then line(index:index) = "#" endif end do print *, counter, line

Uncontrolled Loop The controlled loop is a simple loop computation The loop body executed for a number of times that is fixed a priori or at least prior to the execution of the do statement Many times repeat a computation till certain condition holds No upper bound on the number of iterations exists More general loops are required for describing these Uncontrolled loops are meant for this

Another Example Similar to the earlier problem there is no upper bound on the length of the line all lines should be terminated by $ index =0 do !uncontrolled loop index = index +1 if (line(index:index) == "$“) exit if (line(index:index) == "e" ) then counter = counter +1 cycle endif if (line(index:index) == "a" ) then line(index:index) = "#" endif end do

Uncontrolled loop Controlled loop does many things implicitly control variable updation, exit condition checking In uncontrolled version, these have to be done explicitly Termination is guaranteed in the controlled one Termination need to be ensured in the uncontrolled one explicit inclusion of exit statements is required In e_count1, for termination of the loop, the end of line condition is required In e_count, the loop terminates automatically after reading at most 80 characters Controlled loop is safe but less general

General Form do loop_body end do Any number of exit statements can occur in the loop_body At least one required Possibility of non termination, in any case

Loops Loop is an important construct Most programs will involve loops Loops encode potentially unbounded computations Mastering loops is important Loops cause non termination, a most common programming error Correct programs should be terminating Every loop is associated with a condition called loop invariants Loop invariant holds at the beginning of every iteration

Nested Loops Like many other constructs loops can also be nested do statement1 do statement2 end do statement3 end do Note that `inner do' ends before the `outer do‘ Exit and cycle in statement2 refers to the inner loop Exit and cycle in statement1 and statement3 refer to the outer loop

Labeling loops For ease of understanding loops can be labeled: outer: do statement1 inner: do statement2 end do inner statement3 end do outer exit and cycle statements can name the loop exit outer, cycle outer unnamed statements refer to the innermost enclosing loops

Labeling control constructs Like loops all other constructs can be labeled Examples: cond1: if (test1) then statement elseif (test2) then cond1 statement else cond1 statement endif cond1 Similarly for select case construct