LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.

Slides:



Advertisements
Similar presentations
Repeating Actions While and For Loops
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 5: Control Structures II (Repetition)
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
CPS120 Introduction to Computer Science Iteration (Looping)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
REPETITION CONTROL STRUCTURE
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
JavaScript: Control Statements.
JavaScript: Control Statements I
Looping and Repetition
Chapter 6: Repetition Structures
Chapter 8: More on the Repetition Structure
Chapter 6: Repetition Statements
Lab5 PROGRAMMING 1 Loop chapter4.
PROGRAM FLOWCHART Iteration Statements.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Looping and Repetition
Presentation transcript:

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the loop.

while Loops example: while { statement(s) } Semantics 1.A while statement that contains a boolean expression that must be true before the body of the loop will be executed. 2.The body of the loop (the part being repeated). We indent the body of the loop and use braces if it contains more than 1 statement. -Any boolean expression could be used for., including compound tests using && and ||. -The WHILE loop is considered a Pre-Test loop. That is, if the Boolean expression. is false the first time you execute the WHILE statement, then the body of the loop will not be executed at all.

while Loops (continued) The computer will execute the body of the WHILE loop as long as the b.e. is true. It is up to the programmer to make sure that the Boolean expression. eventually becomes false, or you will have created an infinite loop.

C++ for Engineers and Scientists, Second Edition 4 while Loops (continued)

C++ for Engineers and Scientists, Second Edition 5 while Loops (continued) break statement: forces an immediate break, or exit, from switch, while, for, and do-while statements break statement violates pure structured programming, but is useful for breaking out of loops when an unusual condition is detected

C++ for Engineers and Scientists, Second Edition 6 while Loops (continued) Example of a break statement:

C++ for Engineers and Scientists, Second Edition 7 while Loops (continued) continue statement: applies to while, do-while, and for statements; causes the next iteration of the loop to begin immediately continue statement is useful for skipping over data that should not be processed in this iteration, while staying within the loop

C++ for Engineers and Scientists, Second Edition 8 while Loops (continued) A continue statement where invalid grades are ignored, and only valid grades are added to the total:

for loop int var; for( var = start ; var < somenumber ; var++) { statement(s) } The for loop is made up of: 1.A for statement that determines the starting point, what condition will keep the loop going, and the increment of a counting variable. 2.The body of the loop (the part being repeated). We indent the body of the loop to improve readability. If there is more that one statement, beginning and end braces must be used.

for loop To use for loops, you must know – where to begin – how high (or low) to count – what to count by. If any of these 3 values is not known, then you shouldn't be using a for loop. The for loop is a pretest loop. If the middle condition is false, it will skip the loop entirely.

C++ for Engineers and Scientists, Second Edition 11 for Loops (continued)

do / while Loop example: do { statement(s); } while(Boolean expression); Is made up of three parts: 1.A do statement that marks the beginning of the loop 2.The body of the loop (the part being repeated). We indent the body of the loop to improve readability. If more than one statement is in body, beginning and ending braces are required. 3.A while statement that signals the end of the loop It will contain a condition for continuing. -The boolean expression could include compound tests using && and ||. -The do/while loop is considered a posttest loop. A posttest loop will always be executed at least once, whereas a pretest loop may not be executed at all.

C++ for Engineers and Scientists, Second Edition 13 Nested Loops Nested loop: a loop contained within another loop – All statements of the inner loop must be completely contained within the outer loop; no overlap allowed – Different variables must be used to control each loop – For each single iteration of the outer loop, the inner loop runs through all of its iterations

C++ for Engineers and Scientists, Second Edition 14 Nested Loops (continued)