Chapter 7 Other Loop forms, Procedures; Exception Handling.

Slides:



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

CS0004: Introduction to Programming Repetition – Do Loops.
Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There.
Objectives In this chapter, you will learn about:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Loops. Outline of control structures zSequential zDecision (selection) yA. Logical IF yB. Block IF x1. Single alternative IF x2. Double alternative IF.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 6 - Visual Basic Schneider
©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 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
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.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
CPSC- 120 Principle of Computer Science I Computer = Hardware + Software.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Chapter 4: Control Structures II
Chap 7- Control Structures : The WHILE Statement.
Control Structures Repetition or Iteration or Looping Part II.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
System Structures b Package b Abstract Data Type.
6.2 For…Next Loops General Form of a For…Next Loop
Chap 5 Control Structure Boolean Expression and the IF Statement.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Chapter 3 The General Structure of Ada Programs. General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - -
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
Control Structures Repetition or Iteration or Looping Part II.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
More Loops.
copyright © Michael B. Feldman, All Rights Reserved
Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
C:\> sort_3_numbers.exe |
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Chapter 7 Other Loop forms, Procedures; Exception Handling

The General Loop and Exit Problems with FOR statement, when n The loop does not step through all the values of a discrete type in forward or reverse order n The Loop control variable is not discrete n The number of iterations depends on conditions during the execution of the loop

Example - Looping when the increment is NOT 1 WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PROCEDURE Odd_Numbers IS OddNumber : Integer; BEGIN -- Odd_Numbers OddNumber := 1; -- initialize loop LOOP EXIT WHEN OddNumber > 39; -- test for exit Ada.Integer_Text_IO.Put(Item => Oddnumber, Width => 3); OddNumber := Oddnumber + 2; -- update END LOOP; Ada.Text_IO.New_Line; END Odd_Numbers;

Example- Looping when increment is not an integer WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Temperature_Table IS | Displays a table of Fahrenheit and --| equivalent Celsius temperatures CStart : CONSTANT Float := 100.0; -- initial Celsius temp CStep : CONSTANT Float := -10.0; -- change in Celsius temp CLimit : CONSTANT Float := -20.0; -- final Celsius temp Celsius : Float; -- Celsius temp Fahrenheit : Float; -- Fahrenheit temp

Cont. Example BEGIN -- Temperature_Table Ada.Text_IO.Put(Item => "Celsius Fahrenheit"); Ada.Text_IO.New_Line (Spacing => 2); Celsius := CStart; -- initialize LOOP EXIT WHEN Celsius < CLimit; -- test for exit Fahrenheit := 1.8 * Celsius ; Ada.Float_Text_IO.Put (Item => Celsius, Fore => 4, Aft => 0, Exp => 0); Ada.Text_IO.Put(Item => " "); Ada.Float_Text_IO.Put (Item => Fahrenheit, Fore => 3, Aft => 1, Exp => 0); Ada.Text_IO.New_Line; Celsius := Celsius + CStep; -- update END LOOP; END Temperature_Table;

LOOP Statement LOOP statement sequence; EXIT WHEN condition; statement sequence; END LOOP;

Example Looping Controlled by an Event WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Worm_and_Apple IS | Displays distances between a worm and an apple. The worm --| keeps reducing the distance by its body length until it is --| close enough to bite the apple SUBTYPE NonNegFloat IS Float RANGE Float'Last; WormLength: CONSTANT NonNegFloat := 8.5; -- worm body length in CM InitialDist: NonNegFloat; -- input - starting distance -- of worm from apple Distance: NonNegFloat; -- output - diminishing distance -- between worm and apple

Cont. Example BEGIN -- Worm_and_Apple Ada.Text_IO.Put (Item => "Initial distance (CM) away from apple > "); Ada.Float_Text_IO.Get(Item => InitialDist); Ada.Text_IO.New_Line; -- Cut the distance between the worm and the apple by -- the worm's body length until the worm is close enough -- to bite the apple

Cont. Example Distance := InitialDist; -- initialize LOOP EXIT WHEN Distance <= WormLength; -- test for exit Ada.Text_IO.Put(Item => "The distance is "); Ada.Float_Text_IO.Put (Item => Distance, Fore => 4, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Distance := Distance - WormLength; -- update END LOOP;

Cont. Example -- Display final distance before entering the apple. Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "Final distance between worm and apple is "); Ada.Float_Text_IO.Put(Item => Distance, Fore => 4, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "The worm bites the apple."); Ada.Text_IO.New_Line; END Worm_and_Apple;

FOR I IN 1..5 LOOP Square := i * i; Ada.Integer_Text_IO.Put (Item => i, Width => 1); Ada.Integer_Text_IO.Put (Item => Square, Width => 1); Ada.Text_IO.New_Line; END LOOP; i:=1; LOOP EXIT WHEN i > 5; Square := i * i; Ada.Integer_Text_IO.Put (Item => i, Width => 1); Ada.Integer_Text_IO.Put (Item => Square, Width => 1); Ada.Text_IO.New_Line; i:=i +1; END LOOP;

In General LOOP n i is needed to declare as a normal variable n i := 1;---- initialization required n i := i +1;---- increment/decrement required n i > condition to exit required In FOR LOOP n implicit in the FOR LOOP

LOOP DESIGN n Declaration for control variable n (Initialization) for control variable n (Increment/decrement) for control variable n Condition for exit

May not need a control variable In Some Problems, for example, n Add numbers n Multiply numbers

Flag-Controlled Loop n Initialize Sun to 0 n Initialize MoreData to “Y” n LOOP n EXIT WHEN MoreData = ‘N’ Read the next number to num Add num to Sum Read the next value of MoreData(Y or N) n END LOOP

Example- Flag-Controlled Loop Sum := 0; MoreData := ‘Y’; LOOP EXIT WHEN MoreData = ‘N”; Ada.Text_IO.Put(Item => “ Enter the next number ” ); Ada.Integer_Text_IO.Get (Item => num); Sum := Sum + num; Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => “Any more data Y (Yes) or N (No) ” ); Ada.Text_IO.Get( Item => MoreData); END LOOP;

Sentinel-Control Loops n Initialize Sum to 0 n Read the first number into num n Loop Exit WHEN num is the sentinel Add num to Sum Read the next number into num n END LOOP

Example - Sentinel-Control Loops Sum := 0; Ada.Text_IO.Put(Item => “ When done, enter -1 to stop ” ); Ada.Text_IO.Put(Item => “ Enter the first number ” ); Ada.Integer_Text_IO.Get (Item => num); Ada.Text_IO.New_Line; LOOP EXIT WHEN Num = Sentinel; Sum := Sum + num; Ada.Text_IO.Put(Item => “ Enter the next number ” ); Ada.Integer_Text_IO.Get (Item => num); Ada.Text_IO.New_Line; END LOOP;

Sentinel-Controlled Loop with a Priming Read n Read the first value of input variable n LOOP n EXIT WHEN input variable is equal to sentinel n ---- n Read the next value of input variable n END LOOP

Example ---To add positive numbers ---if negative number, exit loop Sum :=0; LOOP Ada.Integer_Text_IO.Get (Item => num); EXIT WHEN num <0; Sum := Sum + num; END LOOP; n

Example ---To multiply positive numbers ---if negative number, exit loop Product :=1; LOOP Ada.Integer_Text_IO.Get (Item => num); EXIT WHEN num <0; Product := Product * num; END LOOP

Example ---To add positive numbers ---if product is more than 1000, exit loop Product :=1; LOOP Ada.Integer_Text_IO.Get (Item => num); EXIT WHEN product > 1000; Product := Product * num; END LOOP