---
schema_version: "1"
id: 5852729efc1401ca1a328be4736a97dc0314958ce5836b1d221e57fedadcb755
title: While Loops
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_loops_while.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_while.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 59ac1228d1537d340fb8487d727099749326e9f910410088883c920042494bad
---
# While Loops

The Apex while loop repeatedly executes a block of code as long as a particular Boolean condition remains true. In this syntax, code\_block is a placeholder value. The syntax is:

while (condition) {
    code\_block
}

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

Curly braces ({}) are required around a code\_block only if the block contains more than one statement.

Unlike do\-while, the while loop checks the Boolean condition statement before the first loop is executed. Consequently, it is possible for the code block to never execute.

As an example, the following code outputs the numbers 1 - 10 into the debug log:

Integer count = 1;

while (count < 11) {
    System.debug(count);
    count++;
}

- [← Previous](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_do_while.htm "Do-While Loops")
- [Next →](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for.htm "For Loops")