© 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.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Advertisements

1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
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.
Chapter 3: Program Statements Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, All rights.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Chapter Day 14. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 14 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 11.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering
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.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
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.
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 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
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.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science.
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
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.
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.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 4 Repetition Statements (loops)
Chapter 3: Program Statements
Topics Introduction to Repetition Structures
Loop Structures.
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.
Chapter 3: Program Statements
Outline Altering flow of control Boolean expressions
Chapter 3: Program Statements
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)
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Chapter 3: Program Statements
Chap 7. Advanced Control Statements in Java
Chapter 3: Program Statements
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:

© 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 increment and decrement operators the assignment operators

© 2006 Pearson Education 2 Increment and Decrement  The increment and decrement operators are arithmetic and operate on one operand  The increment operator ( ++ ) adds one to its operand  The decrement operator ( -- ) subtracts one from its operand  The statement count++; is functionally equivalent to count = count + 1;

© 2006 Pearson Education 3 Assignment Operators  Often we perform an operation on a variable, and then store the result back into that variable  Java provides assignment operators to simplify that process  For example, the statement num += count; is equivalent to num = num + count;

© 2006 Pearson Education 4 Assignment Operators  There are many assignment operators, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y

© 2006 Pearson Education 5 Assignment Operators  The right hand side of an assignment operator can be a complex expression  The entire right-hand expression is evaluated first, then the result is combined with the original variable  Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num);

© 2006 Pearson Education 6 Assignment Operators  The behavior of some assignment operators depends on the types of the operands  If the operands to the += operator are strings, the assignment operator performs string concatenation  The behavior of an assignment operator ( += ) is always consistent with the behavior of the "regular" operator ( + )

© 2006 Pearson Education 7 Repetition Statements  Repetition statements allow us to execute a statement multiple times  Often they are referred to as loops  Like conditional statements, they are controlled by boolean expressions  The text covers two kinds of repetition statements: the while loop the for loop  The programmer should choose the right kind of loop for the situation

© 2006 Pearson Education 8 The while Statement  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 repeatedly until the condition becomes false.

© 2006 Pearson Education 9 Logic of a while Loop statement true condition evaluated false

© 2006 Pearson Education 10 The while Statement  Note that if the condition of a while statement is false initially, the statement is never executed  Therefore, the body of a while loop will execute zero or more times  See Counter.java (page 147)Counter.java  See Average.java (page 148)Average.java A sentinel value indicates the end of the input The variable sum maintains a running sum  See WinPercentage.java (page 151)WinPercentage.java A loop is used to validate the input, making the program more robust

© 2006 Pearson Education 11 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 interrupts the program  This is a common logical error  You should always double check to ensure that your loops will terminate normally  See Forever.java (page 152)Forever.java

© 2006 Pearson Education 12 Nested Loops  Similar to nested if statements, loops can be nested as well  That is, the body of a loop can contain another loop  Each time through the outer loop, the inner loop goes through its full set of iterations  See PalindromeTester.java (page 155)PalindromeTester.java

© 2006 Pearson Education 13 Iterators  An iterator is an object that has methods that allow you to process a collection of items one at a time  The hasNext and next methods are used to loop through the collection  Several classes in the Java class library define iterator objects, including Scanner  See URLDissector.java (page 158)URLDissector.java while (myCollection.hasNext()) { System.out.println(myCollection.next()); }

© 2006 Pearson Education 14 The for Statement  The for statement has the following syntax: for ( initialization ; condition ; increment ) statement; Reserved word 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 The condition-statement-increment cycle is executed repeatedly

© 2006 Pearson Education 15 The for Statement  A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

© 2006 Pearson Education 16 Logic of a for loop statement true condition evaluated false increment initialization

© 2006 Pearson Education 17 The for Statement  Like a while loop, the condition of a for statement is tested prior to executing the loop body  Therefore, the body of a for loop will execute zero or more times  It is well suited for executing a loop a specific number of times that can be determined in advance  See Counter2.java (page 161)Counter2.java  See Multiples.java (page 163)Multiples.java  See Stars.java (page 165)Stars.java

© 2006 Pearson Education 18 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  Both semi-colons are always required in the for loop header

© 2006 Pearson Education 19 Iterators and for Loops  A variation of the for loop, called the foreach loop, allows us to process collections just like iterators, but without the complicated syntax  If bookList is an iterator object that manages Book objects, we can do the following:  See IceCreamShop.java (page 167)IceCreamShop.java for (Book myBook : bookList) { System.out.println(myBook); }

© 2006 Pearson Education 20 Choosing a Loop Structure  When you can’t determine how many times you want to execute the loop body, use a while statement  If you can determine how many times you want to execute the loop body, use a for statement

© 2006 Pearson Education 21 Program Development  We now have several additional statements and operators at our disposal  Following proper development steps is important  Suppose you were given some initial requirements: accept a series of test scores compute the average test score determine the highest and lowest test scores display the average, highest, and lowest test scores

© 2006 Pearson Education 22 Program Development  Requirements Analysis – clarify and flesh out specific requirements How much data will there be? How should data be accepted? Is there a specific output format required?  After conferring with the client, we determine: the program must process an arbitrary number of test scores the program should accept input interactively the average should be presented to two decimal places  The process of requirements analysis may take a long time

© 2006 Pearson Education 23 Program Development  Design – determine a possible general solution Input strategy? (Sentinel value?) Calculations needed?  An initial algorithm might be expressed in pseudocode  Multiple versions of the solution might be needed to refine it  Alternatives to the solution should be carefully considered

© 2006 Pearson Education 24 Program Development  Implementation – translate the design into source code  Make sure to follow coding and style guidelines  Implementation should be integrated with compiling and testing your solution  This process mirrors a more complex development model we'll eventually need to develop more complex software  The result is a final implementation  See ExamGrades.java (page 170)ExamGrades.java

© 2006 Pearson Education 25 Program Development  Testing – attempt to find errors that may exist in your programmed solution  Compare your code to the design and resolve any discrepancies  Determine test cases that will stress the limits and boundaries of your solution  Carefully retest after finding and fixing an error

© 2006 Pearson Education 26 More Drawing Techniques  Conditionals and loops can greatly enhance our ability to control graphics  See Bullseye.java (page 173)Bullseye.java  See Boxes.java (page 175)Boxes.java  See BarHeights.java (page 177)BarHeights.java

© 2006 Pearson Education 27 Summary  Chapter 3 has focused on: program development stages the flow of control through a method decision-making statements expressions for making complex decisions repetition statements drawing with conditionals and loops