What is the importance of condition in a loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the importance of condition in a loop?

6th Jan 2019, 8:52 AM
Muhammad Talha
Muhammad Talha - avatar
8 Answers
0
You don't need a condition. You can simply put (true), but the issue comes in when your computer or phone overheats. You see, if you have a loop with no condition, that just repeats forever, your device will try to execute that code as fast as possible, forever. This is very bad for your device. Most of the time, the compiler will prevent you or warn you from doing that. A condition in a loop determines how many times to run the loop, if the loop should be run, etc. Basically it controls the loop and decides what happens to the code inside of it.
6th Jan 2019, 8:56 AM
Rain
Rain - avatar
+ 1
Ah, I see. Well, infinite loops are fine, it's when there is nothing like "sleep()" in the program. What happens is that, in other programming, there is that function to allow the system to pause for a couple milliseconds. This gives the system rest. Repetitive loops that run for the entirety of the program or in certain conditions, but run for a long period time, show up in games commonly. The thing is though, the sleep function. Without it, what I explained in the first one will occur. The system will try and run the code as fast as it can, with nothing stopping or limiting it. It's like punching a brick wall as hard as you can. It is possible, but just because you can doesn't mean you won't get hurt. Sorry if I sounded rude, I thought you wanted proof for what I was saying, as in you didn't believe me, and not that you wanted a more deep explanation on the subject. Sorry for misunderstanding.
6th Jan 2019, 5:01 PM
Rain
Rain - avatar
+ 1
Probably around 30 seconds? For the program to crash, not the computer to break. Main menus need the sleep function too. People can leave a game open on for 30 minutes and come back later, or something. You wouldn't expect the way the programmer did it to mess up your computer. Also the program does crash pretty fast. There's no "natural" recovery breaks. Just because the program exits one loop and enters another doesn't mean there's a break. If the whole program is just massive, a 3D game let's say, and there are no sleep functions anywhere or anything relating to waiting milliseconds, it will execute it all as fast as possible. It's unhealthy overall.
6th Jan 2019, 5:27 PM
Rain
Rain - avatar
0
'This is very bad for your device.' I tried to google for the truth of that statement but haven't found a confirmation. Do you have a source for that?
6th Jan 2019, 11:02 AM
HonFu
HonFu - avatar
0
HonFu No. I'm not going to search for proof for someone that doesn't understand. You can search and find it yourself. I'll try to explain one more time. Compilers will usually try to stop this. You can sometimes ignore the compiler and start the program anyway. A loop without a condition is impossible. It needs a condition, that's why I took meaning "no condition" as simply true. In this case, the code will run forever. The program will either crash or your device, in some cases, can crash too.
6th Jan 2019, 4:42 PM
Rain
Rain - avatar
0
Rain, what I meant when I wrote 'no confirmation' (and I thought this would be clear): Googling I found several statements (SO etc.) along the lines of 'computers are made for calculating stuff all day, an infinite loop normally won't do any damage.' That's why I asked you for more details about this. But feel free not to, if this is beneath you.
6th Jan 2019, 4:53 PM
HonFu
HonFu - avatar
0
Well, to be honest, it was a bit of both. I thought: Wait, I never heard that, googled a bit, found nothing spectacular, and wanted some proof/deeper explanation. I suppose it depends a bit on things like program design or language too? I mean, if a program loops over a main menu and several sub menus and a user input is taken here or there, these little recovery breaks would occur naturally, right? And I suppose (didn't check) higher languages like Python or JS wouldn't allow a total brick wall banging marathon in the first place... and as you said, compilers would also try to warn you. Let's say, though, we have a dumb loop that doesn't do a lot, but forever, like assigning a random number from 1 to 10 to a variable infinitely ... would that be an issue? I mean, it's a stable effort, and not a big one. How long would it take until the computer breaks? I can see (obviously) how this would be different with recursion involved, since more and more memory is used and naturally there has to be a crash.
6th Jan 2019, 5:21 PM
HonFu
HonFu - avatar
0
I haven't thought about that issue so far. Food for thought, thank you!
6th Jan 2019, 5:36 PM
HonFu
HonFu - avatar