How does the following loop turn out to be different from the expected 25?
조회 수: 1 (최근 30일)
이전 댓글 표시
s=0;
I=5;
for i=1:I
for j=1:i
s=s+1
end
end
value(s)
댓글 수: 0
답변 (1개)
Walter Roberson
2022년 11월 18일
Your inner loop is j=1:i . The first time that is j=1:1 . The second time it is j=1:2 . The third time it is j=1:3, then 1:4, then 1:5 . So the inner loop is going to run 1+2+3+4+5 = 15 times.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!