12 votes

The underappreciated else keyword in Python

4 comments

  1. [4]
    ainar-g
    Link
    N. Wirth's Oberon-07 had a different use case for the ELSIF keywords in loops. They delimited different exit conditions and the corresponding code blocks. Example (the Euclidean algorithm): WHILE...

    N. Wirth's Oberon-07 had a different use case for the ELSIF keywords in loops. They delimited different exit conditions and the corresponding code blocks. Example (the Euclidean algorithm):

    WHILE m > n DO
    	m := m  n
    ELSIF n > m DO
    	n := n  m 
    END
    

    This is equivalent to the C code:

    while (true) {
    	if (m > n) {
    		m = m - n;
    	} else if (n > m) {
    		n = n - m;
    	} else {
    		break;
    	}
    }
    

    (There is another way of writing it in C, but this one is closer to the Oberon-07 version.)

    5 votes
    1. [3]
      Apos
      Link Parent
      I had that as a proposed loop in my language. I had named it the coil loop. I have since dropped it.

      I had that as a proposed loop in my language. I had named it the coil loop. I have since dropped it.

      2 votes
      1. [2]
        ainar-g
        Link Parent
        Wirth himself calls it (PDF) “Dijkstra's form”, although Google doesn't seem to know that name. So you could do something like: EWD m > n DO m := m – n ELSIF n > m DO n := n – m END

        Wirth himself calls it (PDF) “Dijkstra's form”, although Google doesn't seem to know that name. So you could do something like:

        EWD m > n DO
        	m := m – n
        ELSIF n > m DO
        	n := n – m 
        END
        
        2 votes