What languages ​​does the while-else type management structure have and how does it work?

Once upon a time, it seemed to me that I saw a sentence to add a sentence elseto loops in foreither whileC or C ++ ... or something like that. I don’t remember how this should have worked - was the sentence elselaunched if the loop exited normally, but not through the break statement?

In any case, it's hard to find, so I thought that maybe I could get answers to CW here for different languages.

What languages ​​support adding a sentence elseto something other than an operator if? What is the meaning of this article? One language for one answer.

+3
source share
3 answers

Python .

Usage example:

for element in container:
  if element == target:
    break
else:
  # this will not be executed if the loop is quit with break.
  raise ElementNotFoundError()

From Python Docs :

is executed when the loop ends with the exhaustion of list (with for) or when the condition becomes false (for now), but not when the loop ends with a break statement.

+8
source

There is a so-called "Dijkstra Guarded Loop". It has been defined in Secure Command Language (GCP) . You can find some information about this syntax and semantics in the Wikipedia article above in section 6 Repetition: do .

, . Oberon-07 (PDF, 70 KB). "Dijkstra Loop" while. 9.6. PDF.

WHILE m > n DO m := m – n 
ELSIF n > m DO n := n – m 
END
+3

, Python, Oberon - , . C , "" "elsewhile", , . , , ?

, :

if (condition) {
   do {
       condition = update(something);
   } while (condition);  
} else {
   loop_never_taken(something);
}

:

while (condition) {
    condition = update(something);
} otherwhile {
    loop_never_taken(something);
}

, . () :

    while: test condition
           bz elsewhile
     loop: push something    
           call update
     test: test condition
           bnz loop
           jmp done         
elsewhile: push something
           call loop_never_taken
     done: ...

, , . , , , , . , !

+1
source

Source: https://habr.com/ru/post/1739484/


All Articles