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

10th Apr 2025, 1:40 AM
Sadam Hussain Sangain
Sadam Hussain Sangain - avatar
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 }
10th Apr 2025, 1:52 AM
Brian
Brian - avatar
+ 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!
10th Apr 2025, 3:30 AM
「HAPPY TO HELP」
「HAPPY TO HELP」 - avatar
+ 4
Ok thanks for all
10th Apr 2025, 6:23 AM
Sadam Hussain Sangain
Sadam Hussain Sangain - avatar
+ 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/
10th Apr 2025, 7:42 PM
Brian
Brian - avatar
+ 2
Ok sir thanks
11th Apr 2025, 3:30 AM
Sadam Hussain Sangain
Sadam Hussain Sangain - avatar
+ 1
How can i do meaning ful code in c++
10th Apr 2025, 10:03 AM
iktus
iktus - avatar
+ 1
Yes they works easy #c++
10th Apr 2025, 5:12 PM
Sadam Hussain Sangain
Sadam Hussain Sangain - avatar
+ 1
Hay
10th Apr 2025, 6:33 PM
wesem layith
wesem layith - avatar
+ 1
Hi
10th Apr 2025, 6:34 PM
wesem layith
wesem layith - avatar
+ 1
Ok
11th Apr 2025, 3:31 AM
Sadam Hussain Sangain
Sadam Hussain Sangain - avatar
+ 1
Yes
11th Apr 2025, 3:31 AM
Sadam Hussain Sangain
Sadam Hussain Sangain - avatar