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









share|improve this question



















  • 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














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









share|improve this question



















  • 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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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





    share|improve this answer
























      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





      share|improve this answer






















        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 23:48









        beaker

        12.6k22139




        12.6k22139



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto