Ask Question
16 May, 13:07

In the Happy Valley School System, children are classified by age as follows:less than 2, ineligible2, toddler3-5, early childhood6-7, young reader8-10, elementary11 and 12, middle13, impossible14-16, high school17-18, scholargreater than 18, ineligibleGiven an int variable age, write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age.

+5
Answers (1)
  1. 16 May, 16:40
    0
    int age = 10;

    switch (age) {

    case 0:

    case 1:

    System. out. println ("ineligible");

    break;

    case 2:

    System. out. println ("toddler");

    break;

    case 3:

    case 4:

    case 5:

    System. out. println ("early childhood");

    break;

    case 6:

    case 7:

    System. out. println ("young reader");

    break;

    case 8:

    case 9:

    case 10:

    System. out. println ("elementary");

    break;

    case 11:

    case 12:

    System. out. println ("middle");

    break;

    case 13:

    System. out. println ("impossible");

    break;

    case 14:

    case 15:

    case 16:

    System. out. println ("high school");

    break;

    case 17:

    case 18:

    System. out. println ("scholar");

    break;

    default:

    System. out. println ("ineligible");

    }

    Explanation:

    In java and many other programming languages, a switch statement is a way of having multiple branching options in a program. This is usually considered a more efficient way than using multiple if ... else if statements. and the expression variables could be byte, char int primitive data types. etc. every branch (option) in a switch statement is followed by the break statement to prevent the code from "falling through". In the question The variable age is declared as an int and initialized to 10. and tested against the conditions given in the question.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “In the Happy Valley School System, children are classified by age as follows:less than 2, ineligible2, toddler3-5, early childhood6-7, ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers