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; }
manpreet Best Answer 2 years ago
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