Loops: For or While?
조회 수: 4 (최근 30일)
이전 댓글 표시
What loop would work best to write and complete the bullets below?
- Initializes a variable number=200;
- Initializes a variable count=0;
- Subtracts an integer of your choice from number to get the new number during each step of the loop, until number is less than 10.
- Updates count to keep track of how many steps are needed.
댓글 수: 0
채택된 답변
Jan
2021년 4월 15일
편집: Jan
2021년 4월 15일
If the number of iterations is known in advance, use a FOR loop.
If the number of iterations is not known in advance, but determines by dynamic calculations, use a WHILE loop.
"until number is less than 10" sounds clearly like the number of iterations is determined dynamically.
댓글 수: 0
추가 답변 (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!