Debugging: My Unexpected Result of n1=75

  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Debugging
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 1K views
hokhani
Messages
610
Reaction score
22
The program below gives the result n1=75 wheras I expect to get n1=25. What is my mistake?

[C O D E]
clc
clear all
n1=0;
for n=1:5
for m=1:5
n1=n1+m;
end
end
n1
[/C O D E]
 
Physics news on Phys.org
Code:
clc
clear all
n1=0;
for n=1:5
    for m=1:5
        n1=n1+m;
    end
end
n1

The inner loop (for m=1:5) adds 15 to the value of n1. The outer loop (for n=1:5) tells it to do that 5 times. Therefore you get 75.
 
  • Like
Likes   Reactions: hokhani