0% found this document useful (0 votes)
2 views4 pages

Advanced Java Decision Making

The document discusses advanced decision-making constructs in Java, including the ternary operator, using if statements inside loops, and logical operators. It also covers the use of break and continue statements in loops, as well as the switch expression introduced in Java 14. Each concept is illustrated with code examples and their corresponding outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Advanced Java Decision Making

The document discusses advanced decision-making constructs in Java, including the ternary operator, using if statements inside loops, and logical operators. It also covers the use of break and continue statements in loops, as well as the switch expression introduced in Java 14. Each concept is illustrated with code examples and their corresponding outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Advanced Decision Making in Java

After covering 'if', 'if-else', 'nested if', and 'switch-case', here are the next decision-making
constructs in Java with examples:

1. Ternary Operator ( ?: )
A shorthand for if-else.

Syntax:
condition ? expression1 : expression2;

Example:

public class TernaryExample {


public static void main(String[] args) {
int age = 18;
String result = (age >= 18) ? "Eligible to vote" : "Not eligible";
System.out.println(result);
}
}

Output:
Eligible to vote

2. if inside Loops (for, while)


Using conditions inside loops for decision-making.

Example:

public class EvenOdd {


public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
System.out.println(i + " is Even");
} else {
System.out.println(i + " is Odd");
}
}
}
}

Output:
1 is Odd
2 is Even
3 is Odd
4 is Even
5 is Odd

3. Logical Operators with Conditions


Using &&, ||, ! with conditions.

Example:

public class LogicalOperators {


public static void main(String[] args) {
int age = 25;
boolean hasLicense = true;

if (age >= 18 && hasLicense) {


System.out.println("You can drive");
} else {
System.out.println("You cannot drive");
}
}
}

Output:
You can drive

4. break and continue in Decision Making


Example with break:

public class BreakExample {


public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println("i = " + i);
}
}
}

Output:
i=1
i=2

Example with continue:

public class ContinueExample {


public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println("i = " + i);
}
}
}

Output:
i=1
i=2
i=4
i=5

5. Switch Expression (Java 14+)


Switch can also be used as an expression (Java 14+).

Example:

public class SwitchExpression {


public static void main(String[] args) {
int day = 3;
String dayType = switch (day) {
case 1, 7 -> "Weekend";
case 2, 3, 4, 5, 6 -> "Weekday";
default -> "Invalid";
};
System.out.println(dayType);
}
}

Output:
Weekday

You might also like