using for loop how can i print values between 1 to 10 and 15 to 25 and 27 using one for loop?
조회 수: 19 (최근 30일)
이전 댓글 표시
if I want to print number from 1 to 10 I will use for loop; for i= 1:10 but if I want to print number from 1 to 10 then from 15 to 25 and then 27 what for loop should I use to print the specified values?
댓글 수: 0
답변 (3개)
Guillaume
2016년 4월 14일
When you write 1:10, you are in effect passing the vector [1 2 3 4 5 6 7 8 9 10] to the for loop. You can pass whatever vector you want created anyway you want to the loop. E.g.:
for iter = [1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20 21 22 23 24 25 27] %the hard way
for iter = [1:10, 15:25, 27] %the easier way
for iter = setdiff(1:27, [11:24, 26]) %the convoluted way
댓글 수: 0
AKOMONEH Raymond Anumaneh
2021년 2월 11일
for iter = [1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20 21 22 23 24 25 27]
댓글 수: 0
MUHAMMAD Ateeb
2021년 4월 4일
Question 1: Write a program that asks the user to enter a number from 1 to 100. If the input number is less than 1 or greater than 100 display the message “INVALID INPUT”. If the number is even then then the program must display the table of 2. If the number is odd then the program must display the table of 5. For both cases use for loop to write the code
댓글 수: 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!