27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Eighth Edition
Advertisements

Objectives In this chapter, you will learn about:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
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.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Control flow Ruth Anderson UW CSE 160 Spring
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Control flow Ruth Anderson UW CSE 160 Winter
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CSE123 - Lecture 4 Structured Programming- Loops.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 – C Program Control
CHAPTER 5A Loop Structure
JavaScript: Control Statements.
Ruth Anderson UW CSE 160 Spring 2018
Outline Altering flow of control Boolean expressions
Structured Program Development in C
The University of Texas – Pan American
Iteration: Beyond the Basic PERFORM
Michael Ernst UW CSE 140 Winter 2013
2.6 The if/else Selection Structure
JavaScript: Control Statements II
Repetition (While Loop) LAB 9
Michael Ernst UW CSE 190p Summer 2012
REPETITION Why Repetition?
Chapter 4: Loops and Iteration
Presentation transcript:

27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples

27 March, 2000 Repetitive Execution Repetition controlled by a counter DO control_variable = initial_value, limit, step_size statements to be executed END DO Repetition controlled by logical expression DO statements to be executed IF (logical_expression) EXIT statements to be executed END DO

27 March, 2000 Repetition With Counter Control_variable is the variable to be used by the loop initial_value is the starting point limit is the end point step_size has a default of 1 if not specified; must be non-zero. Initial_value, limit, and step_size can be integer or integer expressions

27 March, 2000 DO Loop Example1 !This program will print out a !table of temperature conversions !for every 5 degrees INTEGER :: iCels, iFahr PRINT *, “CELS FAHR” DO iCels = 0, 100, 5 iFahr = (9 * iCels / 5) + 32 PRINT *, iCels, iFahr END DO Control_variable is iCels initial_value is 0 limit is 100 step_size is 5

27 March, 2000 DO with Counter-Controller : Specifics Calculate initial_value, limit,step_size Set control_variable = initial_value Check control_variable Statement_sequence Increment control_variable by step_size false true Check: control_variable <= limit if step_size positive control_variable >= limit if step_size negative body

27 March, 2000 Output of Example1 CELS FAHR

27 March, 2000 DO Loop Example2 !This program will print out a !table of temperature conversions !for every 5 degrees INTEGER :: iCels, iFahr PRINT *, “CELS FAHR” DO iCels = 100, 0, -5 iFahr = (9 * iCels / 5) + 32 PRINT *, iCels, iFahr END DO step_size < 0

27 March, 2000 Output of Example2 CELS FAHR

27 March, 2000 DO with Counter-Controller : Specifics 1 If check control is true the first time, the body is not executed DO number = 1,0,1 statements END DO initial_value=1, limit=0, step_size=1 first time around: control_variable number =1 check control_variable: control_variable >=limit so loop is not executed

27 March, 2000 DO with Counter-Controller : Specifics 2 The initial values of the control_variable, initial_value, limit, step_size are determined before the DO repetition begins and cannot be changed in the body. K=5 istep=1 DO I = 1,K,istep PRINT *, I, K, istep K=K-1 END DO Output:

27 March, 2000 DO with Counter-Controller : Specifics 3 One can use the control_variable in the body but can not modify the value K=5 istep=1 DO I = 1,K,istep PRINT *, I, K, istep K=K-1 I=I+1 END DO ERROR !

27 March, 2000 Calculating Depreciation Example PROGRAM Depreciation_Table ! ! Program to calculate and display a depreciation table ! using one of two methods of depreciation: ! (1) straight-line ! (2) sum-of-the-years'-digits ! Variables used are: ! Price : purchase price of item ! SalvageValue : and its salvage value ! Amount : amount to be depreciated !(Price - SalvageValue) ! UsefulLife : its useful life ! Method : method of depreciation (1 or 2) ! Depreciation : amount of depreciation ! Year : number of year in depreciation table ! Sum : UsefulLife ! (for sum-of-years'-digits method)

27 March, 2000 Details 1 !Sum : UsefulLife ! (for sum-of-years'-digits method) Sum = 0 DO Year = 1, UsefulLife Sum = Sum + Year END DO

27 March, 2000 Details 2 ! Generate the depreciation table PRINT * PRINT *, "Year Depreciation" PRINT *, "===================" DO Year = 1, UsefulLife IF (Method == 2) THEN Depreciation = (UsefulLife - Year + 1) & * Amount / REAL(Sum) END IF PRINT *, " ", Year, Depreciation END DO

27 March, 2000 Nested DO Loops The body of a DO loop may contain other DO loop(s). For each iteration of the outer loop, the inner loop control variable goes through its range of values DO M = 1, Last_M DO N = 1, Last_N Product = M * N PRINT *, M, " ", N, " ", Product END DO

27 March, 2000 For Last_M, Last_N equal 3 and 3, output is MNM * N ================

27 March, 2000 What would be the output of these nested loops ? DO M = 1, Last_M DO N = 1, M Product = M * N PRINT *, M, " ", N, " ", Product END DO

27 March, 2000 For Last_M equals 3, output is MNM * N ================

27 March, 2000 Paired Words PROGRAM... END IF THEN... END IF DO... END DO