Skip to content

Add 30 comprehensive Java conditional examples with modern Java 25 features#3

Merged
janbodnar merged 2 commits intomainfrom
copilot/fix-d2a47f69-2178-449f-9c0e-eb1483202353
Oct 7, 2025
Merged

Add 30 comprehensive Java conditional examples with modern Java 25 features#3
janbodnar merged 2 commits intomainfrom
copilot/fix-d2a47f69-2178-449f-9c0e-eb1483202353

Conversation

Copy link
Contributor

Copilot AI commented Oct 7, 2025

Overview

This PR adds 30 comprehensive Java examples demonstrating various conditional logic patterns to the conditionals.md file, utilizing modern Java 25 features including compact source files, instance main methods, and implicit imports.

What's Included

The examples progress from basic to advanced concepts, covering:

Basic Conditionals (Examples 1-5)

  • Simple if statements
  • If-else branching
  • Multiple conditions with else if
  • Boolean logical operators (&&, ||)
  • Negation operator (!)

Modern Switch Expressions (Examples 6-10)

  • Switch statements with arrow syntax
  • Switch expressions that return values
  • Switch with yield keyword
  • String-based switch cases
  • Null handling in switch

Ternary Operators & Boolean Logic (Examples 11-15)

  • Ternary conditional operator
  • Nested ternary expressions
  • Complex boolean expressions
  • Short-circuit evaluation

Pattern Matching (Examples 16-20)

  • Pattern matching with instanceof
  • Pattern matching across multiple types
  • Pattern matching in switch expressions
  • Guarded patterns with when clause
  • Record pattern deconstruction

Advanced Patterns (Examples 21-30)

  • Conditionals with collections (List, Map, Set)
  • Conditionals in stream operations
  • Guard clauses and early returns
  • Nested conditionals
  • Optional value handling
  • Conditional variable initialization with yield
  • Range checking patterns
  • Loop conditionals with break
  • Combining switch and pattern matching

Modern Java 25 Features

All examples use cutting-edge Java 25 features:

  • Compact source files - No class wrapper needed
  • Instance main methods - Simple void main() entry point
  • Implicit imports - IO, List, Map, Set, ArrayList, HashMap automatically available
  • Type inference - Using var for cleaner code
  • Enhanced pattern matching - With instanceof and switch
  • Guarded patterns - Using when clause for conditional matching
  • Record patterns - For elegant deconstruction

Code Quality

  • ✅ All descriptions under 80 characters per line with two-space endings
  • ✅ Progressive complexity from simple to advanced
  • ✅ Clean section titles without numbering
  • ✅ Consistent code formatting with dark theme
  • ✅ Practical, runnable examples demonstrating real-world use cases

Each example includes a brief explanation below the code to clarify the demonstrated concept.

Original prompt

On conditionals.md, Here’s a refined prompt for generating 30 Java examples focused on conditionals, designed for modern Java (up to Java 25) and compact source files:


🔍 Prompt: Generate 30 Java Examples Demonstrating Java Conditionals

Create 30 concise and progressively advanced Java examples that showcase different types of conditional logic in Java. Each example should highlight a unique use case or syntax variation involving conditionals.

✅ Requirements

  • Use modern Java syntax, including features up to Java 25:
    • Compact source files
    • Instance main methods
    • Type inference with var
    • Implicit imports (List, Map, IO, etc.)
  • Cover a variety of conditional constructs:
    • if, else if, else
    • switch and enhanced switch expressions
    • ternary operator
    • Pattern matching for instanceof
    • Boolean logic (&&, ||, !)
    • Nested conditionals
    • Conditionals with collections and streams
    • Guard clauses and early returns
    • Conditionals inside loops or methods

📄 Format

void main() {
    int score = 85;
    if (score >= 90) {
        IO.println("Grade: A");
    } else if (score >= 80) {
        IO.println("Grade: B");
    } else {
        IO.println("Grade: C or below");
    }
}

Description:
Demonstrates basic conditional branching based on numeric thresholds.

🧠 Style Guidelines

  • Use compact source file format (no class or static method unless necessary)
  • Prefer IO.println for output
  • Use var for type inference
  • Keep examples readable and runnable
  • Include a brief description (max 80 characters per line, two-space line endings)
  • Refer to functions without parentheses (e.g. the main function)

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: janbodnar <16985640+janbodnar@users.noreply.github.com>
Copilot AI changed the title Here’s a refined prompt for generating **30 Java examples focused on conditionals**, designed for modern Java (up to Java 25) and compact source files: --- ## 🔍 Prompt: Generate 30 Java Examples Demonstrating Java Conditionals Create **30 concise an... Add 30 comprehensive Java conditional examples with modern Java 25 features Oct 7, 2025
Copilot AI requested a review from janbodnar October 7, 2025 08:56
@janbodnar janbodnar marked this pull request as ready for review October 7, 2025 09:15
@janbodnar janbodnar merged commit 360c185 into main Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants