What is the difference between the infinite loops for(;;) and while(true)?

Asked

Viewed 539 times

2

What is the difference between infinite loops for(;;) and while(true)?

Is there any advantage or disadvantage of using one or the other?

For example, in a simple terminal input validation code, to validate numbers int between 0 and 100:

#include <iostream>

using namespace std;

int main(void)
{
    int valor;
    //For
    for (;;)
    {
        cout << "Insira o valor: " << endl; cin >> valor;
        if (valor > 0 && valor <= 100)
        {
            break;
        }
        else
        {
            cout << "Por favor, insira um valor valido: " << endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    //While
    while (true)
    {
        cout << "Insira o valor: " << endl; cin >> valor;
        if (valor > 0 && valor <= 100)
        {
            break;
        }
        else
        {
            cout << "Por favor, insira um valor valido: " << endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    return 0;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.