Answer to Question #183624 in Java | JSP | JSF for Masudul Farhad

Question #183624

Give an example of a "while" loop, then provide the equivalent "do-while" loop and "for" loop. Then give a different example of a do-while loop, along with the equivalent while loop and for loop. Finally, give an example of a for loop, along with the equivalent while loop and do-while loop. 


1
Expert's answer
2021-04-20T10:16:58-0400
public class Q183624 {
    /***
     * Main method
     * @param args 
     */
    public static void main(String[] args) {
        int numbers[]={5,6,89,4,55,5,56,5,3,54,8};
        //an example of a "while" loop, then provide the equivalent "do-while" loop and "for" loop.
        //display the numbers in the array "numbers" using "while" loop 
        System.out.println("Print the numbers in the array \"numbers\" using \"while\" loop ");
        int i =0;
        while(i<numbers.length){
            System.out.print(numbers[i]+" ");
            i++;
        }
        //display the numbers in the array "numbers" using "do-while" loop
        System.out.println("\nPrint the numbers in the array \"numbers\" using \"do-while\" loop ");
        i =0;
        do{
            System.out.print(numbers[i]+" ");
            i++;
        }while(i<numbers.length);
        
        //display the numbers in the array "numbers" using "for" loop 
        System.out.println("\nPrint the numbers in the array \"numbers\" using \"for\" loop ");
        for(int n: numbers){
            System.out.print(n+" ");
        }
        
        //a different example of a do-while loop, along with the equivalent while loop and for loop.
        //Print the powers of two:
        System.out.println("\n\nPrint the powers of two using \"do-while\" loop");
        i =0;
        do{
            System.out.println(i+"^2 = "+(i*i));
            i++;
        }while(i<10);
        
        i =0;
        System.out.println("\nPrint the powers of two using \"while\" loop");
        while(i<10){
            System.out.println(i+"^2 = "+(i*i));
            i++;
        }
        System.out.println("\nPrint the powers of two using \"for\" loop");
        for(int j=0;j<10;j++){
            System.out.println(j+"^2 = "+(j*j));
        }
        //Finally, give an example of a for loop, along with the equivalent while loop and do-while loop. 
        System.out.println("\nPrint the random 10 numbers using \"for\" loop");
        for(;;){
            double number = 0 + (Math.random() * 100);
            System.out.println(""+number);
            if(number>90){
                //exit loop
                break;
            }
        }
        System.out.println("\nPrint the random 10 numbers using \"while\" loop");
        while(true){
            double number = 0 + (Math.random() * 100);
            System.out.println(""+number);
            if(number>90){
                //exit loop
                break;
            }
        }
        System.out.println("\nPrint the random 10 numbers using \"do-while\" loop");
        do{
            double number = 0 + (Math.random() * 100);
            System.out.println(""+number);
            if(number>90){
                //exit loop
                break;
            }
        }while(true);
    }
}

Example:


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog