1) Fixed count loops 2) Variable count loops 3) Post-test Loops 4) Terminating a loop COMP205 IMPERATIVE LANGUAGES 11. PROGRAM CONSTRUCTS 2 (REPETITION)

Slides:



Advertisements
Similar presentations
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
COMP205 IMPERATIVE LANGUAGES
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
TYPE EQUIVALENCE 1) Coercion 2) Casting 3) Conversion.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
sequence of execution of high-level statements
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
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 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
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)
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming Techniques
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
The Repetition control structure using while loop.
CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction.
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
COMP205 IMPERATIVE LANGUAGES
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after another in sequence Some programming.
Control Structures Lecture 7.
Structured Program Development in C
Structured Program
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 6: Repetition Statements
UMBC CMSC 104 – Section 01, Fall 2016
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:

1) Fixed count loops 2) Variable count loops 3) Post-test Loops 4) Terminating a loop COMP205 IMPERATIVE LANGUAGES 11. PROGRAM CONSTRUCTS 2 (REPETITION)

REPETITION Two main forms of repetition: 1) Fixed Count Loops: Repetition over a finite set (for loops). 2) Variable Count Loops: Repetition as long as a given condition holds (while and do-while loops). Loops can be be further classified as being either pre-test (while loops) or post-test (do-while or repeat-until loops). We could add recursion to the above (routine calls itself). Although not used so much in imperative languages recursion is the principal program construct used in logic and functional languages.

FIXED COUNT (FOR) LOOPS

Allow a statement to be repeated a given number of times according to a given control variable (i.e a counter). On each loop the control value is incremented (or decremented) until the end condition is met when the loop ends. Issues 1) Nature of the control value. 2) Nature of end condition. 3) Incrementation 4) Decrementation 5) Nesting

NATURE OF CONTROL VARIABLE In some imperative languages (Ada) the control variable is initialised automatically during compilation. In others it must be declared specifically. NATURE OF END CONDITION On each loop the control value is tested against the end condition. The end condition is usually a Boolean expression or a sequence of such expressions.

INCREMENTATION This may be carried out automatically as designated by the compiler or as defined by the programmer. Incrementation is much more flexible if defined by the programmer. In Ada the control value is incremented automatically in steps of 1. In C the user defines the nature of the incrementation.

DECREMENTATION In languages where the control value is automatically incremented, to process a series of statements in reverse, an adjective such as reverse (Ada) or downto (Pascal) must be included in the definition of the loop. In languages where the programmer controls incrementation, decrementation can be defined in the same manner. NESTING All common imperative languages support nesting of fixed count loops.

Nested pre-test loops T TF F

procedure EXAMPLE is DIGIT: array (integer range 0..9) of integer; begin for INDEX in 0..9 loop DIGIT(INDEX) := INDEX; end loop; for INDEX in reverse 0..9 loop put(DIGIT(INDEX)); end loop; new_line; end EXAMPLE; ADA "FOR-LOOP" EXAMPLE (processing an array)

void main(void) { int index=0, digits[10]; for (index;index<10;index++) { digits[index] = index; } for (index=9;index>=0;index--) { printf("%d ",digits[index]); } printf("\n"); } C "FOR-LOOP" EXAMPLE (processing an array)

#include void main(void) { int num1, num2; for (num1=4; num1!=1; num1--) { for (num2=2; num2<=6; num2=num2+2) { printf(" %d",num1+num2); } printf("\n"); } C “NESTED FOR- LOOP” EXAMPLE

# include void main(void) { char letter; int number; for (letter = 'A', number = 2; letter < 'J'; letter++, number = number+5) { printf("%c(%d), %c(%d)\n",letter, letter,letter+number, letter+number); } C EXAMPLE "FOR LOOP" WITH TWO CONTROL VARIABLES A(65), C(67) B(66), I(73) C(67), O(79) D(68), U(85) E(69), [(91) F(70), a(97) G(71), g(103) H(72), m(109) I(73), s(115)

VARIABLE COUNT (WHILE) LOOPS

Variable count loops (also known as while or conditional loops) allow statements to be repeated an indeterminate number of times. The repetition continues until a control condition (usually a Boolean expression) is no longer fulfilled. The is tested for before each repetition (pretest loop) Condition must be: 1) Initialised prior to the start of the loop, and then 2) Changed on each repetition of the loop.

procedure EXAMPLE is X: integer range 0..10; begin put_line("Input integer 0..10") get(X); while X <= 10 loop put(X); put(X*X); put(X*X*X); new_line; X := X + 1; end loop; end EXAMPLE; ADA VARIABLE COUNT (WHILE) LOOP EXAMPLE

void main(void) { int x = 1; printf("Input integer 0..10\n"); scanf("%d",&x); while (x <= 10) { printf("%d %d %d\n",x,x*x,x*x*x); x++; } C VARIABLE COUNT (WHILE) LOOP EXAMPLE

“FOR” LOOP OR “WHILE” LOOP? Both loops are pre-test loops. In languages where "for" loops can only be used to describe fixed count loops (e.g. Ada, Pascal, BASIC) the answer is straight forward: –For statements for fixed count loops –While statement for variable count loops In C where for and while statements can be used to describe both fixed and variable count loops, it simply a matter of style.

COMPARISON OF C WHILE AND FOR LOOPS (Variable count examples) void main(void) { int x; scanf("%d",x); while (x < = 10) { printf("%d ",x); printf("%d ",x*x); printf("%d\n",x*x*x); x++; } void main(void) { int x; scanf("%d",x); for(; x < = 10; x++) { printf("%d ",x); printf("%d ",x*x); printf("%d\n",x*x*x); x++; }

POST-TEST LOOPS

Whereas for pre-test loops the control variable is tested prior to, the start of each iteration in post-test loops the control variable is tested at the end of each iteration.

void main(void){ int numTerms=0, frstTerm, scndTerm, tempNum; do { if (numTerms == 0) { frstTerm = 0; printf(“0”); } else if (numTerms == 1) { scndTerm = 1; printf(“1”); } else { tempNum = frstTerm+scndTerm; frstTerm = scndTerm; scndTerm = tempNum; printf("%d ",scndTerm); } numTerms++; } while (numTerms < 11); printf("\n"); } C POSTEST DO- WHILE LOOP EXAMPLE (FIBONACCI)

COMPARISON OF C PRE-TEST (WHILE) AND POST-TEST (DO-WHILE) LOOPS (Input example) void main(void) { int number; printf("Input an integer between 1 and 50 (inclusive)\n"); do { scanf("%d",&number); } while (number 50); printf("%d\n",number); } void main(void) { int number = 0; printf("Input an integer between 1 and 50 (inclusive)\n"); while (number 50) { scanf("%d",&number); } printf("%d\n",number); }

TERMINATING A LOOP

Sometimes there is a need to terminate a loop somewhere in the middle, for example when a sentinel value is reached. Many languages therefore supply a break (C) or exit (Ada) statement. Ada actually supplies two types of exit statement, exit and exit when.

Pre-test loop with exit

Post-test loop with exit

void main(void) { int seed; printf("Enter seed (odd number range )\n"); do { scanf("%d",&seed); if (seed 8191) { printf("Invalid seed: %d, ",seed); printf("seed must be range , try again!\n"); } else { if (seed % 2 == 1) break; else { printf("Invalid seed: %d, ",seed); printf("seed must be odd number, try again!\n"); } } while(1) ; printf("Seed = %d\n",seed); } C LOOP EXAMPLE WITH “BREAK”

procedure LOOP_EXAMPLE is NUMBER : INTEGER ; begin PUT("Input an integer between "); PUT_LINE("0 and 50 (inclusive)"); loop GET(NUMBER); exit when (NUMBER 0); end loop; PUT(NUMBER); NEW_LINE; end LOOP_EXAMPLE; USING AN ADA “EXIT WHEN” STATEMENT TO ACHIEVE THE EFFECT OF A POST-TEST LOOP

procedure LOOP_EXAMPLE is NUMBER : INTEGER := 10; begin PUT("Input an integer between "); PUT_LINE("0 and 50 (inclusive)"); while (NUMBER 0) loop GET(NUMBER); end loop; PUT(NUMBER); NEW_LINE; end LOOP_EXAMPLE; USE OF EXIT STATEMENTS CAN BE AVOIDED!

SUMMARY: PROGRAM CONSTRUCTS 2 1) Fixed count loops 2) Variable count loops 3) Post-test Loops 4) Terminating a loop