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.




1442 posts

Uber Geek
+1 received by user: 174


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

9576 posts

Uber Geek
+1 received by user: 1397


  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.

 
 
 
 


Try Wrike: fast, easy, and efficient project collaboration software
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.



1442 posts

Uber Geek
+1 received by user: 174


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

6115 posts

Uber Geek
+1 received by user: 987

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*



1442 posts

Uber Geek
+1 received by user: 174


  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

9576 posts

Uber Geek
+1 received by user: 1397


  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

9576 posts

Uber Geek
+1 received by user: 1397


  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 »

Amazon launches the International Shopping Experience in the Amazon Shopping App
Posted 19-Apr-2018 08:38


Spark New Zealand and TVNZ to bring coverage of Rugby World Cup 2019
Posted 16-Apr-2018 06:55


How Google can seize Microsoft Office crown
Posted 14-Apr-2018 11:08


How back office transformation drives IRD efficiency
Posted 12-Apr-2018 21:15


iPod laws in a smartphone world: will we ever get copyright right?
Posted 12-Apr-2018 21:13


Lightbox service using big data and analytics to learn more about customers
Posted 9-Apr-2018 12:11


111 mobile caller location extended to iOS
Posted 6-Apr-2018 13:50


Huawei announces the HUAWEI P20 series
Posted 29-Mar-2018 11:41


Symantec Internet Security Threat Report shows increased endpoint technology risks
Posted 26-Mar-2018 18:29


Spark switches on long-range IoT network across New Zealand
Posted 26-Mar-2018 18:22


Stuff Pix enters streaming video market
Posted 21-Mar-2018 09:18


Windows no longer Microsoft’s main focus
Posted 13-Mar-2018 07:47


Why phone makers are obsessed with cameras
Posted 11-Mar-2018 12:25


New Zealand Adopts International Open Data Charter
Posted 3-Mar-2018 12:48


Shipments tumble as NZ phone upgrades slow
Posted 2-Mar-2018 11:48



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.