hey guys,I'm soo beginner in C++ :$ .could you please explain logic of the loop

#include <iostream>
using namespace std;
int main()

int x = 0, p = 5;
while(x < p)
for(int i = 0; i < x; i++)
cout << "+";
cout << "\n";

the output will be

but I couldn't understand this part

for(int i = 0; i < x; i++)

i<x ? how the loop going to continue when i is zero and x is zero too

Recommended Answers

All 5 Replies

The second loop will not execute the first time.Then x will be incremented at line 14 so when the main loop reinitiates, x will be 1 and so on until it reaches five.

It doesn't run for the first turn through. In general, if the condition on a for loop evaluates to false to begin with, the loop is skipped.

In this case, after the for is skipped, x is incremented to 1, the next cycle of the while loop takes place, and that's when you see the '+' signs start printing.

Edit: beaten to the punch on that one

thanks all , but how in the third loop it shows ++ and fourth +++ and ...
how cout distaly the ++ signs in the third loop when I don't see anywhere in the codes that how many + signs it should represent ?

Every "+" character gets printed next to the previous one for i times each time x gets increased.

thanks I got it now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.