Do While Programmas Example:1
Do While Programmas Example:1
DO WHILE PROGRAMMAS
Example :1
class DoWhileLoopExample2 {
int arr[]={2,11,45,9};
int i=0;
do{
System.out.println(arr[i]);
i++;
}while(i<4);
Output:
2
11
45
Example :2
{
int arr[]={2,11,45,9};
int i=0;
do{
System.out.println(arr[i]);
i++;
}while(i<4);
Output:
11
45
Example :3
import java.util.Scanner;
class Sum {
do {
number = input.nextDouble();
sum += number;
Enter a number: 0
Sum = 25.
Example :4
public class Test {
int x = 10;
do {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
WHLIE LOOP
Example :1
public static void main(String[] args) {
int myNumber = 1;
while(myNumber != 1000) {
if((myNumber % 2) == 0) {
number++;
Example : 2
class WhileLoopExample {
int i=10;
while(i>1){
System.out.println(i);
i--;
10
8
7
Example : 3
public class Test {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
Example : 4
class whileLoop
int x = 1;
while (x <= 4)
// next iteration
x++;
Output:
Value of x:1
Value of x:2
Value of x:3
Value of x:4
the end