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

Similar documents
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Motivations. Chapter 5: Loops and Iteration. Opening Problem 9/13/18. Introducing while Loops

Chapter 4 Loops. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

CS1150 Principles of Computer Science Loops (Part II)

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Loops. CSE 114, Computer Science 1 Stony Brook University

3chapter C ONTROL S TATEMENTS. Objectives

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

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

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

Repetition Structures

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

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

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

Introduction to the Java Basics: Control Flow Statements

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

Computer Programming I - Unit 5 Lecture page 1 of 14

Chapter 1: Introduction to Computers, Programs, and Java

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

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

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

Repetition, Looping. While Loop

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

Chapter 4: Control structures. Repetition

CS110D: PROGRAMMING LANGUAGE I

Repetition CSC 121 Fall 2014 Howard Rosenthal

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

Lecture 1 Java SE Programming

Chapter 4: Control structures

Repetition with for loops

CONDITIONAL EXECUTION

Controls Structure for Repetition

Repetition, Looping CS101

ECE 122. Engineering Problem Solving with Java

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

Exam 2, Version 2. For the following code, mark True or False for statements 1.8 to 1.10.

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

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

Loops / Repetition Statements

Iteration statements - Loops

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

Lecture 7 Tao Wang 1

Java Coding 3. Over & over again!

CSC 1051 Data Structures and Algorithms I

