Department of Computer Science

Slides:



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

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS150 Introduction to Computer Science 1
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
do - while  while: Execute the body of the loop at least once
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
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);
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UCT Department of Computer Science Computer Science 1015F Iteration
Department of Computer Science
Start reading Sec and chapter 7 on loops
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
JavaScript: Control Statements I
CHAPTER 5A Loop Structure
Counted Loops.
Lecture 07 More Repetition Richard Gesick.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
LOOPS.
Lecture 4B More Repetition Richard Gesick
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
- Additional C Statements
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
Structured Program Development in C
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Topics discussed in this section:
3 Control Statements:.
Let’s all Repeat Together
More Loops Topics Counter-Controlled (Definite) Repetition
Building Java Programs
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
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
Programming Fundamental
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Department of Computer Science COM S 207 For-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu

The for loop condition checked before each iteration loop variable, initialized before the loop starts update extended after each iteration no semi-column here for (int i=5; i<=10; i++) { Statement1; Statement2; ::: } // end for i body of loop A for loop is a count-controlled loop whereas a while loop is an event-controlled loop

for loop vs. while loop A for loop is a count-controlled loop whereas a while loop is an event-controlled loop for (int i=5; i<=10; i++) { Statement1; Statement2; ::: } // end for i while (condition) { Statement1; Statement2; ::: } // end while A count-controller loop is executed a definite number of times. In an event-controlled loop, we do not know how many iterations of the loop body may be executed – it is also called an infinite loop

A for loop can be countdown instead of up for (int counter = 10; counter >= 0; counter--) { System.out.println(counter); } // end for counter body executed 11 times counter = 10; counter = 9; :: counter = 0;

The increment or decrement to the loop variable does not have to be 1 -- it can be any other number, but the same value is used for each iteration for (int cntr = 10; cntr >= 0; cntr = cntr - 2) { System.out.println(counter); } // end for cntr

Comprehension Checks for (i = 0; i<=5; i++) { System.out.println(i); } // end for i for (i=5; i<=0; i--) { System.out.println(i); } // end for i for (i = 0; i<=9; i = i + 2) { System.out.println(i); } // end for i for (i = 0; i!=9; i = i + 2) { System.out.println(i); } // end for i

Comprehension Checks for (i = 1; i <= 20; i = i * 2) { System.out.println(i); } // end for i for (i = 1; i <= str.length(); i++) { System.out.println(i); } // end for i for (int n = 10; n >= 0; n--) { System.out.print(n + “ “); } // end for n

Write Loops that computes the sum of all integers between -120 and 2312 (inclusive) the sum of all EVEN integers between 78 and 3090 (exclusive) the sum of all SQUARES of integers between -300 and +3300 (inclusive) the average of all ODD integers between -123 and 321 (inclusive)

Ex. 1: Given a number, check if it is a prime number Pseudocode ask for number test against denominator, starting from 2, 3, ..., number/2 if number % denominator = 0, then number is not a prime number otherwise, number is a prime number

More examples Given a number, check if it is a prime number Given a range, find out all prime numbers in between Given a number, print its binary digits (e.g., 15 -> 1111, 20 ->11000) Given a number, print its hexadecimal digits (e.g., 9->9, 10->A, 15 -> F, 20 -> 14)

Sentinel loop (sec. 4.4) loops that have a pre-determined sentinel value for termination. Example: compute the average of a set of salary values and use -1 as a sentinel while (salary != 1) { salary = in.nextDouble(); if (salary != -1) sum = sum + salary; count++; } // end if } // end for while if (counter > 0) double average = sum / count; System.out.println(“Average salary is “ + average); } else System.out.println(“no data”);

Nested Loop A loop inside another loop X1 X2 X3 X4 1 1 1 1 2 4 8 16 1 1 1 1 2 4 8 16 3 9 27 81 : : : : 10 100 1000 1000