Teaching efficient programming if-elif

Teaching students the differences between lots of sequential if statements versus if-elif can be challenging. One technique I tried was:

1. Select eight students and write one of their names on a piece of paper e.g. Eliza
2. Line the 8 students up and ask each in turn if there name is “Eliza” i.e. if name = “Eliza” say “Yes”. Regardless of when “Yes” is said you ask every single student in the line.
3. Line them up again and follow the same process…. as soon as a student says “Yes” don’t bother asking anyone else.

When “Eliza” is the first or second student this becomes a very clear example of differences between efficient and inefficient code. (Notwithstanding what a compiler might do of course.)

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s