Loops in Java
         


o 
o 
o 
Java For Loop
for loop

 
o !"#
o " $"#
o #"#
Java Simple For Loop
%&&''()*
+
, Initialization+
-))

. Condition+
    

/ Statement+           

0 Increment/Decrement+

Syntax:
for1)2234

5
Flowchart:
Example:
6
,
publicclass"$4
publicstaticvoid1!7834
&
for1int9,2:9,;2''34
!132
5
5
5
<+
1
2
3
4
5
6
7
8
9
10
Java Nested For Loop
*

Example:
publicclass="$4
publicstaticvoid1!7834

for1int9,2:9/2''34
 
for1int 9,2 :9/2 ''34
!1'>>' 32
5
5 
5
5
<+
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Pyramid Example 1:
publicclass6$4
publicstaticvoid1!7834
for1int9,2:9?2''34
for1int 9,2 :92 ''34
!1>@>32
5
!132
5
5
5
<+
*
* *
* * *
* * * *
* * * * *
Pyramid Example 2:
publicclass6$.4
publicstaticvoid1!7834
int9A2
for1int9,2:92''34
for1int 92 B92 34
!1>@>32
5
!132
5
5
5
<+
* * * * * *
* * * * *
* * * *
* * *
* *
*
Java for-each Loop
 
C

*

Syntax:
for1+34

5
Example:
"

publicclass"$$4
publicstaticvoid1!7834
D
int7894,../00?AEF52
6
for1int+34
!132
5
5
5
<+
12
23
44
56
78
Java Infinitive For Loop
22
Syntax:
for12234

5
Example:


publicclass"$4
publicstaticvoid1!7834
G
for12234
!1>>32
5
5
5
<+
infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c
='
Java While Loop
while loop

Syntax:
while134

5
Example:
publicclass($4
publicstaticvoid1!7834
int9,2
while1:9,;34
!132
''2
5
5
5
<+
1
2
3
4
5
6
7
8
9
10
Java Infinitive While Loop
true
Syntax:
while1true34

5
Example:
publicclass($.4
publicstaticvoid1!7834
while1true34
!1>>32
5
5
5
<+
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c
='
Java do-while Loop
do-while loop


 do-while loop       
*
Syntax:
do4

5while132
Example:
publicclassD($4
publicstaticvoid1!7834
int9,2
do4
!132
''2
5while1:9,;32
5
5
<+
1
2
3
4
5
6
7
8
9
10
Java Infinitive do-while Loop
true
Syntax:
do4

5while1true32
Example:
publicclassD($.4
publicstaticvoid1!7834
do4
!1>>32
5while1true32
5
5
<+
infinitive do while loop
infinitive do while loop
infinitive do while loop
ctrl+c
='
Java Break Statement
(    *                  
         

 break    *   
*H
*
(*

Syntax:
2
break2
Java Break Statement with Loop
Example:
6*

publicclassI*$4
publicstaticvoid1!7834

for1int9,2:9,;2''34
if199?34
*
break2
5
!132
5
5
5
<+
1
2
3
4
Java Break Statement with Inner Loop
**
Example:
6*

publicclassI*$.4
publicstaticvoid1!7834

for1int9,2:9/2''34

for1int 9,2 :9/2 ''34
if199.JJ 99.34
*
break2
5
!1'>>' 32
5
5
5
5
<+
1 1
1 2
1 3
2 1
3 1
3 2
3 3
Java Break Statement in while loop
Example:
6*

publicclassI*($4
publicstaticvoid1!7834

int9,2
while1:9,;34
if199?34
*
''2
break2*
5
!132
''2
5
5
5
<+
1
2
3
4
Java Break Statement in do-while loop
Example:
6*

publicclassI*D($4
publicstaticvoid1!7834

int9,2

do4
if199?34
*
''2
break2*
5
!132
''2
5while1:9,;32
5
5
<+
1
2
3
4
Java Continue Statement



continue statement
H*

(

Syntax:
, 2
. continue2
Java Continue Statement Example
Example:
6

publicclass&$4
publicstaticvoid1!7834

for1int9,2:9,;2''34
if199?34

continue2*
5
!132
5
5
5
<+
1
2
3
4
6
7
8
9
10
Java Continue Statement in while loop
Example:
6

publicclass&($4
publicstaticvoid1!7834

int9,2
while1:9,;34
if199?34

''2
continue2*
5
!132
''2
5
5
5
<+
1
2
3
4
6
7
8
9
10
Java Continue Statement in do-while loop
Example:
6

publicclass&D($4
publicstaticvoid1!7834

int9,2

do4
if199?34

''2
continue2*
5
!132
''2
5while1:9,;32
5
5
<+
1
2
3
4
6
7
8
9
10