# "built-in loop" question

This topic is 2096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Edit: Questions done, thanks for the answers.

-----

Hey guys, I'm planning on some day making a video series on Youtube, where I talk about certain game programming things. Without revealing too much, I'd like to ask you guys a couple of questions now and then, in order to be as accurate with the given topic as possible. I'm self-taught, so there might be some conventional details that I've overlooked.

So here's my first couple of questions:

1. What do you call the default process that automatically loops through the code and updates, whenever the program is ran? You know, the one where the computer tries to look for the Main function and run that, over and over again? Does it have an actual name, or is it fine to just call it a "built-in loop"?

2. Do all programming languages have a main function/method that the computer will look for and try to run? (I'm thinking it's a necessity, but please correct me if wrong)

3. Do all programming languages (today) cause the computer to loop infinitely through the code by default, unless you control it to do otherwise?

Edited by Malabyte

##### Share on other sites

1. What do you call the default process that automatically loops through the code and updates, whenever the program is ran? You know, the one where the computer tries to look for the Main function and run that, over and over again? Does it have an actual name, or is it fine to just call it a "built-in loop"?

There is no "built-in loop". The Main function/method is run precisely once. If you're thinking of the "Main Game Loop", then that has to be inserted by the programmer.

2. Do all programming languages have a main function/method that the computer will look for and try to run? (I'm thinking it's a necessity, but please correct me if wrong)

No, not all programming languages require a main function/method. Interpreted languages commonly don't require a Main. e.g. JavaScript, Scheme, Lua

3. Do all programming languages (today) cause the computer to loop infinitely through the code by default, unless you control it to do otherwise?

It's the other way around. Main is run exactly once. An infinite loop would have to programmed (or caused by a bug).

Edited by shacktar

##### Share on other sites
Um, in most languages main is run exactly once. There's no built-in loop.

In native languages main is called fist by virtue of how the compiler structures the executable. In interpreted languages, main is called by the language virtual machine.

##### Share on other sites

1. What do you call the default process that automatically loops through the code and updates, whenever the program is ran? You know, the one where the computer tries to look for the Main function and run that, over and over again? Does it have an actual name, or is it fine to just call it a "built-in loop"?

There is no "built-in loop". The Main function/method is run precisely once. If you're thinking of the "Main Game Loop", then that has to be inserted by the programmer.

Ah ok, thanks. I guess it's specific for Java then, as you have to manually terminate the program (e.g. in the IDE console) or declare EXIT_ON_CLOSE for a windowed app.

Edited by Malabyte

##### Share on other sites

Ah ok, thanks. I guess it's specific for Java then, because you have to manually close the process or else the program just keeps running.

Java just runs its main once, too. If your program keeps running, you're doing something to make that happen.

##### Share on other sites

Java just runs its main once, too. If your program keeps running, you're doing something to make that happen.

I'm talking about how the computer reads through the code in the class file. It "scans" through the first line, then the nth line until it reaches the bottom, then goes back to the first line again and reads all lines again. If it ran the code only once, then whenever you start to run the program it would immediately stop running, a millisecond or so afterwards.

For as long as I've been using Java, I've never had to explicitly tell the computer to continue to run the program. It will sometimes wait for some input or other change of its state, sure, but the program itself keeps "pulsing" through the class file(s). I've had to specifically tell it to terminate when I want it to. For instance, if you're using Eclipse to make a windowed Java application, then you specifically need to use the following line to terminate the program when you click the X to close the window (else the program keeps running even if the window isn't):

-----

window.setDefaultCloseOperation(EXIT_ON_CLOSE);

-----

I'm just very confused right now, I'm not sure what you guys are talking about. Does the Eclipse IDE, or the JFrame class specifically, have some sort of integral method that causes it the program to run continuously?

Edited by Malabyte

##### Share on other sites

Java just runs its main once, too. If your program keeps running, you're doing something to make that happen.

I'm talking about how the computer reads through the code in the class file. It "scans" through the first line, then the nth line until it reaches the bottom, then goes back to the first line again and reads all lines again. If it ran the code only once, then whenever you start to run the program it would immediately stop running, a millisecond or so afterwards.

For as long as I've been using Java, I've never had to explicitly tell the computer to continue to run the program. It will sometimes wait for some input or other change of its state, sure, but the program itself keeps "pulsing" through the class file(s). I've had to specifically tell it to terminate when I want it to. For instance, if you're using Eclipse to make a windowed Java application, then you specifically need to use the following line to terminate the program when you click the X to close the window (else the program keeps running even if the window isn't):

-----

window.setDefaultCloseOperation(EXIT_ON_CLOSE);

-----

I'm just very confused right now, I'm not sure what you guys are talking about. Does the Eclipse IDE, or the JFrame class specifically, have some sort of integral method that causes it the program to run continuously?

Java closes the application after all threads have finished executing (not when main is finished), main still only runs once though. (When you create a window the window system(Swing or AWT, other window systems may work differently) will have its own thread that keeps the application alive)

Edited by SimonForsman

##### Share on other sites

Java closes the application after all threads have finished executing (not when main is finished), main still only runs once though. (When you create a window the window system(Swing or AWT, other window systems may work differently) will have its own thread that keeps the application alive)

Ok, sweet. So I guess that, whenever you for instance make a console program with some println string, the string is rendered once and then kept updated by the console (e.g. Eclipse) or command prompt itself and not the program that initially rendered it? (unless, of course, you've explicitly told the program to wait for input or whatever).

If so, then everything makes perfect sense. Because then it's part of the IDE or the OS, I guess.

Edited by Malabyte

##### Share on other sites

Honestly, based on these questions and your 'understanding' shown in your replies, I don't think you should be trying to teach anyone anything - you clearly don't have the knowledge down at this point to be an authority source on the matter and any attempts to 'teach' others is going to do them more harm than good in the long run.

And you don't think that I will eventually acquire the knowledge, once I'm done with the research?

The bottom line is that there's an inconsistency in what I'm actually experiencing when I program, with the logic that has been explained to me in this thread. At least up until the point where SimonForsman elaborated on it.

Here's an example code:

class test {
public static void main (String[] args) {
System.out.println ("Hello world");
}
}

"Hello World" will show up in the console and it will keep showing up. No, it will obviously not visually repeat the line, for that you obviously need to create a loop of some sort. There's only a single line popping up. But that one line keeps itself visible on-screen and in Eclipse, the program doesn't terminate unless you specifically click on the terminate button. I'm just asking what causes that to happen, when I know that a screen ultimately consists of x*y pixels that keep updating as long as the PC screen is turned on. And if the program terminates, then what keeps updating the pixels on the screen? It sure ain't a terminated program.

You can be a pretty solid amateur programmer and still wonder about these subtle things, especially if you've been learning stuff through the internet (because there's all kinds of programming lingo and methodology that you have to figure out on your own). They are easy to miss, because they don't have much to do with the actual coding at all (assuming that we're talking about Java, which is the language I'm learning). There's a reason why a good University is sometimes superior to the internet.

So yes, I agree that I shouldn't be making videos until I know what I'm talking about. Which I'm not planning on doing, either. I just want to make a couple of videos (eventually) that I've yet to find on Youtube, because everyone's focused on teaching syntax and specific algorithms for specific tasks (but not teach people how to invent their own). But if someone else can beat me to it, then by all means. A Professional programmer with solid tutoring skills is a much better conveyor of information, anyways.

Edited by Malabyte

• 9
• 17
• 10
• 11
• 18