• 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

Nested for Loop doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

Code below,


How does the above code compiles? There is no brace for the do loop. Have anyone come across such a loop?? Please explain the logic behind how the complier interprets it?

Thanks in advance.
[ December 07, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code will compile only when there is no ; at the end of for loop.

If that is the case, a single statement would do since
no braces are required for the do statement as long as there is only one statement.
But when we put a ; at the end of the for loop, there comes 2 statements for & System.out.print().

Since i could see a smiley in your code, i assumed there could have been a semicolon at the end of for loop.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vinayagar,

Thanks for the reply.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my answer is it outputs 1 1
if I am wrong correct me. Thanks in advance.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

with better indentation the code looks like this:




prints
121212

No, I've never seen a do loop without braces before. But it just works like a for loop without braces or an if condition without braces. As Vinayagar wrote.


Yours,
Bu.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Th code works without braces.I have seen many codes without braces.The braces are required to make code readable and clearer.

your code:
-----------------------------------------------------------------

public class Main {
public static void main(String[] args)
{
int j = 0;
do
for (int i = 0; i++ < 2; /* not obligate */ )System.out.print(i);
while (j++ < 2);
}
}
--------------------------------------------------------

the semicolon is needed in the for statement or else it shows compile time error.
The for statement runs two times to every single do-while loop
that is,
when j=0 i=1,i=2
j=1 i=1,i=2
j=2 i=1,i=2

so the output is 121212.

another example without braces

code
------------------------------------------------------------------
if (aNumber >= 0)
if (aNumber == 0) System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");

--------------------------------------------------------------------
code
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit surprised to see people say they've never seen braces omitted. Unless you just mean on a do loop, which is maybe understandable because do/while is fairly rare in the first place. But in general, braces may be omitted from any for, while, do, if or else clause, if the body attached to that clause consists of just one statement. In this particular case that's less obvious because the statement a for loop - but note that there's only one line-ending semicolon in that for loop. That's how you can tell that it's just one statement:

[praks]: The braces are required to make code readable and clearer.

Well, that's a matter of opinion. Some of us view unnecessary braces as line noise. It's true that many people consider it bad form to omit braces in these cases. However I certainly think it's necessary for programmers to be able to read and understand such code, even if they believe code shouldn't be written that way. I don't remember if SCJP intentionally tests on this point - I think it does, but I'm not sure. Regardless, I think any professional Java programmer should understand the language rules well enough to successfully understand why something like this does not work:

while this does:

Because at some point in your career, you will probably see examples like both of those.
[ December 07, 2006: Message edited by: Jim Yingst ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim Yingst posted December 07, 2006 12:47 PM

I'm a bit surprised to see people say they've never seen braces omitted.



no, as I said, never saw a do loop without braces.

Bu.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, point taken.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic