Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computers and Programming Lecture 7:

Similar presentations


Presentation on theme: "Introduction to Computers and Programming Lecture 7:"— Presentation transcript:

1

2 Introduction to Computers and Programming Lecture 7:

3 Assignment Operators Given the following: x = 2; x = x + 1; System.out.println ("x: " + x); There are actually several ways to rewrite this more concisely.

4 Short Cut Operator One option is to use the += operator x = 2; x += 1; // same as x = x + 1; System.out.println ("x: " + x); There are similar operators for *, -, /.% –x = x * 5 is equivalent to x *= 5; –x = x – 5;is equivalent to x -= 5; –x = x / 5;is equivalent to x /= 5; –x = x % 5;is equivalent to x %= 5; Good Practice: place a space before and after your short cut operators.

5 Increment Operator A second option is to use an increment operator: x++Post-Increment Operator ++xPre-Increment Operator Both operators will increment x by 1, but they do have subtle differences.

6 Pre v. Post Increment PostIncrement Operator (x++): –use the current value of x in the expression. Then, increment by 1. PreIncrement Operator (++x): –Increment x by 1. Then, use the new value of x in the expression.

7 How about a real example? // Preincrementing v. PostIncrementing public class PrePost { public static void main (String[] args) { int c = 5; System.out.println (c); System.out.println (c++); System.out.println (c); System.out.println(); c = 5; System.out.println (c); System.out.println (++c); System.out.println (c); } Output: 5 6 5 6 Post Increment Pre Increment  2000 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth

8 Pre v. Post Decrement PostDecrement Operator (x--): –use the current value of x in the expression. Then, decrease by 1. PreDecrement Operator (--x): –Decrease x by 1. Then, use the new value of x in the expression. Good practice: Place unary operators directly next to their operands, with no intervening spaces.

9 Introduction to While Loops

10 While Loops While Loop: Keep repeating an action while some condition remains true. Examples: –Every Stairmaster Machine contains a while loop (end condition is based on mode used). while the person is still climbing, keep displaying the status, e.g. number of stairs climbed, calories burned, etc. –Keep prompting for book orders until the user is done.

11 while loop (continued) For example (in pseudocode) while (some Boolean expression is true) { do this (again and again...) }

12 Parts of a While Loop Every while loop will always contain three main elements: 1)Priming: initialize your variables. 2)Testing: test against some known condition. 3)Updating: updates part (or all) of the expression that is tested.

13 Simple While Loop public class While1 { public static void main (String args[]) { int index = 1; while (index <= 10) { System.out.println ("Index: " + index); index++; } Index: 1 Index: 2 Index: 3... Index: 8 Index: 9 Index: 10 1. Priming 2. Test Condition 3. Update: In this case, you can use either the pre or post increment operator.

14 While Loop Flowchart 1. Priming Set index=1 2. Test index <= 10 3. Print value of index Update index++ TRUE FALSE

15 Infinite Loop Infinite Loop: A loop that never ends. –Generally, you want to avoid these! –There are special cases, however, when you do want to create infinite loops on purpose. Common Exam Questions: –Given a piece of code, identify the bug in the code. –You may need to identify infinite loops.

16 Infinite Loop Example #1 public class While2 { public static void main (String args[]) { int index = 1; while (index <= 10) { System.out.println ("Index: " + index); } Here, I have deleted part 3: The update statement (index++). Index: 1 … [forever]

17 Infinite Loop, Example #2 public class While3 { public static void main (String args[]) { int index = 1; while (index >= 0) { System.out.println ("Index: " + index); index++; } Here, I have changed Part 2: the test condition. Index: 1 Index: 2 Index: 3 Index: 4 Index: 5 … [forever]

18 While Loops: Examples

19 While Loop Example Specification for the program: –Find the first power of 2 larger than 1000. For example: 2, 4, 8, 16, 32, etc. are powers of 2. –Which is the first power of 2 larger than 1000? –Finding the answer to this requires some kind of a while loop. –Let’s see how…  2000 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth

20 While Loop Example Example: int product = 2; while ( product <= 1000 ) product = 2 * product; product <= 1000 product = 2 * product true false  2000 Prentice Hall, Inc. All rights reserved.

21 public class PowerOfTwoOver1000 { public static void main (String args[]) { int product = 2; while ( product <= 1000 ) product = 2 * product; System.out.println (product); }  2000 Prentice Hall, Inc. All rights reserved.

22 Counter-Controlled Repetition Counter-controlled repetition –Loop repeated until counter reaches a certain value –Definite repetition: number of repetitions is known –Example: A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz  2000 Prentice Hall, Inc. All rights reserved.

23 4.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) Counter –Variable that controls number of times set of statements executes Average1.java calculates grade averages –uses counters to control repetition  2003 Prentice Hall, Inc. All rights reserved.

24 Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Fig. 4.6 Pseudocode algorithm that uses counter- controlled repetition to solve the class-average problem.  2003 Prentice Hall, Inc. All rights reserved.

25 1 // Fig. 4.7: Average1.java 2 // Class-average program with counter-controlled repetition. 3 import javax.swing.JOptionPane; 4 5 public class Average1 { 6 7 public static void main( String args[] ) 8 { 9 int total; // sum of grades input by user 10 int gradeCounter; // number of grade to be entered next 11 int grade; // grade value 12 int average; // average of grades 13 14 String gradeString; // grade typed by user 15 16 // initialization phase 17 total = 0; // initialize total 18 gradeCounter = 1; // initialize loop counter 19 20 // processing phase 21 while ( gradeCounter <= 10 ) { // loop 10 times 22 23 // prompt for input and read grade from user 24 gradeString = JOptionPane.showInputDialog( 25 "Enter integer grade: " ); 26 27 // convert gradeString to int 28 grade = Integer.parseInt( gradeString ); 29  2003 Prentice Hall, Inc. All rights reserved.

26 30 total = total + grade; // add grade to total 31 gradeCounter = gradeCounter + 1; // increment counter 32 33 } // end while 34 35 // termination phase 36 average = total / 10; // integer division 37 38 // display average of exam grades 39 JOptionPane.showMessageDialog( null, "Class average is " + 40 average, Class Average", JOptionPane.INFORMATION_MESSAGE ); 41 42 System.exit( 0 ); // terminate the program 43 44 } // end main 45 46 } // end class Average1  2003 Prentice Hall, Inc. All rights reserved.


Download ppt "Introduction to Computers and Programming Lecture 7:"

Similar presentations


Ads by Google