Wednesday, March 11, 2009

Teaching Expressions

I've found in recent days that many students can understand expressions better when built up one at a time. For example:

int i = 5;
if (Math.sqrt(i + 1) < 4.5) { /* ... */ }

So, the idea is to work outward with types:
  • What's the type of expression 'i'? int
  • What's the type of expression '1'? int
  • What's the type of expression 'i + 1'? int (then converted to double)
  • What's the type of expression 'Math.sqrt(i + 1)'? double
  • What's the type of expression '4.5'? double
  • What's the type of expression 'Math.sqrt(i + 1) < 4.5'? boolean (required for if)
Going over this in front of students and letting them give the answers seems an effective way to teach the concepts (for some people). Showing assignment, boolean operators, and other such examples can also be useful.

No comments:

Post a Comment