r/javahelp • • Aug 09 '22

what does for(;;) mean?

Just like the title asks, what does for(;;) mean? A double semicolon in a for loop, I found it in some code I'm trying to understand and Google can't seem to answer my question. Thanks!

17 Upvotes

14 comments sorted by

•

u/AutoModerator Aug 09 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

25

u/NautiHooker Software Engineer Aug 09 '22

Equivalent to

while (true)

You omit the iteration variable, its incrementation and the exit condition, which means it becomes an endless loop that needs to be ended with a break/return statement somewhere.

If I am not mistaken this way of writing such loops comes from older C like languages that didnt have the option for while(true).

8

u/Encryp73r Aug 09 '22

I see how it works now, the while(true) comparison helped. That's why they use a return statement. Thanks!

2

u/jonathancast Aug 09 '22

C can do while (1), or while (TRUE) with a macro. More likely for (;;) is shorter.

8

u/knoam Aug 09 '22

I've heard that called a dracula loop because it looks like a vampire with fangs.

2

u/Encryp73r Aug 09 '22

Forever be it's name for me now thanks hah

1

u/goldfingeroo7 Aug 09 '22

Looks like a spider head to me. But I get the fangs.

10

u/LambdaThrowawayy Aug 09 '22 edited Aug 09 '22

Any of the the three statements in a for-loop are optional; you don't have to include an initialization, termination or increment statement. Skipping out on all three creates an infinite loop.

See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for details.

6

u/tsvk Aug 09 '22

It's just a regular for-loop, just stripped out of content into its bare essentials.

Just as you can say for (int i=0;i<10;i++) { }, if you leave out the initialization int i=0, the continuation condition i<10 and the increment i++, then what you are left with is for (;;) { }.

4

u/call_911911 Aug 09 '22

It's a dick move.

Infinite loop unless there's a break inside the for logic

3

u/kaloschroma Aug 09 '22

When you're crying do

3

u/m1ss1ontomars2k4 Aug 09 '22

All basic for loops have 2 semicolons. The components separated by semicolons are, in order:

  1. Any code to run before the loop runs for the first time.

  2. Any condition to check. If it's true at the end of any loop, it will exit the loop.

  3. Any code to run before the aforementioned check.

All 3 are optional. So your for(;;) means: run nothing before the loop runs, don't check any condition after each run, and run nothing before each such check.

2

u/taftster Aug 09 '22

Forevah!