why am I having zero as the answer in the code below?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
andrew brown
am 9 Mär. 2021
Kommentiert: andrew brown
am 9 Mär. 2021
why am I having zero as the answer in the code below?
a = 0
for i = 1:0.5:2
for j = 1:1:3
a = a + j;
end
end
0 Kommentare
Akzeptierte Antwort
Nathanael Jenkins
am 9 Mär. 2021
The only output from that code will be where you define a at the start of the program (without a semicolon at the end).
Your code is calculating correctly, but it's not displaying the final value of a.
You need to add a line after the loops to print the final value of a:
a = 0;
for i = 1:0.5:2
for j = 1:1:3
a = a + j;
end
end
disp(a)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!