Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


1490 posts

Uber Geek
+1 received by user: 189

Trusted

Topic # 150548 26-Jul-2014 13:02
Send private message

Im looking at some Java code. Im no Java expert.

There is a for loop that has this syntax;

for (;;)
{
   //Do some stuff
}

What does this achieve? Surely it doesn't loop (ie it just runs the code once)?

Create new topic

gzt

10181 posts

Uber Geek
+1 received by user: 1560


  Reply # 1096094 26-Jul-2014 13:05
One person supports this post
Send private message

Infinite loop. Within the loop there will be a condition that breaks the loop.

658 posts

Ultimate Geek
+1 received by user: 28


  Reply # 1096123 26-Jul-2014 13:59
4 people support this post
Send private message

I would not recommend using a for construct for infinite looping. I realize it's used quite often - sadly.

Of the 3 general looping constructs used in procedural programming (for, while, do-while), I'd recommend while as a better option. I don't like using For loops for indeterminate (non-finite) loops. It just makes the code harder to read. All my own opinion of course.

28 posts

Geek
+1 received by user: 2


  Reply # 1096151 26-Jul-2014 14:57
Send private message

I can't remember the syntax but there will probably be a "break" there somewhere to exit the loop after some condition is met.



1490 posts

Uber Geek
+1 received by user: 189

Trusted

  Reply # 1096266 26-Jul-2014 17:50
Send private message

Ah right, thanks guys. I wouldnt have thought it was an infinite loop.

Yes, there is a return statement in the loop with no condition. The code is very odd. 

for (;;)
{
sendCommandToRemote(9, str2);
sendPureDatToRemote(str1.getBytes());
return;
str1 = "-1";
int i = str1.length();
str2 = "bnsrv_alarm mtk_bnapk -1 0 " + String.valueOf(i) + " ";
}

6288 posts

Uber Geek
+1 received by user: 1069

Trusted
Lifetime subscriber

  Reply # 1096269 26-Jul-2014 18:11
Send private message

I don't know any Java, but at a glance it looks like it just calls the two "send" methods then leaves the loop... so why have a loop at all? *confused*



1490 posts

Uber Geek
+1 received by user: 189

Trusted

  Reply # 1096309 26-Jul-2014 20:34
Send private message

Behodar: I don't know any Java, but at a glance it looks like it just calls the two "send" methods then leaves the loop... so why have a loop at all? *confused*


Exactly! So why would there be code after the return statement in the same loop. Very odd syntax.

64 posts

Master Geek
+1 received by user: 8

Subscriber

  Reply # 1096336 26-Jul-2014 20:56
Send private message

I'm pretty sure that will not compile. Similar...

package example;

public class Dingo
{
   public void thing()
   {
      for (;;)
      {
         System.out.println("hello");
         return;
         System.out.println("goodbye");
      }
   }

   public static void main(String args[])
   {
      new Dingo().thing();
   }
}

Compile...
javac example/Dingo.java

Yields...
example/Dingo.java:11: error: unreachable statement System.out.println("goodbye");
^ 1 error


658 posts

Ultimate Geek
+1 received by user: 28


  Reply # 1096349 26-Jul-2014 21:24
Send private message

tchart: The code is very odd. 

for (;;)
{
sendCommandToRemote(9, str2);
sendPureDatToRemote(str1.getBytes());
return;
str1 = "-1";
int i = str1.length();
str2 = "bnsrv_alarm mtk_bnapk -1 0 " + String.valueOf(i) + " ";
}


Yep. That is indeed some bizarre code.

gzt

10181 posts

Uber Geek
+1 received by user: 1560


  Reply # 1096413 27-Jul-2014 00:13
Send private message

To state the obvious:

a) 'Break' has not been used in this loop.
b) Technically 'Return' does not (simply) break the loop. It returns control to the calling method.
c) The statements after 'return;' are not executed

And a mildly insane possibility: There is recursion somewhere else in the code which refers to this structure in a very bizarre way.

Some less obvious possibilities: The code may originally have been complied with a not very standard compiler or compiled with some or many error and warning suppress flags.

gzt

10181 posts

Uber Geek
+1 received by user: 1560


  Reply # 1096414 27-Jul-2014 00:14
Send private message

Questions:

d) So is this actual running code?
e) Does this compile on your system without warnings?
f) Or are you a student and this is some crazy example code you are supposed to sort out for a project task? : )

Create new topic

Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.