![]() Keeping the logic in mind, we know that the outer loop always has to control the number of lines and the inner loop has to control the number of stars. The first line has 5 stars, the second line has 4 stars and so on. ![]() We will use this to our leverage when we print full pyramids.įor this pyramid pattern, the number of stars keeps decreasing with each new line. This gives a finished look to our pattern rather than a congested print. This is because we print a single-space along with the stars as well. Note: Keep one thing in mind that the number of spaces in the k-loop is double space. This is how we control our inner loop based on the outer loops control variable. Our outer control variable is i and inner control variable is j.Īnd so on. Since the number of stars in each line is directly dependent on the line number, our inner loop will be a function of the control variable in our outer loop. Keeping the logic in mind, our outer loop will run five times. 1 star in the first line, 2 stars in the second line and so on. It prints the increasing number of stars in subsequent lines. So, keeping all of this in mind, let’s try to code the patterns now. Also, there is the involvement of spaces and tabs to produce the desired pattern. Depending on the pattern, sometimes there are more than one inner loops or three levels of nested loops.So, let’s say if you have to print 1 star in the first line, 2 stars in the second line and so on, the inner loop would control this logic. The inner loop is always concerned with the number of elements in each line.So, let’s say if you have to print a pattern spanning five lines, the outer loop will always run five times. The outer loop is always concerned with the number of lines.This is because loops are syntactically easier to understand and concise. Patterns are always programmed through nested loops– mostly nested for loops. Before jumping right onto the programs, let us understand the basic logic of coding patterns.Web development, programming languages, Software testing & others Examples of Pattern in PHP Start Your Free Software Development Course
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |