Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.

Slides:



Advertisements
Similar presentations
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
CPS120 Introduction to Computer Science Iteration (Looping)
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Loops and Iteration for Statements, while Statements and do-while Statements.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Controlling Execution Dong Shao, Nanjing Unviersity.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
CPS120 Introduction to Computer Science Iteration (Looping)
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 3 Flow of Control
Chapter 3 Flow of Control
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Loops.
JavaScript: Control Statements.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
MSIS 655 Advanced Business Applications Programming
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Lab5 PROGRAMMING 1 Loop chapter4.
Chapter 3 Flow of Control
Chapter 3 Debugging Section 3.4
Chapter 4 Constructors Section 4.4
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CSC215 Lecture Control Flow.
Controlling Program Flow
Chapter 3 Flow of Control Loops in Java.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Loops Loops in Java are similar to those in other high-level languages Java has three types of loop statements: the while, the do-while, and the for statements The code that is repeated in a loop is called the body of the loop Each repetition of the loop body is called an iteration of the loop Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. while statement A while statement is used to repeat a portion of code (i.e., the loop body) based on the evaluation of a Boolean expression The Boolean expression is checked before the loop body is executed When false, the loop body is not executed at all Before the execution of each following iteration of the loop body, the Boolean expression is checked again If true, the loop body is executed again If false, the loop statement ends The loop body can consist of a single statement, or multiple statements enclosed in a pair of braces ({ }) Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. while Syntax while (Boolean_Expression) Statement Or while (Boolean_Expression){ Statement_1 Statement_2 Statement_Last } . . . Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. while statement Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. do-while Statement A do-while statement is used to execute a portion of code (i.e., the loop body), and then repeat it based on the evaluation of a Boolean expression The loop body is executed at least once The Boolean expression is checked after the loop body is executed The Boolean expression is checked after each iteration of the loop body If true, the loop body is executed again If false, the loop statement ends Don't forget to put a semicolon after the Boolean expression Like the while statement, the loop body can consist of a single statement, or multiple statements enclosed in a pair of braces ({ }) Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. do-while Syntax do Statement while (Boolean_Expression); Or do { Statement_1 Statement_2 Statement_Last } while (Boolean_Expression); . . . Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Algorithms and Pseudocode The hard part of solving a problem with a computer program is not dealing with the syntax rules of a programming language Rather, coming up with the underlying solution method is the most difficult part An algorithm is a set of precise instructions that lead to a solution An algorithm is normally written in pseudocode, which is a mixture of programming language and a human language, like English Pseudocode must be precise and clear enough so that a good programmer can convert it to syntactically correct code However, pseudocode is much less rigid than code: One needn't worry about the fine points of syntax or declaring variables, for example Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The for Statement The for statement is most commonly used to step through an integer variable in equal increments It begins with the keyword for, followed by three expressions in parentheses that describe what to do with one or more controlling variables The first expression tells how the control variable or variables are initialized or declared and initialized before the first iteration The second expression determines when the loop should end, based on the evaluation of a Boolean expression before each iteration The third expression tells how the control variable or variables are updated after each iteration of the loop body Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The for Statement Syntax for (Initializing; Boolean_Expression; Update) Body The Body may consist of a single statement or a list of statements enclosed in a pair of braces ({ }) Note that the three control expressions are separated by two, not three, semicolons Note that there is no semicolon after the closing parenthesis at the beginning of the loop Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Semantics of the for Statement Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

for Statement Syntax and Alternate Semantics Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The for Statement Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

for Statement Syntax and Alternate Semantics Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The Comma in for Statements A for loop can contain multiple initialization actions separated with commas Caution must be used when combining a declaration with multiple actions It is illegal to combine multiple type declarations with multiple actions, for example To avoid possible problems, it is best to declare all variables outside the for statement A for loop can contain multiple update actions, separated with commas, also It is even possible to eliminate the loop body in this way However, a for loop can contain only one Boolean expression to test for ending the loop Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Infinite Loops A while, do-while, or for loop should be designed so that the value tested in the Boolean expression is changed in a way that eventually makes it false, and terminates the loop If the Boolean expression remains true, then the loop will run forever, resulting in an infinite loop Loops that check for equality or inequality (== or !=) are especially prone to this error and should be avoided if possible Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Nested Loops Loops can be nested, just like other Java structures When nested, the inner loop iterates from beginning to end for each single iteration of the outer loop int rowNum, columnNum; for (rowNum = 1; rowNum <=3; rowNum++) { for (columnNum = 1; columnNum <=2; columnNum++) System.out.print(" row " + rowNum + " column " + columnNum); System.out.println(); } row 1 column 1 row 1 column 2 row 2 column 1 row 2 column 2 row 3 column 1 row 3 column 2 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The break and continue Statements The break statement consists of the keyword break followed by a semicolon When executed, the break statement ends the nearest enclosing switch or loop statement The continue statement consists of the keyword continue followed by a semicolon When executed, the continue statement ends the current loop body iteration of the nearest enclosing loop statement Note that in a for loop, the continue statement transfers control to the update expression When loop statements are nested, remember that any break or continue statement applies to the innermost, containing loop statement Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The break Statement Infinite conditional loops NOT recommended as shown above. Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The continue Statement Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The Labeled break Statement There is a type of break statement that, when used in nested loops, can end any containing loop, not just the innermost loop If an enclosing loop statement is labeled with an Identifier, then the following version of the break statement will exit the labeled loop, even if it is not the innermost enclosing loop: break someIdentifier; To label a loop, simply precede it with an Identifier and a colon: someIdentifier: Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

The Labeled break Statement Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The exit Statement A break statement will end a loop or switch statement, but will not end the program The exit statement will immediately end the program as soon as it is invoked: System.exit(0); The exit statement takes one integer argument By tradition, a zero argument is used to indicate a normal ending of the program Copyright © 2012 Pearson Addison-Wesley. All rights reserved.