> 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/ud2-programacion-estructurada/estructuras-repetivas/do-while.md).

# Do-While

É unha estrutura de control que repite un conxunto de instrucións mentres non se cumpra unha condición. Diferenciase do WHILE en que este bucle vaise executar polo menos unha vez. Ten as seguintes características:

* A condición avalíase **despois** de executar o bloque
* Se a condición é falsa ó inicio, o bucle se executa unha vez
* Pode dar lugar a bucles infinitos se non se actualizan as variables de control

**Uso típico:** Procesos repetitivos como menús interactivos ou validación de datos.

<figure><img src="/files/oR7lbHTvluak1ct7gMLp" alt=""><figcaption></figcaption></figure>

En Java ademais dispoñemos dun segundo bucle por unha condición: `do-while`<i class="fa-copy">:copy:</i>. A diferencia do `while`, neste caso **primeiro execútase o bloque de instrucións e despois avaliase a condición** para decidir se se realiza unha nova iteración.

```java
int i = 1;
do {
    // bloque de instrucions
    System.out.println(i);
    //incrementamos o valor da condicion
    i++;
} while (i<1);
```

## Ámbito das variables definidas no do-while

En Java, o ámbito das variables definidas dentro dun bucle `do-while` segue as mesmas regras de ámbito que outros bloques de control, como `if` ou `for`. As variables definidas dentro dun bucle `do-while` **teñen o seu ámbito limitado ao bloque do bucle**. Isto significa que non son accesibles fóra dese bloque.

```java
do {
    int x = 10;
    System.out.println("O valor de x dentro do bucle while é: " + x);
} while (i<3);

System.out.println("O valor de x fóra do bucle é: " + x); // Error: non se pode atopar a variable x
```


---

# 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/ud2-programacion-estructurada/estructuras-repetivas/do-while.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.
