Is there a way to perform “if” check only once instead of every iteration in a loop?biblatex-dw: Conditional formatting – multiple vs. single citationsDefine command that returns true/false (multiple conditions)Is there a better way to check divisibility in LaTeX?Numerical conditional within tikz keys?

How to check the quality of an audio sample?

Is killing off one of my queer characters homophobic?

What is temperature on a quantum level

Why would guns not work in the dungeon?

How to query contacts with no cases, opportunities etc

The monorail explodes before I can get on it

How do Windows version numbers work?

How the name "craqueuhhe" is read

Why does the autopilot disengage even when it does not receive pilot input?

affect vs effect

If a specific mass of air is polluted, will the pollution stick with it?

How do I determine whether a permit is required for a new gas line?

Was adding milk to tea started to reduce employee tea break time?

I quit, and boss offered me 3 month "grace period" where I could still come back

Steampunk helicopter

During copyediting, journal disagrees about spelling of paper's main topic

Why do players in the past play much longer tournaments than today's top players?

What's the fastest way to get Hard To Borrow (HTB) stocks?

Did any of the founding fathers anticipate Lysander Spooner's criticism of the constitution?

Is it rude to tell recruiters I would only change jobs for a better salary?

What would the EU do if an EU member declared war on another EU member?

Supporting developers who insist on using their pet language

Dropping outliers based on "2.5 times the RMSE"

Why does resistance reduce when a conductive fabric is stretched?



Is there a way to perform “if” check only once instead of every iteration in a loop?


biblatex-dw: Conditional formatting – multiple vs. single citationsDefine command that returns true/false (multiple conditions)Is there a better way to check divisibility in LaTeX?Numerical conditional within tikz keys?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















The zNonZero is equal to 0 if #1 is 0, but is equal to 1 if #1 is greater than 0. The problem is, the if check inside the loop is performed every iteration: I need it to be performed only once (for the first iteration). Is there a way to get rid of unneeded checks?



begindocument
parindent0pt
newcountzNonZero
defzLoop#1%
zNonZero1
ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
zNonZero0
fi
ifnum#1<5
#1...(thezNonZero)newline
expandafterzLoopexpandafterthenumexpr#1+1relax
fi

zLoop0
enddocument


The "if" check is done 5 times here, but we need it to be done only once:












share|improve this question

















  • 1





    Is this your real code or is your actual test more expensive? I don't think this simple test will have any performance impact even for a larger number of iterations.

    – siracusa
    8 hours ago











  • You could create two separate functions. One that processes the first iteration and does the "if" check, and something that processes subsequent iterations.

    – Werner
    8 hours ago

















5















The zNonZero is equal to 0 if #1 is 0, but is equal to 1 if #1 is greater than 0. The problem is, the if check inside the loop is performed every iteration: I need it to be performed only once (for the first iteration). Is there a way to get rid of unneeded checks?



begindocument
parindent0pt
newcountzNonZero
defzLoop#1%
zNonZero1
ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
zNonZero0
fi
ifnum#1<5
#1...(thezNonZero)newline
expandafterzLoopexpandafterthenumexpr#1+1relax
fi

zLoop0
enddocument


The "if" check is done 5 times here, but we need it to be done only once:












share|improve this question

















  • 1





    Is this your real code or is your actual test more expensive? I don't think this simple test will have any performance impact even for a larger number of iterations.

    – siracusa
    8 hours ago











  • You could create two separate functions. One that processes the first iteration and does the "if" check, and something that processes subsequent iterations.

    – Werner
    8 hours ago













5












5








5








The zNonZero is equal to 0 if #1 is 0, but is equal to 1 if #1 is greater than 0. The problem is, the if check inside the loop is performed every iteration: I need it to be performed only once (for the first iteration). Is there a way to get rid of unneeded checks?



begindocument
parindent0pt
newcountzNonZero
defzLoop#1%
zNonZero1
ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
zNonZero0
fi
ifnum#1<5
#1...(thezNonZero)newline
expandafterzLoopexpandafterthenumexpr#1+1relax
fi

