0% found this document useful (0 votes)
4 views16 pages

Java Loop Control Statements Explained

Uploaded by

palpooja937
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Java Loop Control Statements Explained

Uploaded by

palpooja937
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

We understand each if these in detail

(c) D. R. Gangodkar
(c) D. R. Gangodkar
class BreakLoop
{
public static void main(String args[])
{
for (int i=0; i<100; i++)
{
if (i == 10)
break;
[Link]("i: " + i);
}
[Link]("Loop complete.");
}
} (c) D. R. Gangodkar
Write a Program to achieve the output given below:

Pass i=0: and j=0 1 2 3 4 5 6 7 8 9


Pass i=1: and j=0 1 2 3 4 5 6 7 8 9
Pass i=2: and j=0 1 2 3 4 5 6 7 8 9
Loops complete.

Program has two loops (nested).


• i loop counts from 0 to 2 and
• j loop counts from 0 to 19
Note: the j loop counts only up to 9 though it can
count till 19

(c) D. R. Gangodkar
Pass i=0: and j=0 1 2 3 4 5 6 7 8 9
class NestedLoopBreak
{
Pass i=1: and j=0 1 2 3 4 5 6 7 8 9
public staticPass i=2: and j=0
void main(String 123456789
args[])
{ Loops complete.
for (int i=0; i<3; i++)
{
[Link]("Pass i=" + i + ": and j= ");
for (int j=0; j<20; j++)
{
if (j == 10)
break;//Break loop j
[Link](j + " ");
}
[Link]();
}
[Link]("Loops complete.");
}
}// Click for output:
(c) D. R. Gangodkar
labeled break

However it supports labeled break


Lets understand it with a program.
Consider again the previous program:
Program has two loops (nested).
• i loop counts from 0 to 2 and
• j loop counts from 0 to 19
Write a code to achieve the following output:
Pass i=0: and j= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Pass i=1: and j= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Loops complete.
Note: the i loop counts only up to 1 though it can
count till 2 and j counts up to 18 in last iteration
(c) D. R. Gangodkar
class BreakLabeled
{
public static void main(String args[])
{
first: for ( int i=0; i<3; i++)
{
[Link]("Pass i=" + i + ": and j= ");
second: for ( int j=0; j<20; j++)
{
if (i==1 && j==19 )
break first; //Break loop i and j
else
[Link] ( j + " ");
} //for j
[Link]();
} // for i
[Link]("\nLoops complete.");
} // main method
}
(c) D. R. Gangodkar
One more Program of labeled break. What is the O/p
class Break
{
public static void main(String args[])
{
int v=10; Before the break.
first: {
second: {
After second block.
third: {
[Link]("Before the break.");
if (v==10)
break second;
[Link]("This won't execute");
}
[Link]("This won't execute");
}
[Link](“After second block.");
}
}
} // Click to see the output (c) D. R. Gangodkar
class BreakError
{
public static void main(String args[])
Compile time
{
one: for(int i=0; i<3; i++) error;
{ Undefined
[Link]("Pass " + i + ": "); label “one”
}
for (int j=0; j<100; j++)
{
if (j == 10)
break one;
[Link](j + " ");
}
}
}
(c) D. R. Gangodkar
(c) D. R. Gangodkar
Write a program to display the following output
using for loop (0- 9) and continue statement.

Output:
01
23
45
67
89

(c) D. R. Gangodkar
class Continue
{
public static void main(String args[])
{
for (int i=0; i<10; i++)
{
[Link](i + " ");
if (i%2 == 0)
continue; Output:
else 01
[Link](""); 23
} 45
} 67
} 89

(c) D. R. Gangodkar
Write a program to print the following output

0
01
024
0369
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
(c) D. R. Gangodkar
class LabeledContinue
{
public static void main(String args[])
{
outer: for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
if (j > i)
{
[Link]();
continue outer;
}
[Link](" " + (i * j));
}
}
[Link]();
}
}
(c) D. R. Gangodkar
Lets see examples

(c) D. R. Gangodkar
class Return
{
public static void main(String args[])
{
boolean t = true;
[Link]("Before the return.");
if (t)
return; // return to caller
[Link]("This won't execute.");
}
}

(c) D. R. Gangodkar

You might also like