Matlab if else loop
up vote
-1
down vote
favorite
Welcome I want to check if number is even , non even or not integer and I don't know how to check last case. My code:
disp('check number');
x = input('give number = ');
if mod(x,2)== 0
disp(' even number');
elseif mod(x,2)~= 0
disp(' not even number');
else mod(x,2)== float
disp('non integer');
end
matlab
add a comment |
up vote
-1
down vote
favorite
Welcome I want to check if number is even , non even or not integer and I don't know how to check last case. My code:
disp('check number');
x = input('give number = ');
if mod(x,2)== 0
disp(' even number');
elseif mod(x,2)~= 0
disp(' not even number');
else mod(x,2)== float
disp('non integer');
end
matlab
1
if else
is not a loop.
– Scott Hunter
Nov 10 at 23:10
can u help with this task?
– tomczas
Nov 10 at 23:11
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Welcome I want to check if number is even , non even or not integer and I don't know how to check last case. My code:
disp('check number');
x = input('give number = ');
if mod(x,2)== 0
disp(' even number');
elseif mod(x,2)~= 0
disp(' not even number');
else mod(x,2)== float
disp('non integer');
end
matlab
Welcome I want to check if number is even , non even or not integer and I don't know how to check last case. My code:
disp('check number');
x = input('give number = ');
if mod(x,2)== 0
disp(' even number');
elseif mod(x,2)~= 0
disp(' not even number');
else mod(x,2)== float
disp('non integer');
end
matlab
matlab
edited Nov 11 at 1:48
Banghua Zhao
800217
800217
asked Nov 10 at 23:09
tomczas
76110
76110
1
if else
is not a loop.
– Scott Hunter
Nov 10 at 23:10
can u help with this task?
– tomczas
Nov 10 at 23:11
add a comment |
1
if else
is not a loop.
– Scott Hunter
Nov 10 at 23:10
can u help with this task?
– tomczas
Nov 10 at 23:11
1
1
if else
is not a loop.– Scott Hunter
Nov 10 at 23:10
if else
is not a loop.– Scott Hunter
Nov 10 at 23:10
can u help with this task?
– tomczas
Nov 10 at 23:11
can u help with this task?
– tomczas
Nov 10 at 23:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The else
clause doesn't take a conditional expression, so in order to use it we need to make sure that all integers are handled before we get there. Fortunately, if we catch all even integers and all odd integers, anything left is not an integer.
The if
clause looks good, if mod(x,2) == 0
, then it's even, so let's keep that. For the elseif
part, for all integers, mod(x,1) == 1
. Normally this would catch both odd and even integers, but since we've already handled all of the even integers in the if
clause, we can safely assume that any integers that get here are odd. Anything that makes it past these two conditions must be a non-integer.
disp('check number');
x = input('give number = ');
if mod(x,2) == 0
disp(' even number');
elseif mod(x,1) == 0
disp(' not even number');
else
disp('non integer');
end
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The else
clause doesn't take a conditional expression, so in order to use it we need to make sure that all integers are handled before we get there. Fortunately, if we catch all even integers and all odd integers, anything left is not an integer.
The if
clause looks good, if mod(x,2) == 0
, then it's even, so let's keep that. For the elseif
part, for all integers, mod(x,1) == 1
. Normally this would catch both odd and even integers, but since we've already handled all of the even integers in the if
clause, we can safely assume that any integers that get here are odd. Anything that makes it past these two conditions must be a non-integer.
disp('check number');
x = input('give number = ');
if mod(x,2) == 0
disp(' even number');
elseif mod(x,1) == 0
disp(' not even number');
else
disp('non integer');
end
add a comment |
up vote
2
down vote
accepted
The else
clause doesn't take a conditional expression, so in order to use it we need to make sure that all integers are handled before we get there. Fortunately, if we catch all even integers and all odd integers, anything left is not an integer.
The if
clause looks good, if mod(x,2) == 0
, then it's even, so let's keep that. For the elseif
part, for all integers, mod(x,1) == 1
. Normally this would catch both odd and even integers, but since we've already handled all of the even integers in the if
clause, we can safely assume that any integers that get here are odd. Anything that makes it past these two conditions must be a non-integer.
disp('check number');
x = input('give number = ');
if mod(x,2) == 0
disp(' even number');
elseif mod(x,1) == 0
disp(' not even number');
else
disp('non integer');
end
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The else
clause doesn't take a conditional expression, so in order to use it we need to make sure that all integers are handled before we get there. Fortunately, if we catch all even integers and all odd integers, anything left is not an integer.
The if
clause looks good, if mod(x,2) == 0
, then it's even, so let's keep that. For the elseif
part, for all integers, mod(x,1) == 1
. Normally this would catch both odd and even integers, but since we've already handled all of the even integers in the if
clause, we can safely assume that any integers that get here are odd. Anything that makes it past these two conditions must be a non-integer.
disp('check number');
x = input('give number = ');
if mod(x,2) == 0
disp(' even number');
elseif mod(x,1) == 0
disp(' not even number');
else
disp('non integer');
end
The else
clause doesn't take a conditional expression, so in order to use it we need to make sure that all integers are handled before we get there. Fortunately, if we catch all even integers and all odd integers, anything left is not an integer.
The if
clause looks good, if mod(x,2) == 0
, then it's even, so let's keep that. For the elseif
part, for all integers, mod(x,1) == 1
. Normally this would catch both odd and even integers, but since we've already handled all of the even integers in the if
clause, we can safely assume that any integers that get here are odd. Anything that makes it past these two conditions must be a non-integer.
disp('check number');
x = input('give number = ');
if mod(x,2) == 0
disp(' even number');
elseif mod(x,1) == 0
disp(' not even number');
else
disp('non integer');
end
answered Nov 10 at 23:48
beaker
12.6k22139
12.6k22139
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244313%2fmatlab-if-else-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
if else
is not a loop.– Scott Hunter
Nov 10 at 23:10
can u help with this task?
– tomczas
Nov 10 at 23:11