zLoop0
enddocument


The "if" check is done 5 times here, but we need it to be done only once:












share|improve this question














The zNonZero is equal to 0 if #1 is 0, but is equal to 1 if #1 is greater than 0. The problem is, the if check inside the loop is performed every iteration: I need it to be performed only once (for the first iteration). Is there a way to get rid of unneeded checks?



begindocument
parindent0pt
newcountzNonZero
defzLoop#1%
zNonZero1
ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
zNonZero0
fi
ifnum#1<5
#1...(thezNonZero)newline
expandafterzLoopexpandafterthenumexpr#1+1relax
fi

zLoop0
enddocument


The "if" check is done 5 times here, but we need it to be done only once:









conditionals






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









bp2017bp2017

1,4223 silver badges16 bronze badges




1,4223 silver badges16 bronze badges







  • 1





    Is this your real code or is your actual test more expensive? I don't think this simple test will have any performance impact even for a larger number of iterations.

    – siracusa
    8 hours ago











  • You could create two separate functions. One that processes the first iteration and does the "if" check, and something that processes subsequent iterations.

    – Werner
    8 hours ago












  • 1





    Is this your real code or is your actual test more expensive? I don't think this simple test will have any performance impact even for a larger number of iterations.

    – siracusa
    8 hours ago











  • You could create two separate functions. One that processes the first iteration and does the "if" check, and something that processes subsequent iterations.

    – Werner
    8 hours ago







1




1





Is this your real code or is your actual test more expensive? I don't think this simple test will have any performance impact even for a larger number of iterations.

– siracusa
8 hours ago





Is this your real code or is your actual test more expensive? I don't think this simple test will have any performance impact even for a larger number of iterations.

– siracusa
8 hours ago













You could create two separate functions. One that processes the first iteration and does the "if" check, and something that processes subsequent iterations.

– Werner
8 hours ago





You could create two separate functions. One that processes the first iteration and does the "if" check, and something that processes subsequent iterations.

– Werner
8 hours ago










2 Answers
2






active

oldest

votes


















5














Detach the first conditional.



documentclassarticle

newcountzNonZero

defzLoop#1%
zNonZero=1
zLoopStart#1%
zLoopLoop#1%

defzLoopStart#1%
ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
zNonZero=0
fi

defzLoopLoop#1%
ifnum#1<5
#1...(thezNonZero)par
zNonZero=1
expandafterzLoopLoopexpandafterthenumexpr#1+1expandafterrelaxexpandafter%
fi


setlengthparindent0pt

begindocument

zLoop0

enddocument


Note the two more expandafter tokens so the entire conditional is removed before the loop is restarted.



This possibly doesn't qualify, but I'll mention it anyway.



documentclassarticle
usepackagexparse

ExplSyntaxOn

NewExpandableDocumentCommandzLoopm

bp_zloop:n #1

cs_new:Nn bp_zloop:n

int_compare:nT #1 < 5

#1...(int_eval:n int_min:nn #1 1 )par
bp_zloop:f int_eval:n #1 + 1


cs_generate_variant:Nn bp_zloop:n f

ExplSyntaxOff

begindocument

zLoop0

enddocument





