educative.io

Why would this condition be true and how is it recognized as a condition?

#include

using namespace std;

int main()

{

int a = 1;

while(a) { // the while condition will always be met as will always return true

  cout << "Infinite loop\n" << endl;

}

return 0;

} How is a read as True?

Hi @Nour_Ezzeddine, Thanks for reaching out to us.
The statement inside the parenthesis of while is called condition. If this condition is true, the loop runs one time, and the condition is checked again, this goes on and on until the condition gets false and the loop finally terminates.
The while(1) or while(any non-zero value) is used for infinite loop. There is no condition for while. As 1 or any non-zero value is present, then the condition is always true. So what is present inside the loop will be executed forever.

Hope it will help, Happy Learning :slight_smile:

2 Likes

Thanks! sure did!


Course: Learn C++ from Scratch - Free Interactive Course
Lesson: Infinite Loops - Learn C++ from Scratch

1 Like

Hi @Nour_Ezzeddine, You’re welcome
We are glad to hear that you are satisfied and your feedback is much appreciated :slight_smile: