lundi 1 mars 2021

Is there a better and shorter way of my for loop?

i'm newbie here and this my first question. I learn basic c++ on Visual Studio 2019. I study on functions and loops.

I have a homework that wants print:

I solved it with my code of 30 lines. But I just wonder is there shorter and better way of this?

using namespace std;
int main() 
{   
        for (int j = 0; j < 5; j++)
        {
            if (j == 1)
                break;
            cout << "*";
            cout << endl;
            if (j == 2)
                break;
            cout << "**";
            cout << endl;
            if (j == 3)
                break;
            cout << "***";
            cout << endl;
            if (j == 4)
                break;
            cout << "****";
            cout << endl;
            if (j == 5)
                break;
            cout << "*****";
            cout << endl;
        }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire