0

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";
x++;
}
}

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

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by zidane010h
2

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.

1

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

Edited by jonsca: n/a

0

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 ?

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.