aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested for Loop doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Nested for Loop doubt?" Watch "Nested for Loop doubt?" New topic
Author

Nested for Loop doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

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 ]

SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

Vinayagar,

Thanks for the reply.
Nomula vasudevu
Ranch Hand

Joined: Oct 25, 2006
Posts: 107
my answer is it outputs 1 1
if I am wrong correct me. Thanks in advance.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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 ]

"I'm not back." - Bill Harding, Twister
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Ah, point taken.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Nested for Loop doubt?
 
Similar Threads
for loop
Nested loop
Loops
do while loop
why does 'break' traverse one more time