Only constructs like if-statements and if-expressions can be used to guard expressions against evaluation since the bodies of these constructs are only evaluated if the corresponding condition is true.
The equation is correct since the use of an if-expression guarantees that the condition (y>=1 and y<=n) is always evaluated before then or else-parts of the if-expression.
Boolean operators such as and cannot be used for evaluation protection. The equation in the class GuardTestInvalid below can generate an error such as index out of bounds (if y is not in the interval 1 to 5), see the simulation below. Even if the boolean expression (y>=1 and y<=n) evaluates to false, the evaluation of v[y] is not necessarily prevented.
One problem with the iterative zero-crossing search is that if-expressions in Modelica do not behave exactly as most people expect based on experience with conventional programming languages. During this iterative process small negative values of h can actually be supplied to the body of the if-expression, e.g. -c*sqrt(h) below, causing an attempt to compute the square root of a negative number, which is an error.
The NoEvent operator is used to avoid events from being generated by the expression it is applied to. The operator should be used if the expression is continuous during a discrete switch.
The simulation shows that the value of h decreases and at t=2 is becomes a small negative number.