Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
int main()
{
    int a,b,c;
    a=2;
    for (b=2; b<5; b++)
        for (c=1; c<5; c+=2)
            a*=3;
    a=a*b+c;
    cout<<a++;
}


What I have tried:

I got the answer, but I don't know why it is calculating 5 too because in for loop, b and c are less than 5.
Posted
Updated 27-Oct-23 4:55am
v3
Comments
CPallini 27-Oct-23 4:47am    
Have a look at:
https://www.javatpoint.com/for-loop-in-c
Dave Kreskowiak 27-Oct-23 10:06am    
Stepping through the code in the debugger, executing it line by line and hovering the mouse over the variable names to see their values, would have shown you what's going on and how the code really works.

The debugger is there to debug YOU, not the code. It helps you understand exactly what the code is doing.

The answer is the result of repeatedly mutiplying the variable a by 3, and finally by 5, and then adding 5. If you repeat those steps using pen and paper you will see why. Alternatively you could add some more cout statements to see all the intervening values of a.
 
Share this answer
 
Comments
work business 27-Oct-23 4:38am    
but my question is why it is calculate 5 while in for loop it is : b<5 and c< 5. it means only till 4 it must calculate.
Richard MacCutchan 27-Oct-23 4:55am    
And that is exactly what it does.
Start: b equals 2, and c = 1, a = 2: a becomes 2 * 3 = 6
Next: b = 2, c = 3 (1 + 2) : a = 6 * 3 = 18
Next: b = 2, c = 5 (3 + 2) : End of inner loop
Next: b = 3 (2 + 1), c = 1 : a = 18 * 3 = 54
Next: b = 3, c = 3 (1 + 2) : a = 54 * 3 = 162
Next: b = 3, c = 5 (3 + 2) : End of inner loop
Next: b = 4 (3 + 1), c = 1 : a = 162 * 3 = 486
Next: b = 4, c = 3 (1 + 2) : a = 486 * 3 = 1458
Next: c = 5 (3 + 2) : End of inner loop
Next: b = 5 (4 + 1) : End of outer loop
a = 1458 * 5 + 5 = 7295

Simples!
work business 27-Oct-23 5:10am    
I 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
Richard MacCutchan 27-Oct-23 5:15am    
Yes, the loops both end when their variable reaches the value of 5. For as long as the value is less than 5 the loop will continue. For example when b = 4, it is less than 5, so the following code will be executed. When it reaches 5 (on the next time round) it is no longer less than 5 so the loop terminates.
jeron1 27-Oct-23 10:18am    
"If you repeat those steps using pen and paper you will see why"
Wise words indeed. :thumbsup:
That's just how for loops work. If the loop variable is visible outside of the loop, then its value after the loop has finished will be whatever value it had when it no longer matched the loop condition.

In this case, both variables will be set to 5 when the loop has finished.

The loops are equivalent to:
C++
b = 2;
while (b < 5) {
    c = 1;
    while (c < 5) {
        a *= 3;
        c += 2;
    }
    
    // c will be 5 here...
    
    b++;
}

// b will be 5 here...
 
Share this answer
 
Comments
CPallini 27-Oct-23 4:45am    
5.
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:
C++
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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900