© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.

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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Comparing Data Comparing More than Numbers. Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of.
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.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
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.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
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.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
Chapter 3: Program Statements
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
Chapter 5 Loops.
© 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.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
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)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
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.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CprE 185: Intro to Problem Solving (using C)
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Logical Operators & Truth Tables.
Outline Altering flow of control Boolean expressions
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)
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
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)
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved Midterm Stats Mean 88 Stdev 18.6 Min 25 Max people took the test Only 23 have scores < 70

© 2004 Pearson Addison-Wesley. All rights reserved Best Scores Aaron Logan127 Andrew Miller121 Thomas Noonan120.5 Devin Whiteley120

© 2004 Pearson Addison-Wesley. All rights reserved Quick review of last lecture

© 2004 Pearson Addison-Wesley. All rights reserved Comparing Float Values To determine the equality of two floats, you may want to use the following technique: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ("Essentially equal"); If the difference between the two floating point values is less than the tolerance, they are considered to be equal The tolerance could be set to any appropriate level, such as

© 2004 Pearson Addison-Wesley. All rights reserved Comparing Characters In Unicode, the digit characters (0-9) are contiguous and in order Likewise, the uppercase letters (A-Z) and lowercase letters (a-z) are contiguous and in order CharactersUnicode Values 0 – 948 through 57 A – Z65 through 90 a – z97 through 122

© 2004 Pearson Addison-Wesley. All rights reserved Comparing Strings Remember that in Java a character string is an object The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order The equals method returns a boolean result if (name1.equals(name2)) System.out.println ("Same name");

© 2004 Pearson Addison-Wesley. All rights reserved Comparing Strings We cannot use the relational operators to compare strings The String class contains a method called compareTo to determine if one string comes before another A call to name1.compareTo(name2)  returns zero if name1 and name2 are equal (contain the same characters)  returns a negative value if name1 is less than name2  returns a positive value if name1 is greater than name2

© 2004 Pearson Addison-Wesley. All rights reserved Comparing Strings if (name1.compareTo(name2) < 0) System.out.println (name1 + "comes first"); else if (name1.compareTo(name2) == 0) System.out.println ("Same name"); else System.out.println (name2 + "comes first"); Because comparing characters and strings is based on a character set, it is called a lexicographic ordering

© 2004 Pearson Addison-Wesley. All rights reserved Lexicographic Ordering Lexicographic ordering is not strictly alphabetical when uppercase and lowercase characters are mixed For example, the string "Great" comes before the string "fantastic" because all of the uppercase letters come before all of the lowercase letters in Unicode Also, short strings come before longer strings with the same prefix (lexicographically) Therefore "book" comes before "bookcase"

© 2004 Pearson Addison-Wesley. All rights reserved Logic of an if statement condition evaluated statement true false

© 2004 Pearson Addison-Wesley. All rights reserved Logic of a while Loop statement true false condition evaluated

© 2004 Pearson Addison-Wesley. All rights reserved The while Statement A while statement has the following syntax: while ( condition ) statement; If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false

© 2004 Pearson Addison-Wesley. All rights reserved The while Statement An example of a while statement: int count = 1; while (count <= 5) { System.out.println (count); count++; } If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times

© 2004 Pearson Addison-Wesley. All rights reserved Other Stuff from Section 5.5

© 2004 Pearson Addison-Wesley. All rights reserved Infinite Loops The body of a while loop eventually must make the condition false If not, it is called an infinite loop, which will execute until the user interrupts the program This is a common logical error You should always double check the logic of a program to ensure that your loops will terminate normally

© 2004 Pearson Addison-Wesley. All rights reserved Infinite Loops An example of an infinite loop: int count = 1; while (count <= 25) { System.out.println (count); count = count - 1; } This loop will continue executing until interrupted (Control-C) or until an underflow error occurs

© 2004 Pearson Addison-Wesley. All rights reserved Nested Loops How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { System.out.println ("Here"); count2++; } count1++; } 10 * 20 = 200

© 2004 Pearson Addison-Wesley. All rights reserved Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely See PalindromeTester.java (page 235)PalindromeTester.java

© 2004 Pearson Addison-Wesley. All rights reserved Example: PalindromeTester.java (page 235)PalindromeTester.java

Chapter 5 Sections 5.7 & 5.8

© 2004 Pearson Addison-Wesley. All rights reserved The do Statement A do statement has the following syntax: do { statement; } while ( condition ) The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false

© 2004 Pearson Addison-Wesley. All rights reserved Logic of a do Loop true condition evaluated statement false

© 2004 Pearson Addison-Wesley. All rights reserved Comparing while and do statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop

© 2004 Pearson Addison-Wesley. All rights reserved The do Statement An example of a do loop: The body of a do loop executes at least once See ReverseNumber.java (page 244)ReverseNumber.java int count = 0; do { count++; System.out.println (count); } while (count < 5);

© 2004 Pearson Addison-Wesley. All rights reserved Example: ReverseNumber.java (page 244)ReverseNumber.java

© 2004 Pearson Addison-Wesley. All rights reserved The for Statement A for statement has the following syntax: for ( initialization ; condition ; increment ) statement; The initialization 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

© 2004 Pearson Addison-Wesley. All rights reserved Logic of a for loop statement true condition evaluated false increment initialization

© 2004 Pearson Addison-Wesley. All rights reserved The for Statement A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

© 2004 Pearson Addison-Wesley. All rights reserved The for Statement An example of a for loop: for (int count=1; count <= 5; count++) System.out.println (count); The initialization section can be used to declare a variable Like a while loop, the condition of a for loop is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times

© 2004 Pearson Addison-Wesley. All rights reserved The for Statement The increment section can perform any calculation A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance See Multiples.java (page 248)Multiples.java See Stars.java (page 250)Stars.java for (int num=100; num > 0; num -= 5) System.out.println (num);

© 2004 Pearson Addison-Wesley. All rights reserved Example: Multiples.java (page 248)Multiples.java

© 2004 Pearson Addison-Wesley. All rights reserved Example: Stars.java (page 250)Stars.java

© 2004 Pearson Addison-Wesley. All rights reserved The for Statement Each expression in the header of a for loop is optional 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 increment is left out, no increment operation is performed

© 2004 Pearson Addison-Wesley. All rights reserved THE END