I'm a newbie here and this my first question. I learn basic c++ on Visual Studio 2019. I study functions and loops.
I have a homework that wants print:
I solved it with my code of 30 lines. But I just wonder is a there a 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; }
coutstatements executed sequentially. In the second iterationj == 1and the loop will be exitedif (j == 5) break;condition.if (j == 2), because it will alwaysbreakin the second iteration.continuenotbreak