Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

Java Program to Print Alphabets (A to Z) Using Loops

To print alphabets in Java, loop a char variable from 'A' to 'Z' and print each letter. Because char is a numeric type backed by ASCII values, the loop simply steps through codes 65 to 90 for uppercase and 97 to 122 for lowercase. A for loop is the clearest way, but while loops, ASCII casting, and streams work too.

How Printing Alphabets Works in Java

The trick behind every alphabet program is that Java stores characters as numbers. A char is a 16-bit Unicode value, and for the English alphabet those values match the ASCII table: 'A' is 65, 'Z' is 90, 'a' is 97, and 'z' is 122. Because char is an integral type, you can compare and increment it just like an int, so a condition such as ch <= 'Z' and an operation such as ++ch behave exactly as they would with numbers. That single fact is what makes looping through the alphabet a two-line problem.

Print Uppercase Alphabets Using a for Loop

The most common approach initializes a char at 'A' and loops while it is less than or equal to 'Z', printing each letter followed by a space.

class PrintUppercase {
  public static void main(String[] args) {
    char ch;
    for (ch = 'A'; ch <= 'Z'; ++ch) {
      System.out.print(ch + " ");
    }
  }
}

Output:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Print Lowercase Alphabets Using a for Loop

Only the start and end characters change: begin at 'a' and loop while the value stays at or below 'z'.

class PrintLowercase {
  public static void main(String[] args) {
    char ch;
    for (ch = 'a'; ch <= 'z'; ++ch) {
      System.out.print(ch + " ");
    }
  }
}

Output:

a b c d e f g h i j k l m n o p q r s t u v w x y z

Print Alphabets Using a while Loop

A while loop produces the same result and can read more naturally when the increment logic is more complex. Initialize the variable before the loop and increment it inside the body.

class PrintWhile {
  public static void main(String[] args) {
    char ch = 'A';
    while (ch <= 'Z') {
      System.out.print(ch + " ");
      ch++;
    }
  }
}

If you want a refresher on the construct itself, see this TestMu AI example on how to demonstrate a while loop in Java.

Reverse Order and ASCII-Based Variations

To print Z to A, start at 'Z' and decrement. To make the ASCII relationship explicit, loop an int and cast it to char:

class Variations {
  public static void main(String[] args) {
    // Reverse: Z to A
    for (char ch = 'Z'; ch >= 'A'; ch--) {
      System.out.print(ch + " ");
    }
    System.out.println();

    // Using ASCII values 65..90
    for (int i = 65; i <= 90; i++) {
      System.out.print((char) i + " ");
    }
  }
}

On Java 8 and above, IntStream.rangeClosed('A', 'Z') can generate the same sequence without an explicit loop, mapping each int to a char before printing.

Common Mistakes and Troubleshooting

  • Using < instead of <=: Writing ch < 'Z' stops at Y and drops the last letter. Use <= to include Z.
  • Forgetting the int-to-char cast: When looping over ASCII numbers, printing i without (char) i shows digits like 65, not letters.
  • Mixing up cases: Uppercase and lowercase live in different ASCII ranges, so reusing 'A' to 'Z' bounds for lowercase prints nothing.
  • char overflow in reverse loops: Decrementing below 'A' without a correct condition can loop unexpectedly, so guard with ch >= 'A'.
  • Using + with chars accidentally: ch + 1 promotes char to int, so wrap it in (char) if you expect a letter, not a number.

Conclusion

Printing the alphabet in Java comes down to one idea: characters are numbers, so you can loop a char from 'A' to 'Z' exactly like an int. A for loop is the cleanest solution, while loops, ASCII casting, reverse order, and streams all give you flexibility once you understand the underlying character codes. Master this pattern and you have a foundation for far more advanced Java string and character manipulation.

Frequently Asked Questions

How do you print A to Z in Java?

Use a for loop with a char variable that starts at 'A' and continues while it is less than or equal to 'Z', incrementing each iteration. Because char is a numeric type in Java, the loop steps through the ASCII codes 65 to 90 and prints each letter with System.out.print(ch).

Why can you use a char in a for loop condition in Java?

In Java, char is an integral type backed by a 16-bit Unicode value, so characters can be compared and incremented like numbers. 'A' equals 65 and 'Z' equals 90, which lets ch <= 'Z' and ++ch work exactly as they would with integers, stepping through the alphabet.

How do you print alphabets using ASCII values in Java?

Loop an int from 65 to 90 and cast each value to char with (char) i before printing. Because 65 to 90 are the ASCII codes for A to Z, casting the number produces the matching letter, giving the same output as looping directly over a char variable.

How do you print alphabets in reverse in Java?

Start the char variable at 'Z' and decrement it while it stays greater than or equal to 'A'. Using for (char ch = 'Z'; ch >= 'A'; ch--) prints the alphabet from Z back to A, since the loop walks the ASCII codes downward from 90 to 65.

How do you print both uppercase and lowercase alphabets in Java?

Run two loops: one from 'A' to 'Z' for uppercase and one from 'a' to 'z' for lowercase. The lowercase letters occupy ASCII codes 97 to 122, so the same loop pattern works by simply changing the start and end characters.

Can you print alphabets in Java without a loop?

Yes. From Java 8 onward you can use IntStream.rangeClosed('A', 'Z') and map each int to a char, then print the result. This stream-based approach avoids an explicit loop, though a simple for loop remains the clearest and most common way to print the alphabet.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests