New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

ASCII Border Box Pattern (+, -, |)

The ASCII border box draws a rectangle the way old text programs did: a plus sign at each corner, dashes along the top and bottom, and vertical bars down the sides. It is a small exercise in choosing between four characters.

Quick answer: Loop over h rows and w columns. A cell on an edge row and an edge column is a corner and gets +; an edge row only gets -; an edge column only gets |; everything else is a space. Work out both edge tests first, then choose.

This program is part of the Symbol Patterns collection.

Table of contents8 sections
  1. ASCII Border Box Pattern output
  2. How the ascii border box pattern works
  3. ASCII Border Box Pattern in C
  4. ASCII Border Box Pattern in C++
  5. ASCII Border Box Pattern in Java
  6. ASCII Border Box Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

ASCII Border Box Pattern output

Output (4 rows, 16 columns):

+--------------+
|              |
|              |
+--------------+
ASCII Border Box Pattern layout diagram, row by row (4 rows, 16 columns)
How each row of the ascii border box pattern is built (4 rows, 16 columns). Blue cells are printed characters; grey dots are spaces.

How the ascii border box pattern works

Most border patterns need two characters, but this one needs four, so a single if/else is not enough. Naming the two tests, edge_row and edge_col, keeps the chain readable: both true means a corner, one true picks a dash or a bar, neither means the inside. The order matters, because a corner also passes each single test.

Key takeaways

  • There are four cases: corner, top/bottom edge, side edge, inside.
  • Check the corner case first, because a corner is also on an edge.
  • Storing the two edge tests in variables makes the chain easy to read.
  • Text tables and menus in console programs use the same characters.

ASCII Border Box Pattern in C

#include <stdio.h>

int main(void) {
    int h = 4, w = 16;                          /* rows and columns */

    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            int edge_row = (i == 1 || i == h);
            int edge_col = (j == 1 || j == w);
            if (edge_row && edge_col)
                printf("+");
            else if (edge_row)
                printf("-");
            else if (edge_col)
                printf("|");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

ASCII Border Box Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int h = 4, w = 16;                          // rows and columns

    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            bool edgeRow = (i == 1 || i == h);
            bool edgeCol = (j == 1 || j == w);
            if (edgeRow && edgeCol)
                cout << "+";
            else if (edgeRow)
                cout << "-";
            else if (edgeCol)
                cout << "|";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

ASCII Border Box Pattern in Java

public class AsciiBorderBox {
    public static void main(String[] args) {
        int h = 4, w = 16;                          // rows and columns

        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                boolean edgeRow = (i == 1 || i == h);
                boolean edgeCol = (j == 1 || j == w);
                if (edgeRow && edgeCol)
                    System.out.print("+");
                else if (edgeRow)
                    System.out.print("-");
                else if (edgeCol)
                    System.out.print("|");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

ASCII Border Box Pattern in Python

h, w = 4, 16                                # rows and columns

for i in range(1, h + 1):
    for j in range(1, w + 1):
        edge_row = i == 1 or i == h
        edge_col = j == 1 or j == w
        if edge_row and edge_col:
            print("+", end="")
        elif edge_row:
            print("-", end="")
        elif edge_col:
            print("|", end="")
        else:
            print(" ", end="")
    print()

Time and space complexity

The program visits each of the h × w cells once, so it runs in O(h × w) time with O(1) extra memory.

Frequently asked questions

Why check for corners first?

A corner is on an edge row and on an edge column. If the edge-row test came first, corners would print a dash instead of a plus sign.

How do I put text inside the box?

Print the box rows one by one and, on a middle row, print |, the text padded to w − 2 characters, and |.

How do I draw a grid instead of one box?

Repeat the corner-and-dash unit for every cell, as in the tic-tac-toe grid pattern.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 10 of 17 in Symbol Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.