Chapter 3. Iteration

Similar documents
SELECTION. (Chapter 2)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Getting User Input. What I can I do now? CS 201F11 M. Wainer

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

CS112 Lecture: Repetition Statements

CS112 Lecture: Loops

More about Loops and Decisions

LECTURE 5 Control Structures Part 2

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Repetition and Loop Statements Chapter 5

More About WHILE Loops

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

REPETITION CONTROL STRUCTURE LOGO

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

Condition Controlled Loops. Introduction to Programming - Python

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops / Repetition Statements

OER on Loops in C Programming

Chapter Goals. Contents LOOPS

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

Laboratory 5: Implementing Loops and Loop Control Strategies

Chapter 4 Repetition Structures. Dr. Zhang COSC 1436 Spring 2017 June 15, 2017

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

CMPT 102 Introduction to Scientific Computer Programming

How Do Robots Find Their Way?

Text Input and Conditionals

Announcements - Grades UBLearns grades just updated Monday, March 28 th (11:00am). Please check grades.

CS 177 Week 15 Recitation Slides. Review

Dept. of CSE, IIT KGP

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

The action of the program depends on the input We can create this program using an if statement

Welcome! COMP s1. Programming Fundamentals

PREPARING FOR PRELIM 1

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

CSE 113 A. Announcements - Lab

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Warmup : Name that tune!

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Software often repeats a set of instructions over and over again. This repetition continues until some condition is met.

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

CS110D: PROGRAMMING LANGUAGE I

Selection and Repetition Revisited

Assignment 2.4: Loops

Subject: PIC Chapter 2.

Loops (while and for)

LAB: WHILE LOOPS IN C++

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Module 4: Decision-making and forming loops

Loops. Repeat after me

Selection and Repetition

Java Control Statements

Programming in C++ PART 2

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

IT 1033: Fundamentals of Programming Loops

Object-Oriented Programming in Java

Introduction. C provides two styles of flow control:

Java Coding 3. Over & over again!

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Chapter 9 Conjunctions

For full credit, show all work.

Chapter 2: Functions and Control Structures

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Decision Making and Loops

Study Guide and Review - Chapter 1

What you can do: Use Transparency #10

Lecture 10. Daily Puzzle

Loops / Repetition Statements

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

CSCE 110 Programming I

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Introduction to the Java Basics: Control Flow Statements

Repetition Structures

CS Computer Science I

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

204111: Computer and Programming

Problem Solving and Algorithms

What is PHP? [1] Figure 1 [1]

PLD Semester Exam Study Guide Dec. 2018

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Theory of control structures

LOOPS. Repetition using the while statement

PayFirst Salon User Guide:

Transcription:

Chapter 3 Iteration

Iteration Iteration is the form of program control that allows us to repeat a section of code. For this reason this form of control is often also referred to as repetition. The programming structure that is used to control this repetition is often called a loop. There are three types of loops in Java: for loop; while loop; do while loop.

When to use a loop? Display a square of stars (five by five) on the screen as follows: * * * * * * * * * * * * * * * * * * * * * * * * * This could be achieved with five output statements executed in sequence. Better to repeat one output statement five times with a loop.

The for loop If we wish to repeat a section of code a fixed number of times (five in the example above) we would use Java's for loop. The for loop is usually used in conjunction with a counter to keep track of how many times we have been through the loop so far:

Using a for loop to display a 5x5 square of stars

The loop counter Very common way of using a for loop: start the counter at 1; add 1 to the counter each time the loop repeats. However you may start your counter at any value and you may change the counter in anyway you choose.

A different way of using the loop counter

Nested loops

Non-fixed repetitions The for loop is an often used construct to implement fixed repetitions. Sometimes, however, a repetition is required that is not fixed: a racing game that repeatedly moves a car around a track until the car crashes; a ticket issuing program that repeatedly offers tickets for sale until the user chooses to quit the program; a password checking program that does not let a user into an application until he or she enters the right password. The while loop offers one type of non-fixed iteration.

The while loop The syntax for constructing this loop in Java is as follows: As this loop is not repeating a fixed number of times, there is no need to create a counter to keep track of the number of repetitions.

Input validation checking input data for errors is referred to as input validation; example: asking the user to enter an exam mark (as a percentage); the mark should never be greater than 100 or less than 0

Using the while loop

The do while loop the do while loop is another variable loop construct unlike the while loop, the do while loop has its test at the end of the loop rather than at the beginning. the syntax of a do while loop is given below:

Implications of having the test at the end of the loop if the test is at the end of the loop, the loop will iterate at least once; if the test is at the beginning of the loop, however, there is a possibility that the condition will be false to begin with, and the loop is never executed; a while loop therefore executes zero or more times; a do...while loop executes one or more times.

Using the do while loop SO FAR: programs you have written stop as soon as they have done their job; BETTER SOLUTION: put whole program in a loop that keeps repeating until the user chooses to quit your program; involves asking the user each time if he or she would like to continue repeating your program, or to stop; a while loop would be difficult to use, as the test that checks the user's response to a question cannot be carried out at the beginning of the loop; the answer is to move the test to the end of the loop and use a do while loop.

An example program

Sample run of program 3.4 *** Product Price Check *** Enter initial price: 50 Enter tax rate: 10 Cost after tax = 55.0 Would you like to enter another product (y/n)?: y *** Product Price Check *** Enter initial price: 70 Enter tax rate: 5 Cost after tax = 73.5

Sample run of program 3.4 continued Would you like to enter another product (y/n)?: Y *** Product Price Check *** Enter initial price: 200 Enter tax rate: 15 Cost after tax = 230.0 Would you like to enter another product (y/n)?: n

Menu driven programs another way to allow a program to be run repeatedly using a do...while loop is to include a menu of options within the loop the options themselves are processed by a switch statement. one of the options in the menu list would be the option to quit and this option is checked in the while condition of the loop.

A complete menu driven program

Sample run of program 3.5 ***Lab Times*** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 2 1.00 p.m [1] TIME FOR GROUP A [2] TIME FOR GROUP B

Sample run of program 3.5 continued [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 5 Options 1-4 only! [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 1 10.00 a.m

Sample run of program 3.5 continued [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 3 11.00 a.m [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 4 Goodbye

Picking the right loop if the number of repetitions required can be determined prior to entering the loop - use a for loop; if the number of repetitions required cannot be determined prior to entering the loop, and you wish to allow for the possibility of zero repetitions - use a while loop; if the number of repetitions required cannot be determined before the loop, and you require at least one repetition of the loop - use a do...while loop.