share|improve this answer
































    0














    documentclassarticle 
    begindocument
    parindent0pt
    newcountzNonZero
    newififCheck Checktrue
    defzLoop#1%
    zNonZero1
    ifCheckifnum#1<1 zNonZero0 fiCheckfalsefi
    ifnum#1<5
    #1...(thezNonZero)newline
    expandafterzLoopexpandafterthenumexpr#1+1relax
    fi
    Checktrue

    zLoop0
    enddocument





    share|improve this answer























    • ...this still performs a conditional check ifCheck with every call of zLoop.

      – Werner
      7 hours ago













    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "85"
    ;
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2ftex.stackexchange.com%2fquestions%2f499693%2fis-there-a-way-to-perform-if-check-only-once-instead-of-every-iteration-in-a-l%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    Detach the first conditional.



    documentclassarticle

    newcountzNonZero

    defzLoop#1%
    zNonZero=1
    zLoopStart#1%
    zLoopLoop#1%

    defzLoopStart#1%
    ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
    zNonZero=0
    fi

    defzLoopLoop#1%
    ifnum#1<5
    #1...(thezNonZero)par
    zNonZero=1
    expandafterzLoopLoopexpandafterthenumexpr#1+1expandafterrelaxexpandafter%
    fi


    setlengthparindent0pt

    begindocument

    zLoop0

    enddocument


    Note the two more expandafter tokens so the entire conditional is removed before the loop is restarted.



    This possibly doesn't qualify, but I'll mention it anyway.



    documentclassarticle
    usepackagexparse

    ExplSyntaxOn

    NewExpandableDocumentCommandzLoopm

    bp_zloop:n #1

    cs_new:Nn bp_zloop:n

    int_compare:nT #1 < 5

    #1...(int_eval:n int_min:nn #1 1 )par
    bp_zloop:f int_eval:n #1 + 1


    cs_generate_variant:Nn bp_zloop:n f

    ExplSyntaxOff

    begindocument

    zLoop0

    enddocument





    share|improve this answer





























      5














      Detach the first conditional.



      documentclassarticle

      newcountzNonZero

      defzLoop#1%
      zNonZero=1
      zLoopStart#1%
      zLoopLoop#1%

      defzLoopStart#1%
      ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
      zNonZero=0
      fi

      defzLoopLoop#1%
      ifnum#1<5
      #1...(thezNonZero)par
      zNonZero=1
      expandafterzLoopLoopexpandafterthenumexpr#1+1expandafterrelaxexpandafter%
      fi


      setlengthparindent0pt

      begindocument

      zLoop0

      enddocument


      Note the two more expandafter tokens so the entire conditional is removed before the loop is restarted.



      This possibly doesn't qualify, but I'll mention it anyway.



      documentclassarticle
      usepackagexparse

      ExplSyntaxOn

      NewExpandableDocumentCommandzLoopm

      bp_zloop:n #1

      cs_new:Nn bp_zloop:n

      int_compare:nT #1 < 5

      #1...(int_eval:n int_min:nn #1 1 )par
      bp_zloop:f int_eval:n #1 + 1


      cs_generate_variant:Nn bp_zloop:n f

      ExplSyntaxOff

      begindocument

      zLoop0

      enddocument





      share|improve this answer



























        5












        5








        5







        Detach the first conditional.



        documentclassarticle

        newcountzNonZero

        defzLoop#1%
        zNonZero=1
        zLoopStart#1%
        zLoopLoop#1%

        defzLoopStart#1%
        ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
        zNonZero=0
        fi

        defzLoopLoop#1%
        ifnum#1<5
        #1...(thezNonZero)par
        zNonZero=1
        expandafterzLoopLoopexpandafterthenumexpr#1+1expandafterrelaxexpandafter%
        fi


        setlengthparindent0pt

        begindocument

        zLoop0

        enddocument


        Note the two more expandafter tokens so the entire conditional is removed before the loop is restarted.



        This possibly doesn't qualify, but I'll mention it anyway.



        documentclassarticle
        usepackagexparse

        ExplSyntaxOn

        NewExpandableDocumentCommandzLoopm

        bp_zloop:n #1

        cs_new:Nn bp_zloop:n

        int_compare:nT #1 < 5

        #1...(int_eval:n int_min:nn #1 1 )par
        bp_zloop:f int_eval:n #1 + 1


        cs_generate_variant:Nn bp_zloop:n f

        ExplSyntaxOff

        begindocument

        zLoop0

        enddocument





        share|improve this answer















        Detach the first conditional.



        documentclassarticle

        newcountzNonZero

        defzLoop#1%
        zNonZero=1
        zLoopStart#1%
        zLoopLoop#1%

        defzLoopStart#1%
        ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
        zNonZero=0
        fi

        defzLoopLoop#1%
        ifnum#1<5
        #1...(thezNonZero)par
        zNonZero=1
        expandafterzLoopLoopexpandafterthenumexpr#1+1expandafterrelaxexpandafter%
        fi


        setlengthparindent0pt

        begindocument

        zLoop0

        enddocument


        Note the two more expandafter tokens so the entire conditional is removed before the loop is restarted.



        This possibly doesn't qualify, but I'll mention it anyway.



        documentclassarticle
        usepackagexparse

        ExplSyntaxOn

        NewExpandableDocumentCommandzLoopm

        bp_zloop:n #1

        cs_new:Nn bp_zloop:n

        int_compare:nT #1 < 5

        #1...(int_eval:n int_min:nn #1 1 )par
        bp_zloop:f int_eval:n #1 + 1


        cs_generate_variant:Nn bp_zloop:n f

        ExplSyntaxOff

        begindocument

        zLoop0

        enddocument






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 6 hours ago

























        answered 6 hours ago









        egregegreg

        754k90 gold badges1977 silver badges3317 bronze badges




        754k90 gold badges1977 silver badges3317 bronze badges























            0














            documentclassarticle 
            begindocument
            parindent0pt
            newcountzNonZero
            newififCheck Checktrue
            defzLoop#1%
            zNonZero1
            ifCheckifnum#1<1 zNonZero0 fiCheckfalsefi
            ifnum#1<5
            #1...(thezNonZero)newline
            expandafterzLoopexpandafterthenumexpr#1+1relax
            fi
            Checktrue

            zLoop0
            enddocument





            share|improve this answer























            • ...this still performs a conditional check ifCheck with every call of zLoop.

              – Werner
              7 hours ago















            0














            documentclassarticle 
            begindocument
            parindent0pt
            newcountzNonZero
            newififCheck Checktrue
            defzLoop#1%
            zNonZero1
            ifCheckifnum#1<1 zNonZero0 fiCheckfalsefi
            ifnum#1<5
            #1...(thezNonZero)newline
            expandafterzLoopexpandafterthenumexpr#1+1relax
            fi
            Checktrue

            zLoop0
            enddocument





            share|improve this answer























            • ...this still performs a conditional check ifCheck with every call of zLoop.

              – Werner
              7 hours ago













            0












            0








            0







            documentclassarticle 
            begindocument
            parindent0pt
            newcountzNonZero
            newififCheck Checktrue
            defzLoop#1%
            zNonZero1
            ifCheckifnum#1<1 zNonZero0 fiCheckfalsefi
            ifnum#1<5
            #1...(thezNonZero)newline
            expandafterzLoopexpandafterthenumexpr#1+1relax
            fi
            Checktrue

            zLoop0
            enddocument





            share|improve this answer













            documentclassarticle 
            begindocument
            parindent0pt
            newcountzNonZero
            newififCheck Checktrue
            defzLoop#1%
            zNonZero1
            ifCheckifnum#1<1 zNonZero0 fiCheckfalsefi
            ifnum#1<5
            #1...(thezNonZero)newline
            expandafterzLoopexpandafterthenumexpr#1+1relax
            fi
            Checktrue

            zLoop0
            enddocument






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 8 hours ago









            Red-CloudRed-Cloud

            4,6042 silver badges17 bronze badges




            4,6042 silver badges17 bronze badges












            • ...this still performs a conditional check ifCheck with every call of zLoop.

              – Werner
              7 hours ago

















            • ...this still performs a conditional check ifCheck with every call of zLoop.

              – Werner
              7 hours ago
















            ...this still performs a conditional check ifCheck with every call of zLoop.

            – Werner
            7 hours ago





            ...this still performs a conditional check ifCheck with every call of zLoop.

            – Werner
            7 hours ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f499693%2fis-there-a-way-to-perform-if-check-only-once-instead-of-every-iteration-in-a-l%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







            Popular posts from this blog

            ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

            Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

            19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу