+ 1

Please help explain this

int count = 1; for(int i = 0; i <= 99; i++){ for(int j = 0; j <= 99; j++){ count++; } } cout << count;

29th Jun 2022, 7:48 PM
DevAbdul
DevAbdul - avatar
9 Respuestas
+ 2
I see that you have started the V+ course? Do you understand for-loops? The inner loop incremebts count by 1 100times. The outer loop runs 100 times, that means the inner loop runs 100times, 100*100. And 1 more for.the initial value. Run the code and output the steps inside the loops to see what is going on.
29th Jun 2022, 8:24 PM
Lisa
Lisa - avatar
+ 5
100*100+1 The inner loop run 100times, the outer loop as well. The initial value of count is 1.
29th Jun 2022, 7:54 PM
Lisa
Lisa - avatar
+ 2
Read my initial reply: The inner loop run 100times, the outer loop as well. The initial value of count is 1. You can see that count is increased by 1 each time.
29th Jun 2022, 8:13 PM
Lisa
Lisa - avatar
+ 1
Yeah I've read it, you said it's 100*100 meaning 100 X 100 right?, So since count is initially 1 that means 1000 gets added to it? Am I right sensei Lisa? But then a little bit something again, why is it 100*100 instead of 100 + 100? Thanks
29th Jun 2022, 8:20 PM
DevAbdul
DevAbdul - avatar
+ 1
Okay thanks allot for all the help Lisa, I certainly understand loops thoroughly so let me give the code another angry look and see😂 Thanks once again. Definitely gonna upvote ur answer
29th Jun 2022, 8:28 PM
DevAbdul
DevAbdul - avatar
0
I still don't understand Lisa, the result it gives is 100001 so how did the program arrive at that?
29th Jun 2022, 8:04 PM
DevAbdul
DevAbdul - avatar
0
This is exactly what I explained in my reply? And ut is 10001, not 100001, run the code to see it yourself.
29th Jun 2022, 8:08 PM
Lisa
Lisa - avatar
0
Actually you are using nested For loop which means Loog within loop. So here you are initialised an integer type variable "count" with 1. After that a for loop which is starting from 0 to 99 which mean outer loop will run 100 times but on every iteration of outer loop the inner loop will iterate 100 times which will add 1 every time to variable count. (count = count + 1).Once inner loop is ended the flow will go again to the outer loop untill the condition become false. So number of iteration will be 100*100+1
1st Jul 2022, 2:59 PM
Hussain Mehdi Birmani
Hussain Mehdi Birmani - avatar
- 1
I've already ran the code and saw the result, and you didn't explain how tf the program outputs 10001
29th Jun 2022, 8:11 PM
DevAbdul
DevAbdul - avatar