{"id":"return-values-branches","slug":"returning-from-branches","title":"Returning from branches","text":"A method can have several `return` statements, one in each branch. The first `return` that runs ends the method:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(result(72));\n        System.out.println(result(35));\n    }\n\n    public static String result(int points) {\n        if (points >= 50) {\n            return \"pass\";\n        }\n        return \"fail\";\n    }\n}\n```\n\n```output\npass\nfail\n```\n\nFor 72 the first `return` runs, and the method ends there. For 35 the `if` is skipped and the second `return` runs. No `else` is needed, because the method never gets past a `return`.\n\nJava checks that every way through the method ends in a `return`. Without the last line, the method has no value to give back when the points are below 50, and it doesn't compile:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(result(72));\n    }\n\n    public static String result(int points) {\n        if (points >= 50) {\n            return \"pass\";\n        }\n    }\n}\n```\n\n```javac\nMain.java:10: error: missing return statement\n    }\n    ^\n1 error\n```\n","task":"Write a method `isAdult(int age)` that returns `true` for an age of 18 or more, and `false` otherwise. The check runs `System.out.println(isAdult(18));` and expects:\n\n```\ntrue\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(isAdult(18));\n    }\n\n    // Write the isAdult method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(isAdult(18));\n    }\n\n    public static boolean isAdult(int age) {\n        if (age >= 18) {\n            return true;\n        }\n        return false;\n    }\n}\n","hints":["The return type is `boolean`.","An `if` with `return true;`, and `return false;` after it. (A shorter way works too: `return age >= 18;`)"],"tests":[{"name":"System.out.println(isAdult(18));","stdin":"","call":"System.out.println(isAdult(18));\n","expect":"true","hidden":false},{"name":"System.out.println(isAdult(17));","stdin":"","call":"System.out.println(isAdult(17));\n","expect":"false","hidden":true},{"name":"System.out.println(isAdult(40));","stdin":"","call":"System.out.println(isAdult(40));\n","expect":"true","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a method `sign(int number)` that returns -1 for a negative number, 0 for zero and 1 for a positive number. The check runs `System.out.println(sign(-8));` and expects:\n\n```\n-1\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(sign(-8));\n    }\n\n    // Write the sign method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(sign(-8));\n    }\n\n    public static int sign(int number) {\n        if (number < 0) {\n            return -1;\n        }\n        if (number == 0) {\n            return 0;\n        }\n        return 1;\n    }\n}\n","hints":["Three cases, each with its own `return`.","The last case needs no `if`: whatever gets past the first two checks is positive."],"tests":[{"name":"System.out.println(sign(-8));","stdin":"","call":"System.out.println(sign(-8));\n","expect":"-1","hidden":false},{"name":"System.out.println(sign(0));","stdin":"","call":"System.out.println(sign(0));\n","expect":"0","hidden":true},{"name":"System.out.println(sign(25));","stdin":"","call":"System.out.println(sign(25));\n","expect":"1","hidden":true}],"require":[],"forbid":[]},{"task":"Write a method `clamp(int value, int low, int high)` that keeps `value` inside the range from `low` to `high`: below it, return `low`; above it, return `high`; otherwise return the value itself. The check runs `System.out.println(clamp(130, 0, 100));` and expects:\n\n```\n100\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(clamp(130, 0, 100));\n    }\n\n    // Write the clamp method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(clamp(130, 0, 100));\n    }\n\n    public static int clamp(int value, int low, int high) {\n        if (value < low) {\n            return low;\n        }\n        if (value > high) {\n            return high;\n        }\n        return value;\n    }\n}\n","hints":["Like the volume control in module 7, but with the limits as parameters and a `return` in each case.","`if (value < low) { return low; }`, the same for `high`, and `return value;` last."],"tests":[{"name":"System.out.println(clamp(130, 0, 100));","stdin":"","call":"System.out.println(clamp(130, 0, 100));\n","expect":"100","hidden":false},{"name":"System.out.println(clamp(-5, 0, 100));","stdin":"","call":"System.out.println(clamp(-5, 0, 100));\n","expect":"0","hidden":true},{"name":"System.out.println(clamp(42, 0, 100));","stdin":"","call":"System.out.println(clamp(42, 0, 100));\n","expect":"42","hidden":true},{"name":"System.out.println(clamp(5, 10, 20));","stdin":"","call":"System.out.println(clamp(5, 10, 20));\n","expect":"10","hidden":true}],"require":[],"forbid":[]}]}