Presentation is loading. Please wait.

Presentation is loading. Please wait.

Catie Welsh February 9, 2011 1.  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.

Similar presentations


Presentation on theme: "Catie Welsh February 9, 2011 1.  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2."— Presentation transcript:

1 Catie Welsh February 9, 2011 1

2  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2

3 3

4 ◦ Use a switch statement when you have more than 2 conditions on a single variable ◦ Example: Weekdays – if you have a different action to perform for each day of the week, use a switch statement ◦ Use an if-else for all other scenarios:  More than one variable you’re testing (multiple conditions)  Testing for a range of values  Variable is not an int or char ◦ Example: Grades - each grade (A, B, C, D, E) has a range of values that reflect each grade letter 4

5  Loop - part of program that repeats  Body - statements being repeated  Iteration - each repetition of body  Stopping condition 5

6  while ◦ Safest choice ◦ Not always most elegant  do-while ◦ Loop iterates at least ONCE  for ◦ Numeric computation changes by equal amount 6

7 The while Statement  Also called a while loop  A while statement repeats while a controlling boolean expression remains true  The loop body typically contains an action that ultimately causes the controlling boolean expression to become false.

8 Evaluate Boolean Expression Execute Body End loop true false 8

9

10 While Loops  Syntax while (Boolean_Expression) Body_Statement or while (Boolean_Expression) { First_Statement Second_Statement … }

11 The do-while Statement  Also called a do-while loop  Similar to a while statement, except that the loop body is executed at least once  Syntax do Body_Statement while (Boolean_Expression);  Don’t forget the semicolon!

12 Evaluate Boolean Expression Execute Body End loop true false 12 Execute Body

13 13

14  First, the loop body is executed.  Then the boolean expression is checked. ◦ As long as it is true, the loop is executed again. ◦ If it is false, the loop is exited.  Equivalent while statement Statement(s)_S1 while (Boolean_Condition) Statement(s)_S1 Do-while Loops

15 Loop Practice Write a while loop or a do-while loop that will compute the sum of the first n positive odd numbers. For example, if n is 5, you should compute 1 + 3 + 5 + 7 + 9.

16 The for Statement  A for statement executes the body of a loop a fixed number of times.  Also known as a for loop.  Example for (count = 1; count < 3; count++) System.out.println(count);

17  Syntax for (Initialization, Condition, Update) Body_Statement  Body_Statement can be either a simple statement or a compound statement in {}.  Corresponding while statement Initialization while (Condition) Body_Statement_Including_Update For Loops

18 Evaluate Boolean Expression Execute Body End loop true false 18 Execute Initializing Action Execute Update Action

19

20  Possible to declare variables within a for loop int sum = 0; for (int n = 1 ; n <= 10 ; n++) sum = sum + n * n;  Note that variable n is local to the loop

21 Loop Practice Write a for loop that will compute the sum of the first n positive even numbers. For example, if n is 5, you should compute 2 + 4 + 6 + 8 + 10.

22  while ◦ Safest choice ◦ Not always most elegant  do-while ◦ Loop iterates at least ONCE  for ◦ Numeric computation changes by equal amount 22

23  Problem with program preventing correct execution  Two most common mistake in loops ◦ Off-by-one errors ◦ Infinite Loops!!!!!! 23

24 Infinite Loops  A loop which repeats without ever ending is called an infinite loop.  If the controlling boolean expression never becomes false, a while loop or a do-while loop will repeat without ending.

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

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

27 int count; // initializing action; boolean expression; update action for (count = 1; count >= num; count++) { System.out.print(count + “, “); } 27

28  Help with Program 2 28


Download ppt "Catie Welsh February 9, 2011 1.  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2."

Similar presentations


Ads by Google