0% found this document useful (0 votes)
40 views

Do-While For Statements

The document discusses the while, do-while, and for loop statements in Java. The while statement continually executes as long as an expression evaluates to true. The do-while statement executes the block at least once and then evaluates the expression. The for statement provides a compact way to iterate over a range of values with initialization, termination, and increment sections.

Uploaded by

Sophie Mendez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Do-While For Statements

The document discusses the while, do-while, and for loop statements in Java. The while statement continually executes as long as an expression evaluates to true. The do-while statement executes the block at least once and then evaluates the expression. The for statement provides a compact way to iterate over a range of values with initialization, termination, and increment sections.

Uploaded by

Sophie Mendez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The while and do-while Statements

• The while statement continually executes a block of statements while a particular condition is true. Its syntax can be
expressed as:

while (expression)
{
statement(s)
}

The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true,
the while statement executes the statement(s) in the while block. The while statement continues testing the expression and
executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be
accomplished as in the following WhileDemo program:

class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11)
{
System.out.println("Count is: %d", count);
count++;
}
}
}

You can implement an infinite loop using the while statement as follows:

while (true){
// your code goes here
}

• The Java programming language also provides a do-while statement, which can be expressed as follows:

do
{
statement(s)
}
while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top.
Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program:

class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do
{
System.out.println("Count is: %d" ,count);
count++;
}
while (count < 11);
}
}
The for Statement
• The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop"
because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement
can be expressed as follows:

for (initialization; termination; increment) {


statement(s)
}

When using this version of the for statement, keep in mind that:

• The initialization expression initializes the loop; it's executed once, as the loop begins.
• When the termination expression evaluates to false, the loop terminates.
• The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to
increment or decrement a value.

The following program, ForDemo, uses the general form of the for statement to print the numbers 1 through 10 to standard output:

class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++)
{
System.out.println("Count is: %d" ,i);
}
}
}

The output of this program is:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

You might also like