The Java supports the while, do-while and for iteration/loop control statements. The syntax is similar to C/C++. With JDK1.5, a second form of for was added that implements a “for-each” style loop.
while Statement
The general form of a while statement is:
while(condition)
loop body
Loop body may contain one or more statements. If loop body contains more than one statement then they must be enclosed between braces.
Example: The following program accepts one natural number say n as command line argument and then calculates and displays the sum of first n natural numbers.
class SumNaturalNumbers
{
public static void main(String a[])
{ int n, sum=0;
n = Integer.parseInt(a[0]); //command line argument
int i = 1;
sum = 0;
while (i <= n)
{
sum = sum + i;
i = i + 1;
}
System.out.println("Sum of first " + n + " natural numbers is " + sum);
}
}
do-while Statement
The general form of a do-while statement is:
do
{
} while (condition)
loop body
Example: The following program accepts one integer number say n as command line argument and then calculates and displays the sum of its digits.
class SumOfDigits
{ public static void main(String a[])
{ int n, m, sum, digit;
n = Integer.parseInt(a[0]); //command line argument
m = n;
if(m < 0)
m = -m;
sum = 0;
do
{ digit = m % 10;
sum = sum + digit;
m = m / 10;
}while (m > 0);
System.out.println("Sum of digits of number " + n + " is: " + sum);
}
}
for Statement
The general form of a for statement is:
for(initialization; condition; increment/decrement)
loop body
Loop body may contain one or more statements. If loop body contains more than one statement then they must be enclosed between braces.
Example: The following program accepts one natural number say n as command line argument
and then calculates and displays the sum of first n natural numbers.
class SumNaturalNumbers1
{ public static void main(String a[])
{ int i, n, sum;
n = Integer.parseInt(a[0]); //command line argument
for(i = 1, sum = 0; i <= n; i++)
{
sum = sum + i;
}
System.out.println("Sum of first " + n + " natural numbers is " + sum);
}
}
while Statement
The general form of a while statement is:
while(condition)
loop body
Loop body may contain one or more statements. If loop body contains more than one statement then they must be enclosed between braces.
Example: The following program accepts one natural number say n as command line argument and then calculates and displays the sum of first n natural numbers.
class SumNaturalNumbers
{
public static void main(String a[])
{ int n, sum=0;
n = Integer.parseInt(a[0]); //command line argument
int i = 1;
sum = 0;
while (i <= n)
{
sum = sum + i;
i = i + 1;
}
System.out.println("Sum of first " + n + " natural numbers is " + sum);
}
}
do-while Statement
The general form of a do-while statement is:
do
{
} while (condition)
loop body
Example: The following program accepts one integer number say n as command line argument and then calculates and displays the sum of its digits.
class SumOfDigits
{ public static void main(String a[])
{ int n, m, sum, digit;
n = Integer.parseInt(a[0]); //command line argument
m = n;
if(m < 0)
m = -m;
sum = 0;
do
{ digit = m % 10;
sum = sum + digit;
m = m / 10;
}while (m > 0);
System.out.println("Sum of digits of number " + n + " is: " + sum);
}
}
for Statement
The general form of a for statement is:
for(initialization; condition; increment/decrement)
loop body
Loop body may contain one or more statements. If loop body contains more than one statement then they must be enclosed between braces.
Example: The following program accepts one natural number say n as command line argument
and then calculates and displays the sum of first n natural numbers.
class SumNaturalNumbers1
{ public static void main(String a[])
{ int i, n, sum;
n = Integer.parseInt(a[0]); //command line argument
for(i = 1, sum = 0; i <= n; i++)
{
sum = sum + i;
}
System.out.println("Sum of first " + n + " natural numbers is " + sum);
}
}