필터 지우기
필터 지우기

how to tell skip few lines in a code

조회 수: 72 (최근 30일)
Vasudeo Samant
Vasudeo Samant 2019년 1월 30일
답변: Anant Upadhyay 2019년 2월 7일
How to program in matab if I want to skip few lines in the code or after executing a loop I want to go to a particular line in a code?
  댓글 수: 2
KSSV
KSSV 2019년 1월 30일
Write functions.....
Rik
Rik 2019년 1월 30일
For skipping lines under a certain condition, you can also use an if statement.

댓글을 달려면 로그인하십시오.

답변 (1개)

Anant Upadhyay
Anant Upadhyay 2019년 2월 7일
It is my understanding that you want to go to some particular line of code. However, MATLAB does not support "C style goto statements". If you wish to skip some particular lines of code, you can use "continue" function in MATLAB, or, if you wish to go to a particular line in a code after executing a loop, you can use "if" condition.
Here is a sample code:
res = 0;
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for i=1:9
rem = mod(arr(i), 2);
% Checks if element of array "arr" is odd, then it do not add the element to "res" variable.
if rem == 1
continue;
end
res = res + arr(i);
end
% After loop end, checks if "res" is greater than 30 or not, to goto specific line of code.
if(res > 30)
res = res - 5;
else
res = res + 5;
end
Displays the value of res
disp(res);
For more information on "if" statement, checkout the following documentation:
For more information on " continue " statement, checkout the following documentation:

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by