Java Unit II - 05-8 Decision Looping
Java Unit II - 05-8 Decision Looping
Java Unit II - 05-8 Decision Looping
Self-Finance Wing
Class 11
Unit II
On 05/08/2020 at 10:45 a.m to 11:30 a.m
Handled by
Ms. S. Anusuya, M.C.A.,M.Phil.,
Assistant Professor, Dept. of Computer Science
Java Programming,
05/08/2020, 10:45 A.M Ms. S. Anusuya, Asst. Professor in Computer 2
Science
Java Programming,
3
05/08/2020, 10:45 A.M Ms. S. Anusuya, Asst. Professor in Computer
Science
The while statement
condition
true
statement
Example - Counter
// Counts from 1 to 5
class Counter {
static final int LIMIT = 5;
statement
true
condition
false
The for Statement
false
condition
true
statement
increment
Multiplication Table
Example
class MultiplicationTable {
public static void main(String[]
args){
for(int j=1 ; j <= 10 ; j++) {
for(int k=1 ; k <= 10 ; k++)
System.out.print(j*k);
System.out.println();
}
}
}
The break and
continue statements
• The break statement, which we used
with switch statements, can also be
used inside a loop
• When the break statement is
executed, control jumps to the
statement after the loop (the condition
is not evaluated again)
• A similar construct, the continue
statement, can also be executed in a
loop
• When the continue statement is
executed, control jumps to the end of
the loop and the condition is evaluated
Break and Continue
class AvgExample2 {
Example
public static void main(String[] args)
{
InputRequestor in = new
InputRequestor();
double x, sum = 0; count = 0;
while(true){
x = in.RequestDouble();
if (x == 0)
break;
if (x < 0) {
System.out.println(“Only
positive numbers!”);
continue;
}
sum += x ;
count ++ ;
} // continued on next page
Break and Continue
Example (2)
System.out.println(“The average is
“+sum/count);
}
}
Thank You....
Any Queries?…….
Java Programming,
25
05/08/2020, 10:45 A.M Ms. S. Anusuya, Asst. Professor in Computer
Science