Beginners Programs :

Plus Star Pattern Java Program | Patterns

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

  • Print Plus Star Pattern – Using For Loop
  • Print – Using While Loop
  • Print – Using Do While Loop

Using For Loop

1) First checks the condition( i<=n*2-1)at the outer for loop, if it is true then it comes into the loop and checks the

“if “condition. If the “if” condition is true, then the first inner “for” loop will be executed, otherwise “else” part will be executed i.e 2nd inner “for” loop.

2) In 1st inner loop, if the condition true then checks the “if ” condition, if “if” condition is true then it displays the character which we have given to display and displays space,the  code in the 1st inner “for” loop will execute until the inner for loop condition is false, then the cursor comes to outer “for” loop, if condition is true again, the same process will continue.

3) The 2nd inner for loop will execute only the if condition at the outer “for” loop is false, in this for loop if the condition is true at “for” loop then it displays the character which we have given to display.

Output:

Using While Loop

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

2) The code will execute until the condition at while loop is false i.e 1<=n*2-1

Output:

Using Do-while Loop

1) Do- while loop executes the code one time then checks the condition.

2) The first outer do-while loop will execute, then checks the condition i<=n*2-1.

3) If the ” if condition i!=n” is true, then 1st inner do loop will be executed otherwise 2nd inner do loop will be executed.

4) The total code in outer do-while loop executes until the condition false i.e i<=n*2-1.

Output:

More Programs:

Leave a Reply

Your email address will not be published. Required fields are marked *

*