while_loop#

ivy.while_loop(test_fn, body_fn, vars)[source]#

Take a test function, a body function and a set of variables as input. The body function is executed repeatedly while the test function returns True.

Parameters:
  • test_fn (Callable) – A callable function that returns a boolean value representing whether the loop should continue.

  • body_fn (Callable) – A callable function to be executed repeatedly while the test function returns True.

  • vars (Dict[str, Union[Array, NativeArray]]) – Additional variables to be passed to the functions.

Return type:

Any

Returns:

ret – The final result of executing the body function.

Examples

>>> i = 0
>>> test_fn = lambda i: i < 3
>>> body_fn = lambda i: i + 1
>>> result = ivy.while_loop(test_fn, body_fn, vars= (i,))
>>> print(result)
(3,)
>>> i = 0
>>> j = 1
>>> test_fn = lambda i, j: i < 3
>>> body_fn = lambda i, j: (i + 1, j * 2)
>>> vars = (i, j)
>>> result = ivy.while_loop(test_fn, body_fn, vars=vars)
>>> print(result)
(3, 8)