Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is...
-
Upload
phillip-anderson -
Category
Documents
-
view
218 -
download
0
Transcript of Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is...
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
1Java Programming, Sixth Edition
Increasing and Decreasing a Variable
Different ways to increase by 1
int count = 0;count = count + 1; used as an accumulatorcount +=1; used as an accumulatorcount++ ; increment operator ++
Different ways to subtract 1 count = count -1count -= 1count-- decrement operator --
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.
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
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
"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
conditionfalse
true
© 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
© 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:
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
While loop
int x = 1; // initialize the variablewhile(x < 5) // test the variable condition { // begin loop x++; // increment variable System.out.print(x); // print statement is inside loop } // loop ends
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 1 Yes 2 2 2 Yes 3 33 Yes 4 44 Yes 5 55 no stop
Print 2345
accumulator
int m = 2; total = 0; // initialized outside the loopwhile(m < 6) { total += m; // accumulator total = total + m; m++; // increment control variable }System.out.println(total); // print statement is outside the loop
scope
int m = 2, total = 0;
while(m < 6) { // body of looptotal = 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. { }
Trace
int m = 2; total = 0; while(m < 6) { total = total+m; m++; }System.out.println(total);
Check condition 2 < 6m is true? total 2 yes 2 3 yes 54 yes 95 yes 14 6 no get out of loop
total = 14 print 14
Decrement
int num = 10; while ( num > 0 ) // decrease until no longer > 0{System.out.println( num);num--; //decrement } // end while loopSystem.out.println("Loop ended");
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 = 77 7 + 2 = 9 7 + 9=169 9 + 2 = 11 16 + 11 = 27 11 stop
An accumulator is a variable that keeps a running total. It will add something to itself.
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 44 4 + 3 = 7 55 5 + 7 = 12 66 6 + 12 = 18 77 7 + 18 = 25 88 8 + 25 = 33 99 9 + 33 = 42 1010 10 + 42 = 52 1111 stop
An accumulator is a variable that keeps a running total. It will add something to itself.
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 = 74 5 5 + 7 = 12 5 6 6 + 12 = 186 7 7 + 18 = 257 8 8 + 25 = 338 9 9 + 33 = 429 stop
An accumulator is a variable that keeps a running total. It will add something to itself.
Decrement Check condition(5>=0)Start loop num = 5;num is true? print 5 yes 5 4 yes 43 yes 32 yes 2 3 Yes 1 0 no get out of loop
Print 54321Loop ended
int num = 5; while ( num > 0 ) { System.out.print( num);num--;} // endSystem.out.println("Loop ended");
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.
Trace
int b=2, sum=0;while(b<9){ b++; sum=sum+b;}System.out.print(sum);
b= sum=
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 conditiony<=10
y++ q=x+y x+=y;