Quote:
understand the answer, it is simple. but why in your answer u end inner and outer loop with 5, it should not end with 4? because it is b<5, it is not b<=5
Think about how a simple
for
loop works:
for (int i = 0; i < 2; i++)
cout << i;
1) The loop starts, and sets <
i
to zero.
2) It thens compares i and 2.
3) If it is less than two, enter the loop body.
4) If it is not less than (i.e. greater than or equal to) exit the loop.
5) Execute the code in the loop body once. (The value of
i
is printed)
6) Increment
i
by one.
7) Go back to the loop test at (2).
It' pretty obvious that the only way out of the loop is when the comparison at (4) is true: the value in
i
is greater than or equal to 2. Since you increment
by one each time you go round, it will always reach 2 before it reaches any higher number - so after the loop, you are guaranteed that
i
will contain 2.
Your code is the same: you do the same test for "go round the loop again": is
b
or
c
less than a fixed value - 5. Since again you add one to
b
each time, it will only exit the loop when
b
is no longer less than 5, which means when
b
is equal to 5.
Make sense?