Java Program to Display Alphabets (A to Z) using Loop

In this java program, we will learn about how to print uppercase (A to Z) english alphabets in Java using loops.

When we store a character in a variable of data type char, the ASCII value of character is stored instead of that character itself. A character and it's ASCII value can be used interchangeably. If any expression contains a character then it's corresponding ASCII value is used in expression. The ASCII value of uppercase alphabets(A to Z) are from 65 to 90.
For Example

The ASCII value of 'A' is 65
The ASCII value of 'B' is 66
The ASCII value of 'C' is 67
...
The ASCII value of 'Z' is 90

To understand this java program, you should have understanding of the following Java programming concepts:


Java Program to print all alphabets using for loop

public class AlphabetsPrintForLoop {
  public static void main(String[] args) {
    char c;

    for(c = 'A'; c <= 'Z'; c++)
      System.out.format("%c ", c);
  }
}
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

Using for loop, you can iterate from alphabet 'A' to 'Z' like you are iterating form integer 65 to 90 because characters and their ASCII values can be used interchangeably.We first initialize char variable 'c' with 'A' and then in every for loop iteration we print 'c' and increment it's value by 1. For loop will terminate when value of 'c' becomes more than ASCII value of 'Z'.


Java Program to print all alphabets using while loop

public class AlphabetsPrintWhileLoop {
  public static void main(String[] args) {
    char c = 'A';

    while(c <= 'Z') {
      System.out.format("%c ", c);
      c++;
    }
  }
}
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

Instead of using for loop, in this program we are using while loop to iterate from character 'A' to 'Z' and printing it on screen.