• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

do while loop

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic