Skip to content

5.11 Loop else

Loop else construct

  • A loop may optionally include an else clause that executes only if the loop terminates normally, not using a break statement.

The complete forms of while and for loops are:

5%2011%20Loop%20else%203d08bcc842d94d6994a43090e50127af/Untitled.png

5%2011%20Loop%20else%203d08bcc842d94d6994a43090e50127af/Untitled%201.png

  • The loop else construct executes if the loop completes normally.
  • Example: a special message "All names printed" is displayed if the entire list of names is completely iterated through:

5%2011%20Loop%20else%203d08bcc842d94d6994a43090e50127af/Untitled%202.png