Answer to Question #146283 in Java | JSP | JSF for Nompumelelo Ngoma

Question #146283
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. Use your examples to illustrate the advantages and disadvantages of each looping structure and describe those disadvantages and advantages.
1
Expert's answer
2020-11-23T23:47:28-0500

1. While

The while loop operator is very simple and consists of only two parts: the condition and the body of the loop. The body of the loop is executed over and over until the condition is true

This is a good example of using a while loop:

  Scanner console = new Scanner(System.in); 
        int sum = 0; // In the variable sum we will store the sum of numbers.
        while (console.hasNextInt())// While integers are entered in the console, we read the next number into the variable x.
        {
            int x = console.nextInt();
            sum = sum + x;                 // Add x to the sum of numbers (sum variable).
        }
        System.out.println(sum);      //   We display the calculated amount on the screen.


equivalent do-while:

Here, the condition check occurs after the loop body is executed.

If we introduce a non-integer number, then in the case of while the body of the cylinder will not be drunk even once, and in the case of using do -while, this will lead to an error. Obviously, in this case it is better to use a while loop.

      Scanner console = new Scanner(System.in);
        int sum = 0;

       do {
            int x = console.nextInt();
            sum = sum + x;
        } while (console.hasNextInt());
        System.out.println(sum);
        

equivalent for:


        Scanner console = new Scanner(System.in);
        int sum = 0;
        for(;console.hasNextInt();)
        {
            int x = console.nextInt();
            sum = sum + x;
        }
        System.out.println(sum);



2.Do-while

The do-while loop is usually used precisely when it makes no sense to check the condition if the loop body has failed.

For example, some calculations take place in the body of the loop, and their results are used in the condition.

        String s;
        do
        {
            s = console.nextLine();
        }
        while (!s.equals("exit"));

        // equivalent while:
        String s;
        while (true)
        {
            s = console.nextLine();
            if (s.equals("exit"))
                break;
        }


        // equivalent for:
        String s;
        for (;true;){
            s = console.nextLine();
            if (s.equals("exit"))
                break;
        }

3.For

for - a loop with a counter - is executed and at each iteration it updates the counter while the condition in the declaration of the loop is met (i.e. checking the condition returns true).

The while loop can be used in all cases when a command or group of commands needs to be executed several times. But of all cases, one case is worth highlighting.

This is the case when the programmer (the author of the program) knows in advance how many times his cycle should be executed.

This is usually solved by introducing a special counter variable, and each iteration of the loop, the variable increases (or decreases) by 1.   

It seems that everything works as it should, but not very convenient. Before the loop, we set the start value of the counter variable, then in the condition we check whether it has already reached the final value. But we usually change it at the very end of the body of the cycle.

Therefore, where the counter variable is used, it is more convenient to use the for loop


        for (int i=0; i<20; i++)
        {
            System.out.println(i);
        }

 equivalent while:


        int i = 0;
        while (i < 20)
        {
            System.out.println(i);
            i++;
        }


equivalent do-while:

here again, the program may work out incorrectly: 

if the condition is false, then the body of the cycle will still be executed 1 time

       int i = 0;

        do{
            System.out.println(i);
            i++;
        } while (i < 20);

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
APPROVED BY CLIENTS