Beginners Programs :

8 Star Pattern Java Program – 4 Ways | Programs

Java program to print 8-star pattern program. We have written below the print/draw 8-star asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we have added compiler so that you can execute the below codes.

Using For Loop

1) First, checks the condition at for loop, if it is true, then it comes to”if “condition.

If the “if” condition is true then run the first inner for loop, otherwise, run the 2nd inner for loop.

2) In the first inner loop iterates from j=1 to j=n, checks the “if “condition, if it is true it will display “space'” otherwise it comes to else part and display the “*” symbol.

3) If the first “if” condition is false then 2nd inner for loop will be executed, in the 2nd for loop iterates from j=1 to j=n, check the “if” condition if it true it displays the “*” symbol otherwise it displays “space”.

Output:

Using While Loop

1) First, the condition at while will be checked, if the condition is true it checks the “if” condition if it true it executes the first inner while loop otherwise it comes to the else part i.e 2nd inner while loop.

2) In the 1st inner while loop, if the condition at while is true then checks the “if ” condition, if the condition is true then it displays “space” otherwise it displays “*”, it will continue until the inner while loop condition is false.

3) 2nd inner while loop executes only the if condition at outer while loop is false, in 2nd inner while loop first checks the condition at while will be checked after that checks the “if” condition, if condition true then displays “*” otherwise displays “space”.

4) The total code in the outer while loop will execute until the condition at while is false.

Output:

Using Do-While Loop

1) If the condition at the “if” is true in an outer do-while loop it executes the 1st inner do-while loop, otherwise it comes to else part i.e. 2nd inner do-while loop.

2) In 1st inner do-while loop if the “if” condition is true it will display space otherwise it will display “*”, 2nd inner do-while loop if the “if” condition is true then it will display “*” otherwise it will display space.

3) The total code in the outer do-while loop will be executed until the while condition is false i.e while(i<=k).

Output: