Chap 7- Control Structures : The WHILE Statement.

Slides:



Advertisements
Similar presentations
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
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.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
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 6 Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Program Design and Development
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
ITEC 320 Lecture 5 Scope Exceptions. Scope / Exceptions Review Functions Procedures.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Other Loop forms, Procedures; Exception Handling.
Visual Basic Programming
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chap 5 Control Structure Boolean Expression and the IF Statement.
Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Counting Loops.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Chapter 3 The General Structure of Ada Programs. General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - -
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Review Expressions and operators Iteration – while-loop – for-loop.
Control Structures 1 The if, for, and while Statements §5.1, §5.4, & §5.5 (Several examples here; also Lab #4)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 4- part 2 Control Statements: Loops 1
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Looping.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Additional Control Structures
Chapter 4 Control Statements: Loops 1
copyright © Michael B. Feldman, All Rights Reserved
Faculty of Computer Science & Information System
Chapter 6: User-Defined Functions I
Alternate Version of STARTING OUT WITH C++ 4th Edition
C:\> sort_3_numbers.exe |
ECE 103 Engineering Programming Chapter 18 Iteration
Presentation transcript:

Chap 7- Control Structures : The WHILE Statement

n The test in WHILE is opposite of the test in the general LOOP n Test the loop exit condition at the top of the loop n The loop body is repeated as long as the condition expression is true n Loop repetition stops when the conditon is false

In General Loop PowerOf2 := 1; LOOP EXIT WHEN Power >= 10000; Ada.Integer_Text_IO.Put (Item => PowerOf2); PowerOf2 := PowerOf2 *2; END LOOP ;

In WHILE Loop PowerOf2 := 1; WHILE PowerOf2 < LOOP Ada.Integer_Text_IO.Put (Item => PowerOf2); PowerOf2 := PowerOf2 *2; END LOOP ;

Form WHILE expression LOOP statement sequence END LOOP;

Robust Exception Handling A Robust Ada Program n retain control and behave predictably even exceptions are raised

Template for a Robust Input Loop - Initial Version LOOP n Prompt the user for an input value n Get the input value from the user n EXIT the loop if and only if no exception was raised on input n If an exception was raised, notify the user END LOOP

EXIT statement n Meaningful statement only within a loop structure n Transfer control to the next statement after the nearest END LOOP

Template for a Robust Input Loop - Refined Version LOOP BEGIN Prompt the user for an input value Get the input value from the user EXIT; -- valid data EXCEPTION -- invalid data Determine which exception was raised, and notify the user END END LOOP

Form for Exception Handler WHEN exception name => sequence of statements Example, WHEN Constraint_Error => Ada.Text_IO.Put(Item=>”Input number is out of range”); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item=>”Input number is out of range”); Ada.Text_IO.New_Line;

Exception Handler Block Form: Begin normal sequence of statements EXCEPTION WHEN exception name1 => sequence of statements1 WHEN exception name2 => sequence of statements2…. WHEN exception nameN => sequence of statementsN END;

An Example of Robust Numeric Input WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PROCEDURE Exception_Loop IS | Illustrates how to write a robust input loop that --| prompts user to re-enter invalid input and --| refuses to continue until input is good MinVal : CONSTANT Integer := -10; MaxVal : CONSTANT Integer := 10; SUBTYPE SmallInt IS Integer RANGE MinVal.. MaxVal; InputValue: SmallInt; Sum: Integer;

BEGIN -- Exception_Loop Sum := 0; FOR Count IN 1..5 LOOP LOOP -- inner loop just to control robust input BEGIN -- block for exception handler Ada.Text_IO.Put(Item => "Enter an integer between "); Ada.Integer_Text_IO.Put (Item => SmallInt'First, Width => 0); Ada.Text_IO.Put(Item => " and "); Ada.Integer_Text_IO.Put (Item => SmallInt'Last, Width => 0); Ada.Text_IO.Put(Item => " > "); Ada.Integer_Text_IO.Get(Item => InputValue); EXIT; -- leave the loop only upon correct input

EXCEPTION WHEN Constraint_Error => Ada.Text_IO.Put ("Value is out of range. Please try again."); Ada.Text_IO.New_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put ("Value is not an integer. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; END; -- block for exception handler END LOOP; Sum := Sum + InputValue; -- add new value into Sum END LOOP;

Ada.Text_IO.Put (Item => "The sum is "); Ada.Integer_Text_IO. Put (Item => Sum, Width => 1); Ada.Text_IO.New_Line; END Exception_Loop;

Procedure n Procedures and functions are both subprograms, But n A procedure is called with a procedure call n A procedure does not return a result n A procedure is allowed to have parameters of three kinds or modes: – IN parameter – OUT parameter – IN OUT parameter

IN parameters – are passed into the procedure and, inside the procedure, they are treated as constants and may not be changed

OUT Parameters – are computed in the procedure and passed out to the caller

IN OUT parameters – are passed into the procedure, possibly changed by it, and passed back out again.

Sort Three Numbers n Suppose we have a procedure called Order n ‘Order’ procedure to order the values of value1 and value2 n After the procedure call, value1 and value2 are in order

For given three numbers Num1,Num2, Num3 n order Num1 and Num2 n order Num1 and Num3 n order Num2 and Num3 n Now every thing is in order

Example 1 For7, 5, 3 n order 7 and 5, then it becomes 5, 7, and 3 n order 5 and 3, then it becomes 3, 7, and 5 n order 7 and 5, then it becomes 3, 5, and 7 that is, n order Num1 and Num2, then it becomes 5, 7, and 3 n order Num1 and Num3, then it becomes 3, 7, and 5 n order Num2 and Num3, then it becomes 3, 5, and 7

Example 2 For 5, 3, 7 n order 5 and 3, then it becomes 3, 5 and 7 n order 3 and 7, then it remains 3, 5, and 7 n order 5 and 7, then it remains 3, 5 and 7 that is, n order Num1 and Num2, then it becomes 3, 5, and 7 n order Num1 and Num3, then it becomes 3, 5, and 7 n order Num2 and Num3, then it becomes 3, 5, and 7

Example 3 For5, 7, 3 n order 5 and 7, then it becomes 5, 7 and 3 n order 5 and 3, then it becomes 3, 7, and 5 n order 7 and 5, then it becomes 3, 5 and 7 that is, n order Num1 and Num2, then it becomes 5, 7, and 3 n order Num1 and Num3, then it becomes 3, 7, and 5 n order Num2 and Num3, then it becomes 3, 5, and 7

Order Procedure PROCEDURE Order (X: IN OUT Float; Y: IN OUT Float) IS -- Pre: X and Y are assigned values. -- Post: X has the smaller value and Y has the larger value. Temp : Float; -- copy of number originally in X BEGIN -- Order IF X > Y THEN -- interchange the values of X and Y Temp := X; -- Store old X in Temp X := Y; -- Store old Y in X Y := Temp; -- Store old X in Y END IF; END Order;

Example - using order procedure WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Sort_3_Numbers IS | Reads three numbers and sorts them --| so that they are in increasing order Num1 : Float; -- a list of three cells Num2 : Float; Num3 : Float;

Cont. sort program -- Procedure is like function -- It is required to write Specification and Body procedure specification PROCEDURE Order (X: IN OUT Float; Y: IN OUT Float);

Cont. sort program -- procedure body PROCEDURE Order (X: IN OUT Float; Y: IN OUT Float) IS -- Pre: X and Y are assigned values. -- Post: X has the smaller value and Y has the larger value. Temp : Float; -- copy of number originally in X -- Local variable BEGIN -- Order IF X > Y THEN -- interchange the values of X and Y Temp := X; -- Store old X in Temp X := Y; -- Store old Y in X Y := Temp; -- Store old X in Y END IF; END Order;

Cont. sort program BEGIN -- Sort_3_Numbers Ada.Text_IO.Put (Item => "Enter 3 float values to be sorted>"); Ada.Text_IO.New_Line; Ada.Float_Text_IO.Get(Item => Num1); Ada.Float_Text_IO.Get(Item => Num2); Ada.Float_Text_IO.Get(Item => Num3); -- Sort the numbers Order (X => Num1, Y => Num2); Order (X => Num1, Y => Num3); Order (X => Num2, Y => Num3);

Cont. sort program -- Display the results. Ada.Text_IO.Put(Item => "The three numbers in order are: "); Ada.Float_Text_IO.Put (Item => Num1, Fore => 5, Aft => 2, Exp => 0); Ada.Float_Text_IO.Put (Item => Num2, Fore => 5, Aft => 2, Exp => 0); Ada.Float_Text_IO.Put (Item => Num3, Fore => 5, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; END Sort_3_Numbers;

Procedure Specification Form: PROCDURE PNAME (FORMAL-PARAMETERS); Example: Procedure Double(X : IN Integer; Y: OUT Integer);

Procedure Body Form: PROCDURE PNAME (FORMAL-PARAMETERS) IS local declaration-section; Begin Statement sequence End PNAME;

Example: Procedure Double(X : IN Integer; Y: OUT Integer) IS Begin Y := 2 * X; End Double;

Procedure Call Form: PNAME ( actual-parameters); Example: Double ( X => P, Y => Q );

Rules for Parameter List Correspondence Formal and actual parameter correspondence n by named association or by position n by type Actual parameter n variable for OUT or IN OUT parameter mode n variable, constant, or expression for IN parameter mode

Style n Naming is used to associate each formal parameter with an actual parameter (optional) n Order of actual parameters does not have to match the order of the formal parameters

But, it is a good practice to use n named association n list the actual parameters in an order corresponding to the order of the formal parameters