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.


1467 posts

Uber Geek
+1 received by user: 178


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

9928 posts

Uber Geek
+1 received by user: 1493


  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.



1467 posts

Uber Geek
+1 received by user: 178


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

6215 posts

Uber Geek
+1 received by user: 1027

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*



1467 posts

Uber Geek
+1 received by user: 178


  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.

62 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

9928 posts

Uber Geek
+1 received by user: 1493


  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

9928 posts

Uber Geek
+1 received by user: 1493


  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 »

Microsoft Dynamics 365 Business Central launches
Posted 10-Jul-2018 10:40


Spark completes first milestone in voice platform upgrade
Posted 10-Jul-2018 09:36


Microsoft ices heated developers
Posted 6-Jul-2018 20:16


PB Technologies charged for its extended warranties and warned for bait advertising
Posted 3-Jul-2018 15:45


Almost 20,000 people claim credits from Spark
Posted 29-Jun-2018 10:40


Cove sells NZ's first insurance policy via chatbot
Posted 25-Jun-2018 10:04


N4L helping TAKA Trust bridge the digital divide for Lower Hutt students
Posted 18-Jun-2018 13:08


Winners Announced for 2018 CIO Awards
Posted 18-Jun-2018 13:03


Logitech Rally sets new standard for USB-connected video conference cameras
Posted 18-Jun-2018 09:27


Russell Stanners steps down as Vodafone NZ CEO
Posted 12-Jun-2018 09:13


Intergen recognised as 2018 Microsoft Country Partner of the Year for New Zealand
Posted 12-Jun-2018 08:00


Finalists Announced For Microsoft NZ Partner Awards
Posted 6-Jun-2018 15:12


Vocus Group and Vodafone announce joint venture to accelerate fibre innovation
Posted 5-Jun-2018 10:52


Kogan.com to launch Kogan Mobile in New Zealand
Posted 4-Jun-2018 14:34


Enable doubles fibre broadband speeds for its most popular wholesale service in Christchurch
Posted 2-Jun-2018 20:07



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.