Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

at Tools Table Window Help Document9 ces Mailings Review View An . ng :?,?·?,???

ID: 3706696 • Letter: A

Question

at Tools Table Window Help Document9 ces Mailings Review View An . ng :?,?·?,??? 91 | AaBbCcDdEe AaBbCcDdEe AaBbCc NormalNo Spacing Heading 1 This week covered the many different types of loops in Java: while, do, and for. Think of a brief example where you can use loops and write it in correct Java syntax You should show at least 2 of the 3 different types of Java loops in your posting. You may incorporate both kind of loops in the same example, or alternatively, show how the same exampl? can be written using two different kinds of loops. Try to keep your example short, soothers can follow it easily. d States) 655 12

Explanation / Answer

ANSWER: Here I am giving you the code and output if you have any problem then you can comment or like my solution.

CODE:

// Java program to illustrate while loop

class whileLoopDemo

{

public static void main(String args[])

{

//Using for loop to print 1 to 4

System.out.println(" Printing 1 to 4 usnig for loop");

for (int x = 1; x <= 4; x++)

System.out.println("Value of x:" + x);

  

//using while loop to print 1 to 4

// Exit when y becomes greater than 4

System.out.println(" Printing 1 to 4 usnig while loop");

int y = 1;

while (y <= 4)

{

System.out.println("Value of y:" + y);

// Increment the value of y for

// next iteration

y++;

}

System.out.println(" Printing 1 to 4 usnig do-while loop");

//using do while print 1 to 4

int z = 1;

do

{

// The line will be printed even

// if the condition is false

System.out.println("Value of z:" + z);

z++;

}

while (z <=4);

}

}

OUTPUT:

Printing 1 to 4 usnig for loop

Value of x:1
Value of x:2
Value of x:3
Value of x:4

Printing 1 to 4 usnig while loop
Value of y:1
Value of y:2
Value of y:3
Value of y:4

Printing 1 to 4 usnig do-while loop
Value of z:1
Value of z:2
Value of z:3
Value of z:4