LOOPING What are the 3 structures of writing code? sequential decision repetition Def. Looping is the repetition of statements in a program. There various.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
1 Fall 2007ACS-1903 Ch 4 Loops and Files Increment & decrement statements while loop do-while loop Other topics later on …
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
The switch Statement, DecimalFormat, and Introduction to Looping
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
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.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
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 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
While ( number
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Follow up from lab See Magic8Ball.java Issues that you ran into.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Topic 4: Looping Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
The switch Statement, and Introduction to Looping
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Repetition Structures
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Module 4 Loops.
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Loop Strategies Repetition Playbook.
Repetition Statements (Loops) - 2
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.
CSCI 1100/1202 February 6, 2002.
Presentation transcript:

LOOPING What are the 3 structures of writing code? sequential decision repetition Def. Looping is the repetition of statements in a program. There various types of loops: controlled by a flag, count controlled, state controlled

Loop controlled by a boolean flag* and a pretest The flow of logic of a loop is shown in a flowchart: end flag = false !flag == ? true body of loopupdate flag false start *sometimes called a sentinel

boolean done = false;//initialization step while ( ! done )//loop condition { statements //body of loop... If (something) done = true; } //update flag based //on some decision //sometimes from user NOTE: this is a pretest loop the body is executed only when the loop condition is TRUE. See ex. p.241

Loop controlled by a boolean flag and a posttest //no initialization necessary do {statements;... update flag; } while ( flag ); Note: that means flag is true Note: the test is not made until the body has executed once.

Example of a do - while loop : char ch; boolean ok; do { ch = getChar(“Enter a digit”); if (ch ‘9’) ok = false; } while (! ok );

Count controlled loops int lcv = 0; // initialization while ( lcv < 10 ) // final value in condition {println ( “ HI! ” ); // body of loop lcv++; // or lcv = lcv + 1 (update) } Note: each execution of the body of the loop is called one iteration. Is this a pre or posttest loop? What change would make it zero iterations? pretest lcv = 10

Write a loop that will output the integers 1 to 10 Write a loop that will output the integers 10 to 1.

Summation is a common use of looping. Let’s write a loop to find the sum of the integer 1 to 10. int sum = 0; // called the accumulator int count = 0; // initialization of loop control variable while ( count < 10 ) // loop condition {sum = sum + count; // or sum += count count ++; // or count = count + 1 } //end while

State controlled loops (similar to flag-controlled) The loop is ended when a state is reached that causes the loop-repetition condition to become false. See ex bottom p 249 See ex bottom p 261 Question: What is the difference between a sentinel-controlled loop and a state-controlled loop? A sentinel is a “dummy data value” an end marker. A state is reached that causes the boolean condition to become false.

for loops Note: this is an option that most languages provide to condense code but is not necessary. for ( lcv = initial value; lcv < final value; lcv ++ ) Notice: how 3 of the 4 parts of a loop are in this line. Rewrite the while “count to 10” loop using for. Question: can every while loop be written as a for loop? No. Only when the number of repetitions is known ahead of time. Therefore, not a flag controlled and only some state controlled loops. Question: does the initial value have to be 1?no How many iterations of this loop? For (ct = 10; ct <= 20; ct++) { };

What does this loop do? for (int x = 0; x < 32,768; x++); nothing but count to 32,768 - this provides a pause. (It is also a common error: do not put a semi- colon after the parentheses unless you want a loop that does nothing.)

What does this do? For (ct = 10; ct != 20; ct +=3 ) { println ( “Hi!”); } Trace: ctoutput 10Hi 13Hi 16Hi 19Hi 22Hi Caution: infinite loop

Can the lcv be any type besides int? yes. Any ordinal type: any primitve data type whose elements can be listed in sequential order. (short, int, long, char) Is a for loop a pretest loop, posttest loop or neither? Pretest. Write a loop whose body will never be executed.

What does this loop do: for ( char ch = ‘A’; ch <= ‘Z’; ch++) displayResult(ch + “ “ + (int) ch); A 65 B 66 … Z 90