Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.

Similar presentations


Presentation on theme: "Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements."— Presentation transcript:

1 Repetition Statements

2  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements

3 While statements  while statements have the following syntax:  while ( )  The statements execute 0 or more times based on the boolean expression  If the expression is true, the statements will be executed and then the expression will be tested again.  When the expression evaluates to false, the loop is exited

4 While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1;

5 While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False

6 While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False

7 While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False sum += number; number++;

8 While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False sum += number; number++;

9 While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } int sum = 0, number = 1; number <= 100 True False sum += number; number++;

10 While statements  int sum = 0, number = 1;  while (number <= 100){ sum += number; number++; } condition True False Statements

11 While statements  Questions??

12 While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”);

13 While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”);

14 While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); False

15 While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); False x = in.getInteger(“Enter a positive number”); True

16 While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”); x < 0 InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); False x = in.getInteger(“Enter a positive number”); True

17 while statements Write a method that adds the numbers x through y

18 do while statements  do while loops are a variation of the while loop  Syntax: do { } while ( );  This is the same as a while loop, except the statements are always executed at least once.

19 do while statements while (<boolean expression) do{ } while ( ); condition True False Statements condition False Statements True

20 While statements  InputBox in = new InputBox(mw); int x = in.getInteger(“Enter a positive number”); while (x < 0) x = in.getInteger(“Enter a positive number”);  int x; InputBox in = new InputBox(mw); do{ x = in.getInteger(“Enter a positive number”); } while (x < 0); while loop do while loop

21 While statements  Questions?

22 for statements  for statements have four parts  Initalization – executed once (initially)  Condition – boolean expression  Statements – executed based on condition  Increment – executed after statements and before condition is reevaluated  They have the following syntax:  for( [ initialization ]; [ condition ]; [increment] )  Example:  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i;

23 for statements for( [ initialization ]; [ condition ]; [increment] ) initalization condition false statements true increment

24 for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i;

25 for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0;

26 for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0;

27 for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0; i < 100 true false

28 for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0; i < 100 true false sum = sum + i;

29 for statements  sum = 0;  for( int i = 0; i < 100; i++ ) sum = sum + i; sum = 0; int i = 0; i < 100 true false sum = sum + i; i++;

30 for statements Questions????

31 Nested for statements  Nested for statements are just like regular for statements  They are very useful for many processing tasks  Multiplication Table Example:  OutputBox out = new OutputBox(mw);  out.show();  for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); }

32 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); }

33 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1

34 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1

35 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 1

36 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 1

37 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 1 1 _

38 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 2 1 _

39 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 2 1 _

40 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 2 1 2 _

41 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 3 1 2 _

42 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 3 1 2 _

43 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 3 1 2 3 _

44 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 9 1 2 3 4 5 6 7 8 _

45 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 9 1 2 3 4 5 6 7 8 _

46 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 9 1 2 3 4 5 6 7 8 9 _

47 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 10 1 2 3 4 5 6 7 8 9 _

48 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 j 10 1 2 3 4 5 6 7 8 9 _

49 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 1 1 2 3 4 5 6 7 8 9 _

50 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 1 2 3 4 5 6 7 8 9 _

51 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 1 1 2 3 4 5 6 7 8 9 _

52 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 1 1 2 3 4 5 6 7 8 9 _

53 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 1 1 2 3 4 5 6 7 8 9 2 _

54 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 2 1 2 3 4 5 6 7 8 9 2 _

55 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 2 1 2 3 4 5 6 7 8 9 2 _

56 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 2 1 2 3 4 5 6 7 8 9 2 4 _

57 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 3 1 2 3 4 5 6 7 8 9 2 4 _

58 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 3 1 2 3 4 5 6 7 8 9 2 4_

59 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 3 1 2 3 4 5 6 7 8 9 2 4 6_

60 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 9 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 _

61 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 9 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 _

62 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 9 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 _

63 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 10 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 _

64 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 10 0 1 2 3 4 5 6 7 8 9 _

65 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 2 j 10 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 _

66 Nested for statements OutputBox out = new OutputBox(mw); out.show(); for(int i = 1; i < 10; i++){ for(int j = 1; j < 10; j++){ out.print( (i * j) + “ “); } out.skipLine(1); } i 10 j 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 At the end, you get this -------------->

67 Nested for statements Questions???

68 Loops  There are two ways loops can be used  Sentinel controlled loops  Loop is exited based on a sentinel value  while (x != 0) // do something  Count controlled  Loop is exited when a certain count is reached  for (x = 0; x < 10; x++) // do something

69 Pitfalls  Infinite loops  Make sure loops can exit!  Real number addition 1.0/3.0 + 1.0/ 3.0 + 1.0/3.0 =? 1.0  for( x = 0; x != 1.0; x+=.33333333)  In general avoid using real numbers as counter variables  Use, >= and not !=  for(x = 0; x < 3; x++);

70 Pitfalls  Off by one errors  for(int i = 0; i <= 10; i++)  // this will execute 11 times  for(int i=1; i < 10; i++)  // this will execute 9 times  for(int i = 1; i <= 10; i++)  // this will execute 10 times  for(int i =0; I < 10; i++)  // this will execute 10 times

71 Pitfalls  Off by one errors  for(int i = 0; i <= 10; i++)  // this will execute 11 times  for(int i=1; i < 10; i++)  // this will execute 9 times  for(int i = 1; i <= 10; i++)  // this will execute 10 times  for(int i =0; I < 10; i++)  // this will execute 10 times Use zero based indexing

72 Pitfalls  Make sure the loop body contains a statement that will eventually cause the loop to terminate  Make sure the loop repeats exactly the correct number of times  If you want to execute the loop body N times, then go 0 to < n or 1 to <= n


Download ppt "Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements."

Similar presentations


Ads by Google