4
$\begingroup$

I am an experienced programmer that has just started teaching. I am struggling to see how to introduce selection to my programming classes. None of the examples that I have seen so far are good.

I am struggling to find examples other than the ones below. (These are more advanced than I need.)

  • Termination condition for recursion (We don't do recursion until upper years of school, exams do not need it).

  • Input validation (Again the exams say don't worry about checking input (I think this is a mistake, as it leads to cowboy programmers)).

Both of these are only in the syllabus from UK year 12. I am teaching from UK year 7.

Can you help with good examples, and exercises that my younger pupils can use?


year 7 is age 11-12 year 12 is age 16-17

$\endgroup$
  • 2
    $\begingroup$ We always need more good people in this field. I'm glad you made the switch :) $\endgroup$ – Ben I. Jul 7 '17 at 16:09
3
$\begingroup$

There are endless examples that illustrate selection. As you know, most useful programs have at least one selection statement. I've found that some students struggle with the concept, so I start small and pick examples that resonate with students, for example:

    if (hungry)
        System.out.println("Eat a sandwich.");

    if (age > 17)
        System.out.println("You can vote.");
    else
        System.out.println("You cannot vote.");

For a switch statement, I like to show some type of menu:

    switch(choice) {
    case 0:
        System.out.println("Balance Inquiry.");
        break;
    case 1:
        System.out.println("Transfer");
        break;
    case 2:
        System.out.println("Withdrawal");
        break;  
    case 3:
        System.out.println("Deposit");
        break;          
    default:
        System.out.println("Quit");
        break;          
    }
$\endgroup$
  • $\begingroup$ choices=("Balance Inquiry.","Transfer","Withdrawal","Deposit","Quit"); print(choices[choice]) [I would not expect my year 7s do do this, but it pains me to give them bad examples], but +1 for the first example, with the ifs. $\endgroup$ – ctrl-alt-delor Jul 7 '17 at 16:19
  • $\begingroup$ You can easily modify that example: String[] iceCreamChoices = new String[]{"chocolate", "vanilla", "strawberry", "tutti frutti"}; $\endgroup$ – Edwin Torres D.Eng. Jul 7 '17 at 16:45
  • 5
    $\begingroup$ Please use brackets in if-statements. For learners the distinction between statements and blocks can be difficult and is a source for subtle bugs even for experienced programmers. $\endgroup$ – Thorbjørn Ravn Andersen Jul 7 '17 at 18:12
2
$\begingroup$

If you want something very elementary: Write a program that determines the age of a person, where the person's date of birth and the current date are given. For simplicity assume that both dates have already been split into three integer variables.

(The age is the difference of the years minus 1, if the current date comes before the birthday in the calendar, otherwise it's the difference of the years.).

$\endgroup$
  • $\begingroup$ I would do it all in liner maths, just adding, subtraction, multiplying, dividing, and rounding. No need for selection. You complicate the problem, when you start thinking that a year has an end. Time is continues. $\endgroup$ – ctrl-alt-delor Jul 10 '17 at 17:21
  • $\begingroup$ @richard Do you mean something like int(((10000*y2 + 100*m2 + d2) - (10000*y1 + 100*m1 + d1)) / 10000)? That's a hack. Even though it avoids the conditional, I don't think that this is easier to understand for an 11 year old. $\endgroup$ – Uwe Jul 11 '17 at 11:44
1
$\begingroup$

I suppose that a traffic light is a very good example and easy to understand by your target audience.

if (light is green) 
    go
else if (light is yellow) 
    stop and go
else [if (light is red)] 
    stop 
$\endgroup$
  • $\begingroup$ I live in Portugal... Here, this is the meaning of the lights... and we do not have red and yellow at the same time... The braquets mean that what is inside is optional. In the last else you could have an else if (...) or simply the else since there are no aditional options $\endgroup$ – Nuno Gil Fonseca Jul 8 '17 at 19:51
  • $\begingroup$ The website I checked said that Portugal has a red and yellow state. Red, red and yellow, green, yellow, back to red. Is this not the case. Note that my pupils said that this was not true [here in england], but I showed them a video, then set them home work to stare at traffic lights. They said that they were surprised. $\endgroup$ – ctrl-alt-delor Jul 10 '17 at 17:25
  • $\begingroup$ @richard :) Never saw that "red and yellow" scenario... These are the commom traffic lights around here: codigo.pt/sinais-de-transito/sinais-luminosos (Sorry but it is in portuguese). $\endgroup$ – Nuno Gil Fonseca Jul 10 '17 at 18:08
1
$\begingroup$

I think of a factory where you decide upon requirements which type of object to build:

LuquidsContainer produceByRequirement(String requirement1,String requirement2){
   if("can be closed".equals(requirement1){
      if("keeps temperature".equals(requirement2){
         return new ThermosCan();
      } else {
         return new Bottle();
      }
   } else {
      if("individual use".equals(requirement2){
         return new Glass();
      } else {
         return new Flagon();
      }
   }
}
$\endgroup$
0
$\begingroup$

One of the early examples I usually use is a quadratic equation solver using the quadratic formula. Since many languages don't like that old square root of a negative number thing it's a nice place for a conditional.

$\endgroup$
  • $\begingroup$ What would you do if you are about to pass a negative number to a square root function? Do you call it with a positive and assign the result to the imaginary part of the tuple, I think this will be beyond most of my pupils. $\endgroup$ – ctrl-alt-delor Jul 7 '17 at 16:12
  • $\begingroup$ @richard For Year 7 students, you tell them "no solutions". They probably won't be familiar with complex numbers at that stage, unless they're way ahead of their class (they're usually taught in Year 12/13 Further Maths, not to a Year 7 class) $\endgroup$ – Aurora0001 Jul 7 '17 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.