Can I use multiple inner loops instead of an if statement?
You could, but it is incredibly messy. Coordinate-based boolean logic is the standard and most readable way to handle hollow shapes.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Printing a Hollow Square Star Pattern
Generate an N x N grid and print a star only if the current row or column is at the absolute boundary (1 or N). Otherwise, print a space.
Because terminal fonts are usually taller than they are wide. A star (*) takes up more vertical space than horizontal space. You can fix this by printing '* ' (with a space) to widen the shape.
This exact boundary logic (checking if row==0 or col==max) is used in graph traversal algorithms (like BFS/DFS in a matrix) to detect when you hit a wall.
Still have questions?
Browse all our FAQs or reach out to our support team
