Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.

Similar presentations


Presentation on theme: "Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts."— Presentation transcript:

1 Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts for incrementing and accumulating: += add and assign y += 2 y = y+ 2; -= subtract and assign y -=2 y = y-2; *= multiply and assign y*=2 y=y*2 /= divide and assign y/=2 y=y/2 %= remainder and assign y%=2 y=y%2 1 Java Programming, Sixth Edition

2 Increasing and Decreasing a Variable Different ways to increase by 1 int count = 0; count = count + 1; used as an accumulator count +=1; used as an accumulator count++ ; increment operator ++ Different ways to subtract 1 count = count -1 count -= 1 count-- decrement operator --

3 When would you use an accumulator? When you want to average a set of grades. totalGrades += grade; totalDeposit += deposit; sum+= num You would use a while statement that will let the user continually enter numbers until you type a certain value.

4 Categories of loops definite loop: Executes a known number of times. – The for loops are definite loops. – Examples: Print "hello" 10 times. Find all the prime numbers up to an integer n. Print each odd number between 5 and 127. indefinite loop: One where the number of times its body repeats is not known in advance. – Examples: Prompt the user until they type a certain value. Print random numbers until a prime number is printed. Repeat until the user has types "q" to quit.

5 5-5 Repetition Statements Repetition statements or loops allow us to execute a statement multiple times Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: – the while loop – the do loop // not tested on AP exam – the for loop

6 " while " loops A while statement has the following syntax: while (condition) statement; If the condition is true, the statement is executed; then the condition is evaluated again The statement is executed over and over until the condition becomes false statement condition false true

7 © 2004 Pearson Addison- Wesley. All rights reserved 5-7 The while Statement An example of a while statement: int count = 7; while (count < 5){ System.out.println (count); count++; } If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times

8 © 2004 Pearson Addison- Wesley. All rights reserved 5-8 Infinite Loops The body of a while loop eventually must make the condition false If not, it is called an infinite loop, which will execute until the user interrupts the program int count = 1; while (count <= 25){ System.out.println (count); count = count - 1; } An example of an infinite loop:

9 Two types of loops 1. Count Controlled Loop There are three parts to a count-controlled loop: 1) initialize the variable 2) test the variable 3) increment the variable

10 While loop int x = 1; // initialize the variable while(x < 5) // test the variable condition { // begin loop x++; // increment variable System.out.print(x); // print statement is inside loop } // loop ends

11 Trace loop int x = 1; while(x < 5) { x++; // increment x System.out.print(x ); } Starting x = 1 (1 < 5) X Is true? x++ print 1Yes 2 2 2 Yes 3 3 3 Yes 4 4 4 Yes 5 5 5 no stop Print 2345

12 accumulator int m = 2; total = 0; // initialized outside the loop while(m < 6) { total += m; // accumulator total = total + m; m++; // increment control variable } System.out.println(total); // print statement is outside the loop

13 scope int m = 2, total = 0; while(m < 6) { // body of loop total = total+m; m++; } // loop ends System.out.println(total); total needs to be accessed outside the loop Variables need to be declared outside the loop that need to be seen or used outside the loop Any variable created inside { } of method, if statement, for statement, while statement is not visible outside the curly braces. { }

14 Trace int m = 2; total = 0; while(m < 6) { total = total+m; m++; } System.out.println(total); Check condition 2 < 6 m is true? total 2 yes 2 3 yes 5 4 yes 9 5 yes 14 6 no get out of loop total = 14 print 14

15 Decrement int num = 10; while ( num > 0 ) // decrease until no longer > 0 { System.out.println( num); num--; //decrement } // end while loop System.out.println("Loop ended");

16 accumulator variable int b = 5, ans = 0; while(b<11) { b=b+2; ans=ans+b; } System.out.println(ans); 27 m b+2 ans=ans+b 5 5+2 = 7 7+0 = 7 7 7 + 2 = 9 7 + 9=16 9 9 + 2 = 11 16 + 11 = 27 11 stop An accumulator is a variable that keeps a running total. It will add something to itself.

17 accumulator variable int k=3, tot = 0; while(k<11) { tot = tot+k; k++; } System.out.println(tot); 52 k tot+k k++ 3 3 + 0 = 3 4 4 4 + 3 = 7 5 5 5 + 7 = 12 6 6 6 + 12 = 18 7 7 7 + 18 = 25 8 8 8 + 25 = 33 9 9 9 + 33 = 42 10 1010 + 42 = 52 11 11 stop An accumulator is a variable that keeps a running total. It will add something to itself.

18 accumulator variable int z=2, sum = 0; while(z<9) { z++ sum = sum+z } System.out.println(sum); 42 z zz++ sum=sum+z 2 3 3+0 3 4 4 + 3 = 7 4 5 5 + 7 = 12 5 6 6 + 12 = 18 6 7 7 + 18 = 25 7 8 8 + 25 = 33 8 9 9 + 33 = 42 9 stop An accumulator is a variable that keeps a running total. It will add something to itself.

19 Decrement Check condition(5>=0) Start loop num = 5; num is true? print 5 yes 5 4 yes 4 3 yes 3 2 yes 2 1 Yes 1 0 no get out of loop Print 5 4 3 2 1 Loop ended int num = 5; while ( num > 0 ) { System.out.print( num); num--;} // end System.out.println("Loop ended");

20 Writing while conditions If what you want is to execute the loop 10 times, write the condition number < 10 and not as number <= 9 In Java generally you would more likely want to loop not from 1 to 10, but from 0 to 9. All counting in Java tends to start at zero rather than one. This is a convention that most Java programmers adopt.

21

22 Trace int b=2, sum=0; while(b<9) { b++; sum=sum+b; } System.out.print(sum); b=sum=

23 Nested Loop If you have a nested loop, it passes through the first loop and will continue to execute the second loop until finished, then goes back to the first loop int x = 6; int y, q; while (x < 10) { y = 1; while (y <= 10) { y++; q = x + y; } x += y; } System.out.println (q); x=Outer loop condition y=Inner loop condition y<=10 y++q=x+yx+=y;


Download ppt "Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts."

Similar presentations


Ads by Google