Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Loops

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please help me out with this peice of code from Dan Chisholms mock test.

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);
}}

The result of compiling and running the code is

121212.

How are we supposed to interpret the do-while and for loops?
Are both the for loop and print statement nested inside the do-while loop or the do-while applies only to the for statement?
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
We can rewrite code as :


So as you can see that for loop is nested in do loop.
for loop is executed 3 times, each time printing 1 2
hence the output
121212

Hope this helps
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int j = 0;
do for (int i = 0; i++ < 2; )
System.out.print(i);
while (j++ < 2);
This is your LOC rigth.
It will be interpreted as
do
{
for (int i = 0; i++ < 2; )
{
System.out.print(i);
}
}
while(j++ < 2) ;
because if u dont put { the next immediate stmt will come under loop.
If u have more than one stmt u need to put them between { } .
If u have 1 stmt u need not put.
ok
 
Amit Kumar Ch
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys...that made it clear.
 
Greenhorn
Posts: 24
  • 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);
}
}
 
Patrick Punty
Greenhorn
Posts: 24
  • 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);
}
Friends I didnt get the increment part of 'i'
as our 'for' syntax goes like this
for(A,B,C)
{
D
}
and its sequence of interpretting is ABDC untill B returns False.
So my question is it should printing 0 in place of 1
and the output should be 010101.
Please make my doubt clear.

My understanding is
first the control will come to for loop there it will initialize the variable i with 0 and then it will check 0<2 (which is true) so it will print 0 and then will increment the value so it will become 1.

Please correct me if I am wrong.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Patrick Punty:
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);
}
Friends I didnt get the increment part of 'i'
as our 'for' syntax goes like this
for(A,B,C)
{
D
}
and its sequence of interpretting is ABDC untill B returns False.
So my question is it should printing 0 in place of 1
and the output should be 010101.
Please make my doubt clear.

My understanding is
first the control will come to for loop there it will initialize the variable i with 0 and then it will check 0<2 (which is true) so it will print 0 and then will increment the value so it will become 1.

Please correct me if I am wrong.




Hi Patrick,

Can you see that Increment statement in the condition block of for loop.
Every time condition is checked, i is incremented.

thus the output is 121212 instead of 010101

Hope this clears
[ October 09, 2005: Message edited by: Sandeep Chhabra ]
 
Patrick Punty
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sandy....I got the point.
Just to confirm my understanding
The control will first go to the for loop where it will initialize the value of i with 0 then it will increase the value of i but will check the condition with 0 itself i.e 0<2 and then it will print the incremented value i.e. 1 and so on the logic will go...
Right?
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yo.!!!

You Got It Right Patrick
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

great guys great work....
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic