If I have this layout
if (condition)
if (condition)
do_something
else
do_something1
else
do_something1
end
end
Is there a way that when I get to the first else, that it runs do_something1 without me having to retype the code for do_something1? So I'd like to jump from the first else, to the second one
If I understand your question correctly, you can replace:
if (condition1)
if (condition2)
do_something
else
do_something1
else
do_something1
end
end
with:
if (condition1 & condition2)
do_something
else
do_something1
end
You could use something like
if(condition && condition{
doSth.
}else{
doSth2
}
&& is a "and" Java Operator look for "and" Operator for Matlab, there is one for sure.
Related
I have the following if else statement that created by myself in order to link to the if else statement given in second part:
m=4
if m==3
disp(true)
else
disp(false)
Second part ( this code is fix cannot be change):
if (true)
A=Hello World
else
A=Bye
If using the first part code, my output will be
A=Hello World
but my desire output is
A=Bye
Anyone one have idea to edit the first part, because now my return value in first part not able to link to my second part.
If you can't change the second part's code, I'm afraid your desire cannot be fulfilled. Or rather, I'm afraid your code won't run at all, because your perenthesis, quotes, end-statement (and arguably semicolons) are not in place.
if true
A = 'Hello World';
else
A = 'Bye';
end
This code will return A = 'Hello World', no matter what, since true is always true. If-else conditions work like this:
if (*what's in here evealuates to true*)
%do stuff
else (*if what's up there does not evaluate to true*)
%do other stuff
Clearly, true will always evaluate to true. So the above if-else condition will always return A = 'Hello World'.
You don't need two if statements in order to accomplish this task. One is more than enough to perform all what you need:
m = 4;
if (m == 3)
A = 'Hello World';
else
A = 'Bye';
end
disp(A);
A few comments concerning your code:
if statements need to be closed with an end
if (true) will always pass into the statement
the disp function doesn't assign a value, its only goal is to display it in the Command Window
in order to work with text, you have to enclose it within single quotes ' (char array) or double quotes " (string), more info here
If you posted only small excerpts of your code and you need to perform those two checks sequentially, in different parts of your script, then:
m = 4;
if (m == 3)
m_equals_3 = true;
disp('M == 3');
else
m_equals_3 = false;
disp('M ~= 3');
end
% then, somewhere else...
if (m_equals_3)
A = 'Hello World';
else
A = 'Bye';
end
% ...
I guess this is a homework exercise. You should disclose that if it’s the case.
The exercise requieres you to change the workspace such that the second bit of code evaluated the else case. This can be accomplished by changing the meaning of true. In your first bit of code, make it so that
true = flase;
Or equivalently,
true = 0;
Note that this is really bad form, if you ever do something like this outside of a homework exercise that explicitly asks you to do so, you’ll get fired or maybe even shot. You’ve been warned!
By the way, I assume that the missing quote characters around the strings and the missing ends are typos?
if(Z1(m)<Z1(m-1)
IN=IN+1;
M1(:,IN)=Vb(:,t);
else
FCAST=Vb(N,T);
break;
end
I have this condition on my program, but the problem is it cannot work for first iteration because Z1(m)
Did anyone know how to solve this?
You can put the code you would like in the first iteration in it's own if statement
IF in == 1 || Z1(m)<Z1(m-1)
...
That might work.
I have a problem with writing a program in MATLAB. I need a goto-statement for solving my problem. Here is the code:
for i=1:n
1: newT=T(i)+unifrnd(-r,r);
newT=P(i)+unifrnd(-r,r);
if newT<Tmax && newT>Tmin && newP<Pmax && newP>Pmax
bestT=newT bestP=newP
else
go to 1
end
end
Is there any alternative for that goto-statement?
Is something like this what you're looking for?
for i=1:n
loop = true;
while loop
newT=T(i)+unifrnd(-r,r);
newT=P(i)+unifrnd(-r,r);
if newT<Tmax && newT>Tmin && newP<Pmax && newP>Pmax
bestT=newT bestP=newP
loop = false;
end
end
end
You can use this submission at MathWorks ® : https://www.mathworks.com/matlabcentral/fileexchange/26949-matlab-goto-statement
However, I recommend you to modify your code instead of using that!
you can try switch-case method. Please see example below;
caseno = input('input your case no');
switch (caseno)
case 1
disp('this first section will run');
case 2
disp('this second section will run');
otherwise
disp('wrong case no');
end
Is there a way to make condition such that if error comes in the statements/matrices under if then execute statements under else?
i.e .
if (some condition)
some statment1/matrix1/variable1
some statement2/matrix2/variable2
some statement3/matrix3/variable3
else newstatement/matrix %come to else part of the code if any of the statements 1,2 or 3 under if condition yields any error like dimension mismatch or anyother
end
No built-in mechanism to jump to else section. But you can use a construct like this:
condition_flag = (some condition);
error_flag = false;
if condition_flag
try
some statment1/matrix1/variable1
some statement2/matrix2/variable2
some statement3/matrix3/variable3
catch
error_flag = true;
end
end
if ~condition_flag || error_flag
%if any of the statements 1,2 or 3 under if condition yields any error like dimension mismatch or anyother
....
error_flag = false;
end
Alternative solution to the one of Mohsen Nostrania.
else_flag=true;
if (condition)
try
%% The if code
else_flag=false;
end
end
if else_flag
%% The else code
end
The else_flag=false; command is executed only when condition is true and no error occured within the <if code>.
Be aware that both solutions do literally what you want - the <if code> commands are executed until an error occur and then the <else code> is executed.
If one want to reduce tre number of if statements:
try
if statement
%% The if code
else
%% Obvious error
a(0);
end
catch
%% The else code
end
I'm wondering if there is a way to avoid repeating myself in the code below:
if (isfield(A,'test') && isempty(A.test)) || ~isfield(A,'test')
statement1
else
statement2
end
alternatively, this is equivalent to:
if isfield(A,'test')
if isempty(A.test)
statement1
else
statement2
else
statement1
end
In the first example, I've repeated the isfield condition and in the second statement1 is repeated. Is there a neat way to do this without repetition?
Thanks,
Rich
Simply
if isfield(A,'test') && ~isempty(A.test)
statement2
else
statement1
end
is enough.
Because using the operator && the statement isempty(A.test) is just tested if isfield(A,'test') is true, otherwise it is skipped anyway.
expr1 && expr2 represents a logical AND operation that employs
short-circuiting behavior. With short-circuiting, the second operand
expr2 is evaluated only when the result is not fully determined by the
first operand expr1. For example, if expr1 = 0, then the following
statement evaluates to false, regardless of the value of expr2.
Testing:
A = struct % Case1
%A.test = 5 % Case2
%A = 5 % Case3
if isfield(A,'test')
if isempty(A.test)
disp(1)
else
disp(2)
end
else
disp(1)
end
if isfield(A,'test') && ~isempty(A.test)
disp(2)
else
disp(1)
end
For all 3 testing cases the results are the same.
I think either
if ~isfield(A,'test') || isempty(A.test))
statement1
else
statement2
end
or
if isfield(A,'test') && isempty(A.test)
statement2
else
statement1
end
will do what you need.
MATLAB's || and && operators short-circuit, so if the first operands evaluate to true (in the first case) or false (in the second case), the second operand is not evaluated and won't cause an error.
Don't think there's any other way, other than storing the condition in a boolean variable and passing it on, like this -
cond1 = isfield(A,'test');
if (cond1 && isempty(A.test) || ~cond1)
statement1
else
statement2
end
Though I must add, I would rather prefer the IF-ELSE style that you have adopted in your second approach. The case when 'test' field doesn't exist for A, it would throw error if you do only isempty(A.test), but with the double checking of that alongwith isfield(A,'test'), I think MATLAB ignores the error.