( &% class MyClass { }

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

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

Zheng-Liang Lu Java Programming 45 / 79

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

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

CSC 1051 Data Structures and Algorithms I

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

In this chapter you will learn:

Example: Monte Carlo Simulation 1

CS112 Lecture: Repetition Statements

CS171:Introduction to Computer Science II

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

AP Programming - Chapter 6 Lecture

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

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

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Introduction. C provides two styles of flow control:

1.3 Conditionals and Loops

204111: Computer and Programming

Control Structures II. Repetition (Loops)

CompSci 125 Lecture 11

CP122 CS I. Iteration

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

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

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

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

Java Basic Programming Constructs

PROGRAMMING FUNDAMENTALS

1.3 Conditionals and Loops. 1.3 Conditionals and Loops. Conditionals and Loops

Control Statements. Musa M. Ameen Computer Engineering Dept.

COMP 111. Introduction to Computer Science and Object-Oriented Programming

REPETITION CONTROL STRUCTURE LOGO

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

University of Palestine. Mid Exam Total Grade: 100

CS112 Lecture: Loops

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

The for Loop. Lesson 11

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Course Outline. Introduction to java

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Flow of Control: Loops

Building Java Programs

CS313D: ADVANCED PROGRAMMING LANGUAGE

1.3 Conditionals and Loops

Loops (while and for)

Accelerating Information Technology Innovation

Transcription:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection Statements Chapter 4 Loops Chapter 5 Methods 19.1-19.3 in Chapter 19 Recursion Chapter 6 Arrays Chapter 23 Algorithm Efficiency and Sorting Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1

To use while, do-while, and for loop statements to control the repetition of statements ( 4.2-4.4). To understand the flow of control in loop statements ( 4.2-4.4). To use Boolean expressions to control loop statements ( 4.2-4.4). To write nested loops ( 4.5). To know the similarities and differences of three types of loops ( 4.6). To implement program control with break and continue ( 4.7). Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2

while (loop-continuation-condition) { // loop-body; Statement(s); int count = 0; while (count < 100) { count++; count = 0; Loop Continuation Condition? false (count < 100)? false true Statement(s) (loop body) true count++; (A) (B) rights reserved. 0-13-222158-6 3

int count = 0; while (count < 2) { count++; Initialize count rights reserved. 0-13-222158-6 4

int count = 0; while (count < 2) { count++; (count < 2) is true rights reserved. 0-13-222158-6 5

int count = 0; while (count < 2) { count++; Print Welcome to Java rights reserved. 0-13-222158-6 6

int count = 0; while (count < 2) { count++; Increase count by 1 count is 1 now rights reserved. 0-13-222158-6 7

int count = 0; while (count < 2) { count++; (count < 2) is still true since count is 1 rights reserved. 0-13-222158-6 8

int count = 0; while (count < 2) { count++; Increase count by 1 count is 2 now rights reserved. 0-13-222158-6 9

int count = 0; while (count < 2) { count++; Print Welcome to Java rights reserved. 0-13-222158-6 10

int count = 0; while (count < 2) { count++; (count < 2) is false since count is 2 now rights reserved. 0-13-222158-6 11

int count = 0; while (count < 2) { count++; The loop exits. Execute the next statement after the loop. rights reserved. 0-13-222158-6 12

The Math subtraction learning tool program generates just one question for each run. You can use a loop to generate questions repeatedly. This example gives a program that generates ten questions and reports the number of the correct answers after a student answers all ten questions. SubtractionTutorLoop Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a value is known as a sentinel value. Write a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. SentinelValue rights reserved. 0-13-222158-6 13

Statement(s) (loop body) do { // Loop body; Statement(s); true Loop Continuation Condition? false while (loop-continuation-condition); rights reserved. 0-13-222158-6 14

for (initial-action; loopcontinuation-condition; action-after-each-iteration){ // loop body; Statement(s); int i; for (i = 0; i < 100; i++) { System.out.println( "Welcome to Java!"); Initial-Action i = 0 Loop Continuation Condition? false (i < 100)? false true Statement(s) (loop body) true System.out.println( "Welcome to Java"); Action-After-Each-Iteration i++ (A) (B) rights reserved. 0-13-222158-6 15

int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); Declare i rights reserved. 0-13-222158-6 16

int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); Execute initializer i is now 0 rights reserved. 0-13-222158-6 17

int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); (i < 2) is true since i is 0 rights reserved. 0-13-222158-6 18

int i; for (i = 0; i < 2; i++) { Print Welcome to Java rights reserved. 0-13-222158-6 19

int i; for (i = 0; i < 2; i++) { Execute adjustment statement i now is 1 rights reserved. 0-13-222158-6 20

int i; for (i = 0; i < 2; i++) { (i < 2) is still true since i is 1 rights reserved. 0-13-222158-6 21

int i; for (i = 0; i < 2; i++) { Print Welcome to Java rights reserved. 0-13-222158-6 22

int i; for (i = 0; i < 2; i++) { Execute adjustment statement i now is 2 rights reserved. 0-13-222158-6 23

int i; for (i = 0; i < 2; i++) { (i < 2) is false since i is 2 rights reserved. 0-13-222158-6 24

Exit the loop. Execute the next int i; statement after the loop for (i = 0; i < 2; i++) { rights reserved. 0-13-222158-6 25

The initial-action in a for loop can be a list of zero or more commaseparated expressions. The action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements. Therefore, the following three for loops are correct. for (int i = 1; i < 100; System.out.println(i++)); for (int i = 0, j = 0; (i + j < 10); i++, j++) { // Do something int i=0; for(;i<100;){ i++; rights reserved. 0-13-222158-6 26

If the loop-continuation-condition in a for loop is omitted, it is implicitly true. Thus the statement given below in (a), which is an infinite loop, is correct. Nevertheless, it is better to use the equivalent loop in (b) to avoid confusion: for ( ; ; ) { // Do something (a) Equivalent while (true) { // Do something (b) while (true) { System.out.println("H"); //cause error unreachable statement rights reserved. 0-13-222158-6 27

Problem: Write a program that sums a series that starts with 0.01 and ends with 1.0. The numbers in the series will increment by 0.01, as follows: 0.01 + 0.02 + 0.03 and so on. TestSum Problem: Write a program that uses nested for loops to print a multiplication table. TestMultiplicationTable rights reserved. 0-13-222158-6 28

The three forms of loop statements, while, do-while, and for, are expressively equivalent; that is, you can write a loop in any of these three forms. For example, a while loop in (a) in the following figure can always be converted into the following for loop in (b): while (loop-continuation-condition) { // Loop body (a) Equivalent for ( ; loop-continuation-condition; ) // Loop body (b) A for loop in (a) in the following figure can generally be converted into the following while loop in (b) for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; (a) Equivalent initial-action; while (loop-continuation-condition) { // Loop body; action-after-each-iteration; (b) rights reserved. 0-13-222158-6 29

Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below: for (int i=0; i<10; i++); { Syntax Error System.out.println("i is " + i); int j=0; for (; j<10; j++); { System.out.println("j is " + j); //j is 10 Logic Error rights reserved. 0-13-222158-6 30

Similarly, the following loop is also wrong: int i=0; while (i < 10);{ Logic Error System.out.println("i is " + i); i++; //infinity loop In the case of the do loop, the following semicolon is needed to end the loop. int i=0; do { System.out.println("i is " + i); i++; while (i<10); Correct rights reserved. 0-13-222158-6 31

break immediately ends the innermost loop that contain it. continue only ends the current iteration. Program control goes to the end of the loop body. Both are used with an if statement. rights reserved. 0-13-222158-6 32

public class TestBreak { /** Main method */ public static void main(string[] args) { int sum = 0; int number = 0; while (number < 20) { number++; sum += number; if (sum >= 100) break; System.out.println("The number is " + number); System.out.println("The sum is " + sum); //The number is 14, // The sum is 105 TestBreak rights reserved. 0-13-222158-6 33

public class TestContinue { /** Main method */ public static void main(string[] args) { int sum = 0; int number = 0; while (number < 5) { number++; if (number == 1 number == 3) continue; sum += number; System.out.println("The sum is " + sum); // The sum is 11 // 2+4+5 ข าม 1,3 TestContinue rights reserved. 0-13-222158-6 34