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.


tchart

2379 posts

Uber Geek

ID Verified
Trusted

#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

gzt
17104 posts

Uber Geek

Lifetime subscriber

  #1096094 26-Jul-2014 13:05
Send private message

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



zaptor
745 posts

Ultimate Geek


  #1096123 26-Jul-2014 13:59
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.

Getsited
32 posts

Geek


  #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.



tchart

2379 posts

Uber Geek

ID Verified
Trusted

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

Behodar
10501 posts

Uber Geek

Trusted
Lifetime subscriber

  #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*

tchart

2379 posts

Uber Geek

ID Verified
Trusted

  #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.

redmouse
69 posts

Master Geek


  #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


 
 
 

Cloud spending continues to surge globally, but most organisations haven’t made the changes necessary to maximise the value and cost-efficiency benefits of their cloud investments. Download the whitepaper From Overspend to Advantage now.
zaptor
745 posts

Ultimate Geek


  #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

gzt
17104 posts

Uber Geek

Lifetime subscriber

  #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

gzt
17104 posts

Uber Geek

Lifetime subscriber

  #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





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









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.