Ads

Java example for while loop demonstration. | JAVA LANGUAGE | JAVA PROGRAMMING

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

//Java program to print name 10 times using 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

        while (loop <= 10) {

            System.out.println(name);

            loop++; //increment

        }

}

}


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;

        while (loop <= N) {

            System.out.print(loop + " ");

            loop++;

        }


    }

}




Post a Comment

0 Comments