In Java, branching with |
|
Here are some basic examples: |
public class Main { public static void main(String[] args) { // If-Else example if (7 % 2 == 0) { System.out.println("7 is even"); } else { System.out.println("7 is odd"); } // If without Else example if (8 % 4 == 0) { System.out.println("8 is divisible by 4"); } // If-Else-If example int num = 9; if (num < 0) { System.out.println(num + " is negative"); } else if (num < 10) { System.out.println(num + " has 1 digit"); } else { System.out.println(num + " has multiple digits"); } // Ternary operator example String result = (num % 2 == 0) ? "even" : "odd"; System.out.println("The number is " + result); } } |
You can have an |