Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 6 - Visual Basic Schneider
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
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.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
JavaScript, Fourth Edition
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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 /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Controlling Program Flow with Looping Structures
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Chapter 6 Controlling Program Flow with Looping Structures.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 4 – C Program Control
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
Week 4 – Repetition Structures / Loops
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements I
Looping and Repetition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Control Structure
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled loops are executed a specific number of times. –Conditional loops are executed an indefinite number of times.

The While Loop The while loop is a conditional loop. It is executed an indefinite number of times. –A while loop terminates based upon a boolean expression becoming false. As long as the expression is true, the loop will be executed.

The While Loop Syntax of a while loop: while (boolean_expression) statement1; while(boolean_expression) { statement1; statement2; statement3; }

The While Loop { int num = 0; while (num < 5) num = num + 1; } num = num + 1; num < 5 False True

The While Loop If the boolean_expression is false when the while loop is encountered, the statements inside the loop are never executed. If the boolean_expression never becomes false, then the statements inside the loop are executed “forever” in an infinite loop.

The While Loop What happens when we execute the following loops: Assume x = 4; while (x < 10) x += 4; while (x < 10); x += 4;

The While Loop Write a program to print the squares of the even numbers between 0 and 10. Print the results out in a table.

The Do/While Loop The do/while repetition structure is very similar to the while repetition structure. The difference between the two is where the conditional test is performed. –While statement: the conditional test is performed before the body of the loop is executed. –Do/while statement: the conditional test is performed after the body of the loop is executed. This means the body of the loop is executed at least once.

The Do/While Loop The structure of a Do/While loop is: do { statement1; … statementN; } while (boolean_expression)

The Do/While Loop { // While loop int num = 0; while (num < 5) num = num + 1; } { // Do/While loop int num = 0; do num = num + 1; while (num < 5); } num = num + 1; num < 5 False True num = num + 1; num < 5 False True

The Do/While Loop If the boolean_expression is false when the while section of the loop is encountered, the statements inside the loop are only executed once. If the boolean_expression never becomes false, then the statements inside the loop are executed “forever” in an infinite loop.

The Do/While Loop Let’s rewrite the previous program to repeatedly ask the user to enter a lower and upper bound for which to calculate the squares of the even numbers. This time we will use a Do/While Loop.

The Do/While Loop What is printed when the following code is executed: int x = 1, y = 1; do { while ( y < x ) { outputBox.print(“*”); y++; } // end of while loop outputBox.printLine(‘\n’); } while (x <= 5)

Counter-Controlled Repetition Requires –A named variable that acts as the storage location for the number of times the loop is executed. –An initial value for the named variable. –An increment or decrement statement that is applied to the named variable. –A conditional test that looks for the final value of the named variable.

Counter Controlled Repetition int x = 0; while (x < 5) { statement1; x++; }

For Repetition Structure A For repetition structure is essentially a counter-controlled repetition structure BUT it does the work for you!

For Repetition Structure Structure for (int = initialValue; boolean_expression; incrementExpression ) statement1; for (int = initialValue; boolean_expression; incrementExpression ) { statement1; … statementN; }

For repetition Structure for (int counter = 1; counter <= 10; counter++){ output.printLine(counter); } outputBox.printLine( counter); counter <= 10 False True counter = 1 counter++

For Repetition Structure Write a program to print the squares of the even numbers between 0 and 10. Print the results out in a table.

For Repetition Structure Rewrite the previous program to repeatedly ask the user to enter a lower and upper bound for which to calculate the squares of the even numbers.

For Repetition Structure Examples of for structures: –for ( ; I <= 100; I++) (assume int I = 0; precedes this statement) –for (int I = j; I > m*n; I += 6) (where j = 0, m = 3, and n = 5) – for (int I = 100; I >= 1; I--) –for (int I = 7; I <= 77; I += 7)

For Repetition Structure For loop: for (expression1; expression2; expression3) statement; –What happens if expression2 is omitted? –What happens if expression1 is completed before the for structure? –What happens if expression3 is completed in the body of the for loop?

For Repetition Structure What happens when the following loop is executed? for (j = 1; j < 10; j++); outputBox.printLine(‘H’);

For repetition Structure What does the following Loop print out? for (i = 1; i <= 5; i++){ for (j = 1; j <=3; j++){ for(k = 1; k <= 4; k++) outputBox.printLine(“*“); outputBox.printLine(‘\n’); } outputBox.printLine(‘\n’); }

For Repetition Structure Rules for using For Loops –You should indent the body of the for loop, the statements. –You should not modify the index/counter variable inside the for loop. –Never use floats or doubles for the index counter in a for loop. –Make sure you use a ‘;’ and not a ‘,’ to separate the control statements.

For loop vs. While loop In most cases a For loop can be represented as a While loop. For Loop: for (expression1; expression2; expression3) statement While Loop: expression1; while (expression2) { statement; expression3; }

Format Class The Format class can be used to format data for output. –The Format class needs to know how many characters are to be printed and what value is to be printed.

Format Class The basic statements for formatting integer output are: –Format.leftAlign(, ) –Format.rightAlign(, ) –Format.centerAlign(, )

Format Class The basic statements for real numbers are: –Format.leftAlign(,, ) –Format.rightAlign(,, ) –Format.centerAlign(,, )

ResponseBox The default ResponseBox class creates a dialog box that contains text and two buttons. –The text usually includes a question. –The buttons are labeled “Yes” and “No”.

ResponseBox –The prompt method is used to display the dialog box and get the user’s response. MainWindow mainWindow = new MainWindow(); ResponseBox yesNoBox = new ResponseBox(mainWindow); int selection = yesNoBox.prompt(“Today is Monday”);

ResponseBox –To test the selection compare the input to either ResponseBox.YES or ResponseBox.NO.

ResponseBox A ResponseBox can have up to three buttons. MainWindow mainWindow = new MainWindow(); ResponseBox newBox = new ResponseBox(mainWindow); newBox.setLabel(ResponseBox.BUTTON1, “One”); newBox.setLabel(ResponseBox.BUTTON2, “Two”); newBox.setLabel(ResponseBox.BUTTON3, “Three”);

ResponseBox –The test comparison for the button selected with a three button response box is: selection == ResponseBox.BUTTON1 selection == ResponseBox.BUTTON2 selection == ResponseBox.BUTTON3 See page 307 for more details.