ADVERTISEMENT
ADVERTISEMENT

Java example for do while loop demonstration

This program will demonstrate example of do while loop in java, the use of do while is similar to c programming language, here we will understand how do while loop works in java programming with examples.

do while Loop Example in Java

Programs 1) Print your name 10 times.

//Java program to print name 10 times using do while loop
 
public class PrintNames
{
    public static void main(String args[]){
         
        int loop; //loop counter declaration
        final String name="Mike"; //name as constant
         
        loop=1; //initialization of loop counter
        do{
            System.out.println(name);
            loop++; //increment
        }while(loop<=10);
         
    }
}

Output

    Mike
    Mike
    Mike
    Mike
    Mike
    Mike
    Mike
    Mike
    Mike
    Mike

Programs 2) Print numbers from 1 to N.

//Java program to print numbers from 1 to N
 
import java.util.Scanner;
 
public class PrintNumbers
{
    public static void main(String args[]){
        int loop; //declaration of loop counter
        int N; 
         
        Scanner SC=new Scanner(System.in);
         
        System.out.print("Enter value of N: ");
        N=SC.nextInt();
         
        loop=1;
        do{
            System.out.print(loop +" ");
            loop++;
        }while(loop<=N);
         
    }

Output

Enter value of N: 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 23 24 25 26 27 
28 29 30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49 50 

Java Basic Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.