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.




1415 posts

Uber Geek
+1 received by user: 168


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

9276 posts

Uber Geek
+1 received by user: 1323


  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.



1415 posts

Uber Geek
+1 received by user: 168


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

5998 posts

Uber Geek
+1 received by user: 910

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*



1415 posts

Uber Geek
+1 received by user: 168


  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.

61 posts

Master Geek
+1 received by user: 8


  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

9276 posts

Uber Geek
+1 received by user: 1323


  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

9276 posts

Uber Geek
+1 received by user: 1323


  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:





News »

Spark doubles down on wireless broadband
Posted 24-Jan-2018 15:44


New Zealand's IT industry in 2018 and beyond
Posted 22-Jan-2018 12:50


Introducing your new workplace headache: Gen Z
Posted 22-Jan-2018 12:45


Jucy set to introduce electric campervan fleet
Posted 22-Jan-2018 12:41


Hawaiki cable system will be ready for service in June 2018
Posted 22-Jan-2018 12:32


New Zealand hits peak broadband data
Posted 18-Jan-2018 12:21


Amazon Echo devices coming to New Zealand early February 2018
Posted 18-Jan-2018 10:53


$3.74 million for new electric vehicles in New Zealand
Posted 17-Jan-2018 11:27


Nova 2i: Value, not excitement from Huawei
Posted 17-Jan-2018 09:02


Less news in Facebook News Feed revamp
Posted 15-Jan-2018 13:15


Australian Government contract awarded to Datacom Connect
Posted 11-Jan-2018 08:37


Why New Zealand needs a chief technology officer
Posted 6-Jan-2018 13:59


Amazon release Silk Browser and Firefox for Fire TV
Posted 21-Dec-2017 13:42


New Chief Technology Officer role created
Posted 19-Dec-2017 22:18


All I want for Christmas is a new EV
Posted 19-Dec-2017 19:54



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.