Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.

Slides:



Advertisements
Similar presentations
1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
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.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Chapter Day 12. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 12 Problem set corrected  1 A, 2 B’s and 1 D Starting Late and not turning.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Chapter 3: Program Statements
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
Chapter 3: Decisions and Loops
Chapter 6 More Conditionals and Loops
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Loop Structures.
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
MSIS 655 Advanced Business Applications Programming
Chapter 6 More Conditionals and Loops
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Chapter 3: Program Statements
The ‘while’ Statement September 27, 2006
Chapter 3: Program Statements
3.5- The while Statement The while statement has the following syntax:
What output is produced by the following fragment?
CprE 185: Intro to Problem Solving (using C)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Outline Boolean Expressions The if Statement Comparing Data
Chap 7. Advanced Control Statements in Java
CSCI 1100/1202 February 6, 2002.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed  by John Lewis and William Loftus to accompany  by John Lewis and William Loftus to accompany D Java Software Solutions: Foundations of Program Design, Second Edition and  by Mark Hutchenreuther for CSC-101 at Cal Poly, SLO.  by Mark Hutchenreuther for CSC-101 at Cal Poly, SLO. D

CSC-101 Repetition Statements: Loops D Repetition statements allow us to execute a statement multiple times repetitively D They are often simply referred to as loops D Like conditional statements, they are controlled by boolean expressions D Java has three kinds of repetition statements: the while loop, the do loop, and the for loop D The programmer must choose the right kind of loop for the situation

CSC-101 Logic of a loop Logic of a while loop statementtrue condition evaluated false

CSC-101 The while Statement D The while statement has the following syntax: while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repetitively until the condition becomes false.

CSC-101 The while Statement  If the condition of a while statement is false initially, the statement is never executed D Thus, the body of a while loop will execute zero or more times D See Counter.java Counter.java D See Average.java Average.java D See WinPercentage.java WinPercentage.java

CSC-101 Logic of a loop Logic of a do loop statementtrue condition evaluated false

CSC-101 The do Statement D The do statement has the following syntax: do { statement; } while ( condition ) Uses both the do and whilereservedwords The statement is executed once initially, then the condition is evaluated The statement is repetitively executed until the condition becomes false

CSC-101 The do Statement D A do loop is similar to a while loop, except that here the condition is evaluated after the body of the loop is executed D Therefore the body of a do loop will execute at least one time D See Counter2.java Counter2.java D See ReverseNumber.java ReverseNumber.java

CSC-101 Comparing the and loops Comparing the while and do loops while loop do loop statementtrue condition evaluated false false statementtrue condition evaluated

CSC-101 Infinite Loops  The body of a while loop must eventually make the condition false D If not, it is an infinite loop, which will execute until the user interrupts the program D These are a very common form of logical error You should double check to ensure that your loops will terminate normally You should double check to ensure that your loops will terminate normally You should not rely on a break; or continue; to exit from a loop: when tempted, find another way out You should not rely on a break; or continue; to exit from a loop: when tempted, find another way out See Forever.java for an example of this error. See Forever.java for an example of this error.Forever.java

CSC-101 Nested Loops D Similar to nested if statements, loops can be nested as well D That is, the body of a loop could contain another loop D Each time through the outer loop, the inner loop will go through its entire set of iterations D See PalindromeTester.java PalindromeTester.java

CSC-101 Logic of a loop Logic of a for loop statementtrue false increment initialization condition evaluated

CSC-101 The for Statement D The for statement has the following syntax: for ( initialization ; condition ; increment ) statement;Reservedword The initialization portion is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration

CSC-101 Comparing the and loops Comparing the while and for loops while loop for loop statementtrue condition evaluated false statementtrue false increment initialization condition evaluated

CSC-101 The for Statement D A for loop is equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

CSC-101 The for Statement D Like a while loop, the condition of a for statement is tested prior to executing the loop body D Therefore, the body of a for loop will execute zero or more times D It is well suited for executing a specific number of times a specific number of times that can be determined in advance that can be determined in advance D See Counter3.java Counter3.java D See Multiples.java Multiples.java D See Stars.java Stars.java

CSC-101 The for Statement D Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the increment is left out, no increment operation is performed If the increment is left out, no increment operation is performed D Both semi-colons are always required in the for loop header