Note 2.7.5.
Remember these 3 steps to writing a loop:
-
Initialize the loop variable (before the
whileloop) -
Test the loop variable (in the loop header)
-
Update the loop variable (in the while loop body at the end)

while loop executes the body of the loop as long as (or while) a boolean condition is true. When the condition is false, we exit the loop and continue with the statements that are after the body of the while loop. If the condition is false the first time you check it, the body of the loop will not execute.
while statement looks a lot like an if statement, but it runs more than once. The curly braces ({}) are optional when there is just 1 statement following the condition, but required if there are more than 1 statement in the loop. In the AP exam, they will always use curly braces, which is a good practice to follow.
// The statements in an if run one time if the condition is
// is true and zero times if it is false.
if (condition)
{
statements;
}
// The statements in a while loop run zero or more times,
// determined by how many times the condition is true
while (condition)
{
statements;
}
while loops (or you may have used a for loop which will be covered in the next lesson). Almost every programming language has a while loop.

while loops. The while test is the opposite of the repeat until test. For example, if you are repeatedly moving until reaching x position 100, you must create a Java while loop that repeatedly moves while it has not yet reached x position 100 or is less than 100 as below.



while loop)
int count = 0;
/* missing loop header */
{
System.out.print(count + " ");
count += 2;
}

int count = 1;
while (count <= 10)
{
count *= 2;
}
count = count - 10;
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
while (true)
{
System.out.println("This is a loop that never ends");
}
int i = 0;
while (i < 10)
{
System.out.println(i);
}
while loopβis that although it includes steps 1 and 2 (initializing the loop variable and testing it) it forgot step 3 and never changes the loop variable. The loop variable, i, starts at 0 and the loop loops as long as i < 10 which will always be true because thereβs no code in the loop that changes i. The simple fix is to add a line that increments i:
int i = 0;
while (i < 10)
{
System.out.println(i);
i++;
}
< or <=.
int i = 10;
while (i < 10) // This loop will never run!
{
System.out.println(i);
i++;
}
while loop to repeat the body of the loop a certain number of times in counter-controlled loop as we saw above. However, a while loop is typically used when you donβt know how many times the loop will execute. It is often used for a input-controlled loop where the userβs input indicates when to stop. For example, in the Magpie chatbot lab code below, the while loop stops when you type in βByeβ. The stopping value is often called the sentinel value for the loop. Notice that if you type in βByeβ right away, the loop will never run. If the loop condition evaluates to false initially, the loop body is not executed at all. Another way to stop the loop prematurely is to put in a return statement that makes it immediately return from the method.
Scanner in = new Scanner(System.in);
String statement = in.nextLine();
while (!statement.equals("Bye"))
{
System.out.println(getResponse(statement));
statement = in.nextLine();
}
// The statements in a while loop run zero or more times,
// determined by how many times the condition is true
int count = 0; // initialize the loop variable
while (count < 10) // test the loop variable
{
// repeat this code
// update the loop variable
count++;
}
int n = 35;
int result = 1;
while (n > 0)
{
int d = n % 10;
result *= d;
n /= 10;
}
System.out.println(result);
int count = 0;
/* missing loop header */
{
if (count % 2 == 0)
{
System.out.println(count);
}
count++;
}