Number pattern problem

I have 2 doubts in this code
when i declare value variable inside the loop itself , the output comes out all numbers equal to 2 . Why is that so?

when i had written val=val+1; before cout <<value<<" ";
my output started with instead of 1.
why did it happen?

*started with 2 instead of 1

@ananya257singh can you please share the code and comment your code so that we can discuss it further and please follow cb.lk/askdoubt. In this short link guidline is given how you can share your implementation.

@ananya257singh hey Ananya
you have doubt about three cases
1 when we declare value=1 variable outside the second loop.
n=5 , i=1
The first loop run from i <=n.
the second loop will depend upon on first loop
d<=i
Now what is happening inside the second loop is
that for first iteration i.e i=1
1<=5
the second loop runs from
1<=1
so present value is 1
so first time on printing cout<<value
output
1
now the value is incrementing but value=value+1
value=2
now for the second iteration when i=2
2<=5
the second loop will run
1<=2
now the initial value of value is again 1 this is because when the first loop will be executed the value-initialize with 1
so output here will be
1
value=value+1; value is incrementing
the second loop this time run 2<=2
2
final output 1 2
now when i=3
3<=5
1<=3
first time value is 1
value=value+1;
value=2;
2<=3
second time
value=2
value=value+1;
value=3;
third time
3<=3
value =3
cout<<value;
output
3
value=value+1;
4
when i=4
first loop 4<=5
second loop 1<=4
value this time 1
1
value=value+1;
value=2
2<=4
value will be 2
2
value=value+1;
value=3
3<=4
value will be
3
value=value+1;
4
4<=4
value on printing time
4
output for 4 iteration
1 2 3 4
value=value+1
5
but the second loop will terminate
these phenomena work for 5 time
after five iteration
final output is like
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

@ananya257singh hey Ananya now what will be output when value declare value inside the second loop
for i=1
1st loop 1<=5
2nd loop 1<=1
value this time is 1
so 1 will be printed
now value is incremented by one
value =value+1;
value =2;
but the point is that this will not print 2 instead one because when you come for second iteration
2<=5
1<=2
value is again initialize to 1
so on printing
1
then after incrementing value by one i.e value=2
when 2<=2
runs value is already declare inside the second loop
this will again change the value of variable value=1;
so this time value will again 1
output is
1
so after five iteration
final output is
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1

@ananya257singh hey Ananya third case when value is declare inside the second loop and value is incremented before printing
so that first loop will work like this
1<=5
1<=1
value this time 1
but you’re incrementing this time your value variable so value is 2
output will be like
2
2<=5
1<=2
value is 1
value got incrementing
by 1
value after incrementing
value =2
it will print 2
again when
2<=2
value is 1
increment the value
by one value is 2
it will print 2 again
output in this iteration
2 2
after 5 iteration the final output is
2
2 2
2 2 2
2 2 2 2
2 2 2 2 2

Thank you so much for clearing all my doubts

@ananya257singh Happy coding :smile: