Is there a way to perform "if" check only once instead of every iteration in a loop?
Detach the first conditional.
\documentclass{article}
\newcount\zNonZero
\def\zLoop#1{%
\zNonZero=1
\zLoopStart{#1}%
\zLoopLoop{#1}%
}
\def\zLoopStart#1{%
\ifnum#1<1 % HOW TO CHECK THIS ONLY ONCE?
\zNonZero=0
\fi
}
\def\zLoopLoop#1{%
\ifnum#1<5
#1...(\the\zNonZero)\par
\zNonZero=1
\expandafter\zLoopLoop\expandafter{\the\numexpr#1+1\expandafter\relax\expandafter}%
\fi
}
\setlength{\parindent}{0pt}
\begin{document}
\zLoop{0}
\end{document}
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.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\zLoop}{m}
{
\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
\begin{document}
\zLoop{0}
\end{document}
\documentclass{article}
\begin{document}
\parindent0pt
\newcount\zNonZero
\newif\ifCheck \Checktrue
\def\zLoop#1{%
\zNonZero1
\ifCheck\ifnum#1<1 \zNonZero0 \fi\Checkfalse\fi
\ifnum#1<5
#1...(\the\zNonZero)\newline
\expandafter\zLoop\expandafter{\the\numexpr#1+1\relax}
\fi
\Checktrue
}
\zLoop{0}
\end{document}