Create Patterns Using Loop Conditions

Create Patterns Using Loop Conditions

PHP, TUTORIALS, WEB

Create Patterns Using Loop Conditions

In this tutorial Create Patterns Using Loop Conditions we are gonna show how to create a hollow square in asterisk using the for and if conditions. The patterns are some what complicated for the beginner level programmers.
And here we show how to do that in easy simple steps, explain every steps how they are working on individual parts. At first we must know the basics of the Looping statements and if conditions.
Here is the expected result we want to show to the users.
****
* *
* *
* *
****
So lets begin how to do this manually and get the result from the logic. As usual in this tutorial also we are using the bootstrap as styling framework, because of the convenient of displaying the result.
Begin this with just print the * using the echo statement. And now we want the * is repeated for four times, so here we use loop and echo the same thing inside the loop control.

And it shows the result as ****, consider this as the first row. And one more thing we have to remember is these are in row and column structure like the matrix.
Create Patterns Using Loop Conditions
After getting correctly the **** lets do this for the last row just copy and paste the above code. Where both are same but we have to break the lines to avoid print them in the same line.
Create Patterns Using Loop Conditions
In this Create Patterns Using Loop Conditions tutorial the next step is create the intermediate lines which are having only contains the first and last column values. To check that we are gonna use the if statements.

In the above shown code we are using if statements to check the loop value is 0 or 5 then it prints the * or else it prints the empty space. (Just remember the structure of the table we have been created at first in this tutorial.)
* *
Now we have to repeat this step for three times to get the desired result like we shown in the above example. So again we put the whole loop condition into an another looping statements.

And now finally checks the output in the browser, it shows us the desired result as like what we wants at the beginning of this tutorial.
Create Patterns Using Loop Conditions
Now you may gain some basic knowledge about how to use the loop inside another loop and also the conditional statements inside the loop functions.
We hope this Create Patterns Using Loop Conditions tutorial will gives you some basic knowledge about how to use loops and conditional statements inside another loop functions.

Leave a Reply