Transcript while loop

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:
+=
-=
*=
/=
%=
1
add and assign
subtract and assign
multiply and assign
divide and assign
remainder and assign
Java Programming, Sixth Edition
y += 2
y -=2
y*=2
y/=2
y%=2
y = y+ 2;
y = y-2;
y=y*2
y=y/2
y=y%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 --
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.
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
5-5
"while" loops
• A while statement has the
following syntax:
condition
true
statement
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
false
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 AddisonWesley. All rights reserved
5-7
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
• An example of an infinite loop:
© 2004 Pearson AddisonWesley. All rights reserved
int count = 1;
while (count <= 25){
System.out.println (count);
count = count - 1;
}
5-8
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 variable
while(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
3
3
Yes
4
4
4
Yes
5
5
5
no
stop
Print 2345
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
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. { }
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
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");
accumulator variable
An accumulator is a variable that keeps a running total. It will add something to itself.
m
b+2
int b = 5 , ans = 0;
5
5+2 = 7
7
7+2=9
while(b<11)
9
9 + 2 = 11
11 stop
{
b=b+2;
ans=ans+b;
}
System.out.println(ans);
27
ans=ans+b
7+0 = 7
7 + 9=16
16 + 11 = 27
accumulator variable
An accumulator is a variable that keeps a running total. It will add something to itself.
k
int k=3 , tot = 0;
while(k<11)
{
tot = tot+k;
k++;
}
System.out.println(tot);
tot+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
10 10 + 42 = 52 11
11 stop
52
k++
accumulator variable
An accumulator is a variable that keeps a running total. It will add something to itself.
z
int z=2 , sum = 0;
2
3
while(z<9)
4
5
{
6
7
z++
8
9
sum = sum+z
}
System.out.println(sum);
zz++
sum=sum+z
3
4
5
6
7
8
9
stop
3+0
4+3=7
5 + 7 = 12
6 + 12 = 18
7 + 18 = 25
8 + 25 = 33
9 + 33 = 42
42
Decrement
int num = 5;
while ( num > 0 ) {
System.out.print( num);
num--;}
// end
System.out.println("Loop ended");
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
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
condition
y<=10
y++
q=x+y
x+=y;