Darrell Bethea May 19, 2011 1 Assignments: Homework 0 grades up - - PowerPoint PPT Presentation

darrell bethea may 19 2011
SMART_READER_LITE
LIVE PREVIEW

Darrell Bethea May 19, 2011 1 Assignments: Homework 0 grades up - - PowerPoint PPT Presentation

Darrell Bethea May 19, 2011 1 Assignments: Homework 0 grades up Program 1 due yesterday Program 2 due Monday Program 3 assigned today Updated yesterdays slides 2 3 Loops Body Initializing Statements


slide-1
SLIDE 1

Darrell Bethea May 19, 2011

1

slide-2
SLIDE 2

 Assignments:

  • Homework 0 grades up
  • Program 1 due yesterday
  • Program 2 due Monday
  • Program 3 assigned today

 Updated yesterday’s slides

2

slide-3
SLIDE 3

3

slide-4
SLIDE 4

 Loops

  • Body
  • Initializing Statements
  • Ending a loop
  • Bugs

4

slide-5
SLIDE 5

count = 1; while (count <= num) { System.out.print(count + “, “); count++; }

 Repeated code  Write pseudocode and turn repeated

statements into loops

5

slide-6
SLIDE 6

 Get user input  sum = sum + input  Get user input  sum = sum + input  Get user input  sum = sum + input  Average sum

6

Repeated statements in pseudocode become your loop

slide-7
SLIDE 7

 Get user input  sum = sum + input

slide-8
SLIDE 8

sum = sum + input

 Variables in your loop must be initialized (set

to a value) before the loop

 What is initialization of sum?

8

slide-9
SLIDE 9

sum = sum + input

 Variables in your loop must be initialized (set

to a value) before the loop

 What is initialization of sum?

8

 What if we wanted the product?

  • sum = sum * input
slide-10
SLIDE 10

9

 If you know number of loop iterations?

 Count-controlled loops  for(count = 0; count < iterations; count++)

 User controlled ending

 Ask-before-iterating  Sentinel value

 Booleans

slide-11
SLIDE 11

for (count = 0; count < iterations; count++) { System.out.print(“I have iterated”); System.out.println((count + 1) + “ times”); }

slide-12
SLIDE 12

do { //do stufg in your code here System.out.print( “Continue? yes/no”); answer = keyboard.next(); } while (answer.equalsIgnoreCase(“yes”));

11

slide-13
SLIDE 13

12

 Signal end of input System.out.print(“enter a negative number to end the loop”); next = keyboard.nextInt(); sum = 0; while ( next >= 0 ) {

  • sum = sum + next;
  • System.out.print(“Enter a number: ”);
  • next = keyboard.nextInt();

}

slide-14
SLIDE 14

int next, sum = 0; boolean numbersLeft = true; Scanner keyboard = new Scanner(System.in); while (numbersLeft) { next = keyboard.nextInt(); if (next < 0) { numbersLeft = false; } else { sum = sum + next; } } System.out.print(“the sum is “ + sum);

13

slide-15
SLIDE 15

13 Output: Value of count: Value of count2:

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-16
SLIDE 16

13 Output: Value of count: Value of count2:

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-17
SLIDE 17

13 Output: Value of count: Value of count2:

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-18
SLIDE 18

13 Output: Value of count: Value of count2:

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-19
SLIDE 19

13 Output: Value of count: Value of count2:

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-20
SLIDE 20

13 Output: Value of count: Value of count2: 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-21
SLIDE 21

13 Output: Value of count: Value of count2: 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-22
SLIDE 22

13 Output: Value of count: Value of count2: 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-23
SLIDE 23

13 Output: Value of count: Value of count2: 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-24
SLIDE 24

13 Output: Value of count: Value of count2: 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-25
SLIDE 25

13 Output: Value of count: Value of count2: 1 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-26
SLIDE 26

13 Output: Value of count: Value of count2: 1 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-27
SLIDE 27

13 Output: Value of count: Value of count2: 2 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-28
SLIDE 28

13 Output: Value of count: Value of count2: 2 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-29
SLIDE 29

13 Output: Value of count: Value of count2: 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-30
SLIDE 30

13 Output: Value of count: Value of count2: 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-31
SLIDE 31

13 Output: Value of count: Value of count2: 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-32
SLIDE 32

13 Output: Value of count: Value of count2: 2 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-33
SLIDE 33

13 Output: Value of count: Value of count2: 2 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-34
SLIDE 34

13 Output: 1 Value of count: Value of count2: 2 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-35
SLIDE 35

13 Output: 1 Value of count: Value of count2: 2 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-36
SLIDE 36

13 Output: 1 Value of count: Value of count2: 2 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-37
SLIDE 37

13 Output: 1 Value of count: Value of count2: 3 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-38
SLIDE 38

13 Output: 1 Value of count: Value of count2: 3 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-39
SLIDE 39

13 Output: 1 Value of count: Value of count2: 3

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-40
SLIDE 40

13 Output: 1 Value of count: Value of count2: 3

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-41
SLIDE 41

13 Output: 1 Value of count: Value of count2: 3

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-42
SLIDE 42

13 Output: 1 Value of count: Value of count2: 3 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-43
SLIDE 43

13 Output: 1 Value of count: Value of count2: 3 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-44
SLIDE 44

13 Output: 1 1 Value of count: Value of count2: 3 1

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-45
SLIDE 45

13 Output: 1 1 Value of count: Value of count2: 3 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-46
SLIDE 46

13 Output: 1 1 Value of count: Value of count2: 3 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-47
SLIDE 47

13 Output: 1 2 1 Value of count: Value of count2: 3 2

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-48
SLIDE 48

13 Output: 1 2 1 Value of count: Value of count2: 3 3

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-49
SLIDE 49

13 Output: 1 2 1 Value of count: Value of count2: 3 3

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-50
SLIDE 50

13 Output: 1 2 1 Value of count: Value of count2: 3 4

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-51
SLIDE 51

13 Output: 1 2 1 Value of count: Value of count2: 3 4

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-52
SLIDE 52

13 Output: 1 2 1 Value of count: Value of count2: 3 4

int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } }

slide-53
SLIDE 53

 Give a Java loop statement that will set the

variable result equal to 25.

15

slide-54
SLIDE 54

 Problem with program preventing correct

execution

 Two most common mistake in loops

  • Ofg-by-one errors
  • Infinite Loops!!!!!!

16

slide-55
SLIDE 55

 Loop repeats one too many or one too few

times

 for (count = 1; count < 10; count++);

  • Loop 9 times

17

slide-56
SLIDE 56

 A loop which repeats without ever ending

is called an infinite loop.

 If the controlling boolean expression

never becomes false, a loop will repeat without ending.

slide-57
SLIDE 57

count = 1; while (count <= num) { System.out.print(count + “, “); //count++; }

19

slide-58
SLIDE 58

count = 1; while (count <= num); { System.out.print(count + “, “); count++; }

20

slide-59
SLIDE 59

int count;

// initializing action; boolean expression; update action

for (count = 1; count >= num; count++) { System.out.print(count + “, “); }

21

slide-60
SLIDE 60

 Error checking

  • System.out.print(variable);
  • Run on simple input

 Debugger

22