SLIDE 1
Darrell Bethea May 19, 2011
1
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
3
SLIDE 4 Loops
- Body
- Initializing Statements
- Ending a loop
- Bugs
4
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
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
Get user input sum = sum + input
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 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?
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
for (count = 0; count < iterations; count++) { System.out.print(“I have iterated”); System.out.println((count + 1) + “ times”); }
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 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Give a Java loop statement that will set the
variable result equal to 25.
15
SLIDE 54 Problem with program preventing correct
execution
Two most common mistake in loops
- Ofg-by-one errors
- Infinite Loops!!!!!!
16
SLIDE 55 Loop repeats one too many or one too few
times
for (count = 1; count < 10; count++);
17
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
count = 1; while (count <= num) { System.out.print(count + “, “); //count++; }
19
SLIDE 58
count = 1; while (count <= num); { System.out.print(count + “, “); count++; }
20
SLIDE 59
int count;
// initializing action; boolean expression; update action
for (count = 1; count >= num; count++) { System.out.print(count + “, “); }
21
SLIDE 60 Error checking
- System.out.print(variable);
- Run on simple input
Debugger
22