[Tutor] dictionary swittching

dman dman@dman.ddts.net
Wed, 15 May 2002 09:59:50 -0500


--ADZbWkCsHQ7r3kzd
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable


If you don't want to see a comparision against C then don't read this
:-).

On Tue, May 14, 2002 at 09:45:29PM -0400, Erik Price wrote:
| On Tuesday, May 14, 2002, at 08:20  AM, dman wrote:
|=20
| >Hmm, maybe the on-line doc will do a better job of explaining the
| >specific semantics of an "if" statement and the meaning of 'elif' and
| >'else'.
|=20
| How about:
|=20
| if (condition1):
|   execute green code
| elif (condition2):
|   execute orange code
| elif (condition3):
|   execute red code
| else:
|   execute blue code
|=20
| is equivalent to
|=20
| if (condition1):
| 	execute green code
| else:
| 	if (condition2):
| 		execute orange code
| 	else:
| 		if (condition3):
| 			execute red code
| 		else:
| 			execute blue code

That's functionally equivalent, and is actually how the parse tree is
built for the C/C++/Java if-else ladder.  Just for your amusement,
here's how the C version is written.  Remember that indentation is not
significant.

if (condition1)
{
    execute green code
}
else if (condition2)
{
    execute orange code
}
else if (condition3)
{
    execute red code
}
else
{
    execute blue code
}

It almost looks the same, but notice that only 1 "else" block has
braces and notice the space between "else" and "if".  If I use braces
for all blocks, and indent it like that, it would look like :

if (condition1)
{
    execute green code
}
else=20
{
    if (condition2)
    {
        execute orange code
    }
    else=20
    {
        if (condition3)
        {
            execute red code
        }
        else
        {
            execute blue code
        }
    }
}

In this case I don't really need the braces since each block is just
one line (which is why the braces can be omitted and the indentation
easier to read as in the first example). =20

Notice, too, that C/C++/Java suffer from the "dangling else" problem.
Take this example :

if (condition1)
    execute green code
    if (condition2)
        execute orange code
else=20
    execute red code

It doesn't do what you (as a python programmer) would expect.  That
last "else" clause really belongs with the inner "if".  The solution
is to use braces to explicitly denote the limit of the first "if"
block.  Python doesn't have this problem because the indentation is
what defines the structure.

-D

--=20

Thy Word is a lamp unto my feet
and a light unto my path.
        Psalms 119:105
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--ADZbWkCsHQ7r3kzd
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzid+YACgkQO8l8XBKTpRQoGQCgpBRB0x/zxEuNcQ9bAdwdSKKN
G9cAoKgF7YJw+scR720DEox4ce1AGw4N
=lubM
-----END PGP SIGNATURE-----

--ADZbWkCsHQ7r3kzd--