Debugging: My Unexpected Result of n1=75

  • Thread starter hokhani
  • Start date
  • Tags
    Debugging
In summary, debugging is the process of identifying and fixing errors in code. If you receive an unexpected result, it could mean that there is an error in your code that needs to be carefully reviewed and fixed. You can use debugging tools and follow good coding practices to prevent unexpected results in the future. Hardware issues are not typically the cause of unexpected results while debugging. Some common mistakes that could lead to unexpected results include typos, incorrect syntax, incorrect variable assignments, and logical errors.
  • #1
hokhani
483
8
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
  • #2
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 hokhani
  • #3
If you want 25 you should do n1=n1+1 instead.
 
  • Like
Likes hokhani

1. What does it mean when I get an unexpected result of n1=75 while debugging?

This could mean that there is an error in your code that is causing this unexpected result. It is important to carefully review your code and check for any mistakes or logical errors that could be causing this issue.

2. How do I debug my code to fix the unexpected result of n1=75?

The first step is to carefully review your code and try to identify any errors or mistakes that could be causing the unexpected result. You can also use debugging tools such as breakpoints and console logs to track the flow of your code and identify any issues.

3. Can hardware issues cause unexpected results while debugging?

No, hardware issues are not typically the cause of unexpected results while debugging. Debugging is the process of identifying and fixing errors in code, so it is more likely that there is an error in the code itself.

4. What are some common mistakes that could lead to an unexpected result of n1=75 while debugging?

Some common mistakes that could lead to unexpected results while debugging include typos, incorrect syntax, incorrect variable assignments, and logical errors. It is important to carefully review your code for these types of mistakes.

5. How can I prevent unexpected results while debugging in the future?

To prevent unexpected results while debugging in the future, it is important to follow good coding practices such as commenting your code, testing your code frequently, and using debugging tools. It is also helpful to have a clear understanding of the programming language you are using and to carefully review your code for errors before running it.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
23K
Replies
12
Views
1K
  • Advanced Physics Homework Help
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Replies
11
Views
364
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • General Math
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
Back
Top