Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Introduction to C Programming
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CS0007: Introduction to Computer Programming
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Repeating Actions While and For Loops
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS150 Introduction to Computer Science 1
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Looping Yong Choi School of Business CSU, Bakersfield.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Copyright © Texas Education Agency, Computer Programming For Loops.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
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.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
1. Agenda for loop Short-handed notation How to use break and continue 2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
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.
The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Counting Loops.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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 Looping 5. The Increment and Decrement Operators 5.1.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Alterations u assignment is the operation provided by programming languages for altering the value stored by a variable. u The assignment operator (=)
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 9 Repetition.
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
Java's for Statement.
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.
Chapter 5: Loops and Files.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 Repetition.
Unary Operators ++ and --
Lec 6.
Repetition Control Structure
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
JavaScript 101 Lesson 8: Loops.
Control Statements:.
Presentation transcript:

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things

Increment and Decrement counter = counter + 1;// Add 1 and reset variable ++counter;// Prefix increment counter++;// Postfix increment counter = counter - 1; // Subtract 1 and reset variable --counter;// Prefix decrement counter--;// Postfix decrement

The for Loop for (int counter = 1; counter <= 10; ++counter) for (int counter = 10; counter >= 1; --counter) Count up and do something 10 times Count down and do something 10 times

Syntax and Semantics of the for Loop for ( ; ; ) termination statement true false initializer update Loop header Loop body

How It works for (int counter = 1; counter <= 10; ++counter) 1. Initialize the loop control variable before the loop runs

How It works for (int counter = 1; counter <= 10; ++counter) 2. Test the termination condition before running the loop body (entry control). If true, run the loop body; if false, exit the loop.

How It works for (int counter = 1; counter <= 10; ++counter) 3. Run the loop body.

How It works for (int counter = 1; counter <= 10; ++counter) 4. Update the loop control variable so it eventually will allow the loop to terminate.

The while Loop Write code that computes the sum of the numbers between 1 and 10. int counter = 1; int sum = 0; while (counter <= 10) { sum = sum + counter; counter = counter + 1; }

Syntax and Semantics of while Statements while ( ) while ( ) {. } ? statement true false

The do…while Loop Write code that inputs numbers (at least one) until -999 is input. int number; do { cout << "Enter a number: "; cin >> number; } while (number != -999); Uses exit control; the body of the loop runs at least once.

Syntax and Semantics of do… while Statements do while ( ); Do {. } while ( ); statement false ? true

Designing Correct Loops Initialize all variables properly –Plan how many iterations, then set the counter and the limit accordingly Check the logic of the termination condition Update the loop control variable properly

Off-by-One Error int counter = 1; while (counter <= 10) { // Executes 10 passes counter++; } int counter = 1; while (counter < 10) { // Executes 9 passes counter++; }

Infinite Loop int counter = 1; while (counter <= 10) { // Executes 5 passes counter = counter + 2; } int counter = 1; while (counter != 10) { // Runs forever counter = counter + 2; } In general, avoid using != in loop termination conditions.

Testing Loops Can vary the limit or the control variable, or both Use a negative value, zero, and a positive value Display an output trace if things arent working