Chapter 4: Control structures. Repetition

Similar documents
Chapter 4: Control structures

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Repetition CSC 121 Fall 2014 Howard Rosenthal

Loops / Repetition Statements

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

Control Structures II. Repetition (Loops)

Repetition and Loop Statements Chapter 5

CS110D: PROGRAMMING LANGUAGE I

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

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Repetition Structures

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

Repetition, Looping CS101

A Look Back at Arithmetic Operators: the Increment and Decrement

REPETITION CONTROL STRUCTURE LOGO

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Structured Programming. Dr. Mohamed Khedr Lecture 9

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction. C provides two styles of flow control:

Quiz Determine the output of the following program:

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

Simple Java Programming Constructs 4

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

STUDENT LESSON A12 Iterations

Repetition, Looping. While Loop

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

CS 106 Introduction to Computer Science I

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Chapter Goals. Contents LOOPS

Dept. of CSE, IIT KGP

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS302: Self Check Quiz 2

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

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

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

CompSci 125 Lecture 11

Loops. Repeat after me

Loops / Repetition Statements

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Building Java Programs

204111: Computer and Programming

ECE 122. Engineering Problem Solving with Java

Building Java Programs

Lecture 10. Daily Puzzle

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Iteration statements - Loops

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

CS111: PROGRAMMING LANGUAGE II

Computer Programming I - Unit 5 Lecture page 1 of 14

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Introduction to the Java Basics: Control Flow Statements

Loops (while and for)

Top-Down Program Development

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Example: Monte Carlo Simulation 1

Review for Test 1 (Chapter 1-5)

Controls Structure for Repetition

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

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Repetition Structures II

Chapter 3 Structured Program Development

Information Science 1

Information Science 1

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Building Java Programs Chapter 2

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Looping. Arizona State University 1

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Programming Lecture 4

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Recap: Assignment as an Operator CS 112 Introduction to Programming

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Why Is Repetition Needed?

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Structured Program Development

1 Short Answer (10 Points Each)

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

CS 112 Introduction to Programming

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

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

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

Conditionals and Loops

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Transcription:

Chapter 4: Control structures Repetition

Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition control in a program using do-while statements. Implement a generic loop-and-a-half repetition control statement Implement repetition control in a program using for statements. Nest a loop repetition statement inside another repetition statement. Choose the appropriate repetition control statement for a given task Page 2

Definition Repetition statements control a block of code to be executed for a fixed number of times or until a certain condition is met. There are three types of repetition: Count-controlled repetitions terminate the execution of the block after it is executed for a fixed number of times. Sentinel-controlled repetitions terminate the execution of the block after one of the designated values called a sentinel is encountered. Flag-controlled repetitions terminate the execution of the block after one of the designated values called a sentinel is encountered. Repetition statements are called loop statements also. Page 3

The while Statement int sum = 0, number = 1; while ( number <= 100 ) { sum = sum + number; number = number + 1; These These statements statements are are executed executed as as long long as as number number is is less less than than or or equal equal to to 100. 100. Page 4

Syntax for the while Statement while ( <boolean expression> ) <loop body> Boolean Expression while ( number <= 100 ) { loop body sum = sum + number; number = number + 1; Page 5

Control Flow of while int int sum sum = 0, 0, number number = 1 number <= <= 100 100? true false sum sum = sum sum + number; number = number + 1; 1; Page 6

More Examples 1 int sum = 0, number = 1; while ( sum <= 1000000 ) { sum = sum + number; number = number + 1; Keeps Keeps adding adding the the numbers numbers 1, 1, 2, 2, 3, 3, until until the the sum sum becomes becomes larger larger than than 1,000,000. 1,000,000. 2 int product = 1, number = 1, count = 20, lastnumber; lastnumber = 2 * count - 1; Computes Computes the the product product of of the the first first 20 20 odd odd integers. integers. while (number <= lastnumber) { product = product * number; number = number + 2; Page 7

Loop Logical Errors 1 Goal: Execute the loop body 10 times. count = 1; while ( count < 10 ){... count++; 2 count = 1; while ( count <= 10 ){... count++; 3 count = 0; while ( count <= 10 ){ 4 count = 0; while ( count < 10 ){...... count++; count++; 1 and 3 exhibit off-by-one error. Page 8

The do-while Statement int sum = 0, number = 1; do { sum += number; number++; These These statements statements are are executed executed as as long long as as sum sum is is less less than than or or equal equal to to 1,000,000. 1,000,000. while ( sum <= 1000000 ); Page 9

Syntax for the do-while do Statement <loop body> while ( <boolean expression> ) ; do { sum += number; number++; loop body while ( sum <= 1000000 ); Boolean Expression Page 10

Control Flow of do-while int int sum sum = 0, 0, number number = 1 sum sum += += number; number++; sum sum <= <= 1000000? true false Page 11

The for Statement int i, sum = 0, number; for (i = 0; i < 20; i++) { number = scanner.nextint( ); sum += number; These These statements statements are are executed executed for for 20 20 times times (( i i = 0, 0, 1, 1, 2, 2,,, 19). 19). Page 12

Syntax for the for Statement for ( <initialization>; <boolean expression>; <increment> ) <loop body> Initialization Boolean Expression Increment // Decrement for ( i = 0 ; i < 20 ; i++ ) { number = scanner.nextint(); sum += number; loop body Page 13

Control Flow of for i i = 0; 0; false i i < 20 20? true number =...;; sum sum += += number; i i ++; ++; Page 14

More for Loop Examples 1 for (int i = 0; i < 100; i += 5) i i = 0, 0, 5, 5, 10, 10,,, 95 95 2 for (int j = 2; j < 40; j *= 2) j j = 2, 2, 4, 4, 8, 8, 16, 16, 32 32 3 for (int k = 100; k > 0; k--) ) k = 100, 99, 99, 98, 98, 97, 97,...,..., 1 Page 15

The Nested-for Statement Nesting a for statement inside another for statement is commonly used technique in programming. Let s generate the following table using nested-for statement. Page 16

Generating the Table int price; for (int width = 11; width <=20, width++){ for (int length = 5, length <=25, length+=5){ OUTER INNER price = width * length * 19; //$19 per sq. ft. System.out.print ( + price); //finished one row; move on to next row System.out.println( ); Page 17