+ 3
Can a for loop run infinitely without any condition?
Is it possible to create an infinite for loop in C++ without using any condition inside the parentheses? If yes, how would it look like, and what is its use case? Provide an example if possible.
11 Respostas
+ 11
Yes. The parts of a for loop are optional. Valid syntax of an infinite for loop can look like this:
for (;;) {
//loop forever
}
In fact this is often used, as it is as convenient as writing a while loop with a constant true condition.
Optionally you may use the initialization part, and if you wish, the update part for a counter. Neither are required.
for (int i = 0; ; ++i) {
cout << i; // count forever
}
+ 9
Use cases are generally for programs that interact with the real world be it a user, machine, or environment
1) servers waiting for responses
2) gui or tui programs such as editors (text, image, pdf, etc.)
3) battery monitor scripts
4) chat bots
5) video games
6) motion detectors
7) medical equipment
8) atms, electronic menus, production robots
So many!
+ 4
Ok thanks for all
+ 2
Hello iktus and wesem layith, welcome to Sololearn!
Feel free to participate, but please keep your replies within the topic of discussion. Here are the guidelines for posting:
https://www.sololearn.com/Discuss/3021159/?ref=app
https://www.sololearn.com/en/Content-Creation-Guidelines/
+ 2
Ok sir thanks
+ 1
How can i do meaning ful code in c++
+ 1
Yes they works easy
#c++
+ 1
Hay
+ 1
Hi
+ 1
Ok
+ 1
Yes