Chapter 4: Control structures

Similar documents
Chapter 4: Control structures. Repetition

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

Repetition and Loop Statements Chapter 5

Control Structures II. Repetition (Loops)

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

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

REPETITION CONTROL STRUCTURE LOGO

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

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

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:

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

Simple Java Programming Constructs 4

Quiz Determine the output of the following program:

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

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

Repetition, Looping. While Loop

CS 106 Introduction to Computer Science I

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

Chapter Goals. Contents LOOPS

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

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

Dept. of CSE, IIT KGP

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

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

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

CS302: Self Check Quiz 2

CompSci 125 Lecture 11

Loops. Repeat after me

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

204111: Computer and Programming

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

Loops / Repetition Statements

Building Java Programs

Building Java Programs

ECE 122. Engineering Problem Solving with Java

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

Lecture 10. Daily Puzzle

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

Iteration statements - Loops

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

Top-Down Program Development

Example: Monte Carlo Simulation 1

Loops (while and for)

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

Controls Structure for Repetition

Review for Test 1 (Chapter 1-5)

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

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

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

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

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

Building Java Programs Chapter 2

Looping. Arizona State University 1

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

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

Programming Lecture 4

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

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

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

Recap: Assignment as an Operator CS 112 Introduction to Programming

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

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

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

Why Is Repetition Needed?

Structured Program Development

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

Building Java Programs Chapter 2

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

1 Short Answer (10 Points Each)

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

CS 112 Introduction to Programming

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

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)

Conditionals and Loops

CST112 Looping Statements Page 1

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. sae e s Nest a loop repetition statement inside another repetition statement. Choose the appropriate repetition control statement for a given task Page 2 1

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 statements are executed as long as number is less than or equal to 100. Page 4 2

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 sum = 0, number = 1 number <= 100? true false sum = sum + number; number = number + 1; Page 6 3

1 More Examples int sum = 0, number = 1; while ( sum <= 1000000 ) { sum = sum + number; number = number + 1; Keeps adding the numbers 1, 2, 3, until the sum becomes larger than 1,000,000. 2 int product = 1, number = 1, count = 20, lastnumber; lastnumber = 2 * count - 1; Computes the product of the first 20 odd integers. while (number <= lastnumber) { product = product * number; number = number + 2; Page 7 Loop Logical Errors Goal: Execute the loop body 10 times. count = 1; 2 1 count = 1; while ( count < 10 ){ while ( count <= 10 ){...... count++; count++; 3 count = 0; while ( count <= 10 ){... count++; 4 count = 0; while ( count < 10 ){... count++; 1 and 3 exhibit off-by-one error. Page 8 4

The do-while Statement int sum = 0, number = 1; do { number++; These statements are executed as long as sum is less than or equal to 1,000,000. while ( sum <= 1000000 ); Page 9 Syntax for the do-while do Statement <loop body> while ( <boolean expression> ) ; do { number++; loop body while ( sum <= 1000000 ); Boolean Expression Page 10 5

Control Flow of do-while int sum = 0, number = 1 number++; sum <= 1000000? true false Page 11 The for Statement int i, sum = 0, number; for (i = 0; i < 20; i++) { number = scanner.nextint( ); These statements are executed for 20 times ( i = 0, 1, 2,, 19). Page 12 6

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(); t() loop body Page 13 Control Flow of for i = 0; false i < 20? true number =... ; i ++; Page 14 7

More for Loop Examples 1 for (int i = 0; i < 100; i += 5) i = 0, 5, 10,, 95 2 for (int j = 2; j < 40; j *= 2) j = 2, 4, 8, 16, 32 3 for (int k = 100; k > 0; k--) ) k = 100, 99, 98, 97,..., 1 Page 15 The Nested-for Statement Nesting a for statement inside another for statement is commonly used technique in programming. g Let s generate the following table using nested-for statement. Page 16 8

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 9