> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud2-solucions/bucles/exercicios-de-saida-anticipada.md).

# Exercicios de saída anticipada

1. Contrasinal

```java
public static void main(String[] args) {

        boolean encontrado = false;

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 10; j++) {

                if (j % 2 == 0)
                    continue; // Saltar pares

                System.out.println("Probando combinación: (" + i + ", " + j + ")");

                if (i + j == 9) {
                    System.out.println("¡Combinación correcta encontrada!");
                    encontrado = true;
                    break; // Rompe ambos bucles
                }
            }
            if (encontrado)
                break;
        }

        if (!encontrado) {
            System.out.println("No se encontró ninguna combinación válida.");
        }
    }
```

2. Parellas válidas

```java
public class ParejasValidas {
    public static void main(String[] args) {
        System.out.println("Conxunto de parellas válidas para probas:");
        System.out.println("==========================================");
        
        int contador = 0;
        for (int a = 0; a <= 8; a++) {
            //Si e divisible entre tres, saltamos iteración
            if (a % 3 == 0) 
                continue;
            // Bucle para el número b (1 a 9)
            for (int b = 1; b <= 9; b++) {
                int producto = a * b;
                // Si el producto es mayor que 20, saltamos esta pareja
                if (producto > 20) 
                    continue;    
                // Si llegamos aquí, la pareja es válida
                contador++;
                System.out.println("Parella " + contador + ": (" + a + ", " + b + ") - Producto: " + producto);
            }
        }
        
        System.out.println("==========================================");
        System.out.println("Total de parellas válidas: " + contador);
    }
}
```

3. Alumnos

```java
public class PromedioMatematicas {
    public static void main(String[] args) {

        double suma = 0;           // Acumulador das notas válidas
        int contador = 0;          // Contador de alumnos con nota >= 4
        boolean fin = false;       // Indicador para rematar se o promedio supera 7

        externo: // etiqueta para poder romper os dous bucles
        for (int curso = 1; curso <= 2; curso++) {          // Dous cursos: 1º e 2º
            for (int alumno = 1; alumno <= 10; alumno++) {  // Dez alumnos por curso

                // Xerar nota aleatoria entre 0 e 10 (entero)
                double nota = (int)(Math.random() * 11);

                // Mostrar información da iteración
                System.out.println("Curso " + curso + " | Alumno " + alumno + " | Nota: " + nota);

                // Se a nota é menor que 4, non se ten en conta → usar continue
                if (nota < 4) continue;

                // Acumular nota válida
                suma += nota;
                contador++;

                // Calcular promedio actual
                double promedio = suma / contador;

                // Se o promedio supera 7, fin da execución → break externo
                if (promedio > 7) {
                    System.out.println("\n⚠️  Promedio superior a 7 (" + promedio + ").");
                    System.out.println("O programa finaliza anticipadamente.\n");
                    break externo;
                }
            }
        }

        // Mostrar resultados finais
        if (contador > 0) {
            double promedioFinal = suma / contador;
            System.out.println("Número de alumnos tidos en conta: " + contador);
            System.out.printf("Nota promedio final: %.2f\n", promedioFinal);
        } else {
            System.out.println("Non houbo ningunha nota válida para calcular o promedio.");
        }
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud2-solucions/bucles/exercicios-de-saida-anticipada.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
