Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.

Slides:



Advertisements
Similar presentations
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Advertisements

June 10, 2015ICS102: while & do-while1 while and do-while Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
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.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
©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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 4: Control Structures II
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
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.
Java Programming: From the Ground Up
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Java Fundamentals 4.
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Control Structures II (Repetition)
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
The for-loop and Nested loops
Control Statements Loops.
Chapter 5: Control Structures II (Repetition)
Control Statements Loops.
Repetition Statements
Presentation transcript:

Control Structures II

Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas used to find average grades for students in a class. 2

Repetition Java has three repetition, or looping, structures that let you repeat statements over and over again until certain conditions are met:  while  for  do…while 3

The while Looping (Repetition) Structure Syntax: while (expression) statement Statements must change value of expression to false. A loop that continues to execute endlessly is called an infinite loop (expression is always true). 4

The while Looping (Repetition) Structure Example 5-1 i = 0; while (i <= 20) { System.out.print(i + " "); i = i + 5; } System.out.println(); Output

Sentinel-Controlled while Loop Used when exact number of entry pieces is unknown, but last entry (special/sentinel value) is known. General form: Input the first data item into variable; while (variable != sentinel) {. input a data item into variable;. } 6

Sentinel-Controlled while Loop Example 5-4 //Sentinel-controlled while loop import java.util.*; public class SentinelControlledWhileLoop { static Scanner console = new Scanner(System.in); static final int SENTINEL = -999; public static void main (String[] args) { int number; //variable to store the number int sum = 0; //variable to store the sum int count = 0; //variable to store the total //numbers read System.out.println("Enter positive integers " + "ending with " + SENTINEL); 7

Sentinel-Controlled while Loop Example 5-4 (continued) number = console.nextInt(); while (number != SENTINEL) { sum = sum + number; count++; number = console.nextInt(); } System.out.println("The sum of the “+ count +”numbers = “ +sum); if (count != 0) System.out.println("The average = “+(sum / count)); else System.out.println("No input"); } } 8

Flag-Controlled while Loop Boolean value used to control loop. General form: boolean found = false; while (!found) {. if (expression) found = true;. } 9

The for Looping (Repetition) Structure Specialized form of while loop. Its primary purpose is to simplify the writing of counter-controlled loops. For this reason, the for loop is typically called a counted or indexed for loop.. Syntax: for (initial statement; loop condition; update statement) statement 10

The for Looping (Repetition) Structure Example The following for loop outputs the word Hello and a star (on separate lines) five times: for (i = 1; i <= 5; i++) { System.out.println("Hello"); System.out.println("*"); } 2.The following for loop outputs the word Hello five times and the star only once: for (i = 1; i <= 5; i++) System.out.println("Hello"); System.out.println("*"); 11

The for Looping (Repetition) Structure Does not execute if loop condition is initially false. Update expression changes value of loop control variable, eventually making it false. If loop condition is always true, result is an infinite loop. Infinite loop can be specified by omitting all three control statements. 12

For Loop Programming Example: Classify Numbers Input: N integers (positive, negative, and zeros). int N = 20; //N easily modified Output: Number of 0s, number of even integers, number of odd integers. 13

For Loop Programming Example: Classify Numbers (solution) for (counter = 1; counter <= N; counter++) { number = console.nextInt(); System.out.print(number + " "); switch (number % 2) { case 0: evens++; if (number == 0) zeros++; break; case 1: case -1: odds++; } //end switch } //end for loop 14

The do…while Loop (Repetition) Structure Syntax: do statement while (expression); Statements are executed first and then expression is evaluated. Statements are executed at least once and then continued if expression is true. 15

do…while Loop (Post-Test Loop) 16

do…while Loop (Post-Test Loop) Example : i = 0 ; do { System.out.print(i + “ “ ) ; i = i + 5 ; } while ( i <= 30 ) ; output :

break Statements Used to exit early from a loop. ( while, for, and do... while) skip remainder of switch structure. Can be placed within if statement of a loop. If condition is met, loop is exited immediately. After the break statement executes, the program continues to execute with the first statement after the structure 18

break Statements Example : int count ; for ( count = 1 ; count <= 10 ; count ++ ) {if ( count == 5) break ; System.out.print(count + “ ” ); } 19 Output

continue Statements Used in while, for, and do... while structures. When executed in a loop, the remaining statements in the loop are skipped; proceeds with the next iteration of the loop. When executed in a while / do … while structure, expression is evaluated immediately after continue statement. In a for structure, the update statement is executed after the continue statement; the loop condition then executes. 20

continue Statements Example : int count ; for ( count = 1; count <= 10 ; count ++ ) {if ( count == 5) continue; System.out.print(count + “ ” ); } 21 Output

Nested Control Structures Provides new power, subtlety, and complexity. if, if … else, and switch structures can be placed within while loops. for loops can be found within other for loops. 22

Nested Control Structures (Example 5-18) for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) System.out.print(" *"); System.out.println(); } Output: * ** *** **** ***** 23