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
ASCII Border Box Pattern output
Output (4 rows, 16 columns):
+--------------+ | | | | +--------------+

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.






