aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes do while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "do while loop" Watch "do while loop" New topic
Author

do while loop

arivu mathi
Ranch Hand

Joined: Jun 26, 2006
Posts: 38
class JMM110 {
public static void main (String[] args) {
int j = 0;
do for (int i = 0; i++ < 2
System.out.print(i);
while (j++ < 2);
}}

output:1212.....can u explain this???
Chaitanya Vivek
Ranch Hand

Joined: May 17, 2006
Posts: 36
Hi

Pls post code properly.
for loop has no increment expression neither it is separated by ;

use tags

thanks
chaitanya
Akin Ola
Greenhorn

Joined: Jul 18, 2006
Posts: 4
//if this is the code u meant to ask about here's the explanation

class JMM110 {
public static void main (String[] args) {
int j = 0;

do
for (int i = 0;i++ < 2
System.out.print(i);
while (j++ < 2);
}
}

//what's happening is pretty straight forward
//u've got 2 loops, the outer one is the do-loop, the innner one is a for-loop
//the outer loop iterates twice but each time the do-loop goes thru an iteration
//the inner (for) loop also iterates twice printing '12' each time
//so ur final output is 1212 .... easy!
Akin Ola
Greenhorn

Joined: Jul 18, 2006
Posts: 4
Buhi Mume
Greenhorn

Joined: Jul 16, 2006
Posts: 17
The result is actually 121212, not 1212, since this is a do loop.

If this is a while loop, the result will be 1212.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do while loop
 
Similar Threads
Loops
loop related doubt
Regarding Looping...
Could some one explain the follwoing..
Nested loop