File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

do while loop

 
arivu mathi
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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

use tags

thanks
chaitanya
 
Akin Ola
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Buhi Mume
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result is actually 121212, not 1212, since this is a do loop.

If this is a while loop, the result will be 1212.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic