problem in usong if statement

16 views (last 30 days)
nevin joseph
nevin joseph on 17 Jul 2012
if( V2>V1 && V1>V3)
{
if(I2>I3)
disp('RANGE IS 60-90');
if(I3>I2)
disp('RANGE IS 240-270');
}
am getting an error of using if inside another if....how can i resolve this problem
  1 Comment
Jan
Jan on 17 Jul 2012
The usage of the IF statement is explained in the documentation exhaustively. Please take the time to read "help if" and "doc if". It is strongly recommended to read the "Getting Started" chapters also to learn the absolute basics.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 17 Jul 2012
That is not valid MATLAB code. MATLAB does not use "{" or "}", and each "if" must be matched with an "end" statement.
Sample:
if condition1
if condition2
do something
end
end

Kevin Claytor
Kevin Claytor on 17 Jul 2012
You cannot use the brackets {} to group, and use the keyword 'end' to close if and for statements. If you absolutely must have it on one line, this should work;
if( V2>V1 && V1>V3); if(I2>I3); disp('RANGE IS 60-90'); elseif(I3>I2) disp('RANGE IS 240-270'); end; end;
Otherwise, I find using new lines and proper indentation (you can auto indent with ctrl-i) makes things much clearer;
if( V2>V1 && V1>V3)
if(I2>I3)
disp('RANGE IS 60-90');
elseif(I3>I2)
disp('RANGE IS 240-270');
end;
end;

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!