This post describes how to print alphabets in java using the loop. The alphabet can be printed in lowercase and uppercase. i.e., A to Z or a to z.
Print Uppercase Alphabets A to Z using for loop
Here for loop is used to print alphabets.
package com.digitizedpost.examples;
public class PrintAlphabets {
public static void main(String[] args) {
displayUpperCaseAlphapets();
}
private static void displayUpperCaseAlphapets() {
char character;
for (character = 'A'; character <= 'Z'; ++character) {
System.out.format("%c ", character);
}
}
}
Explanation of Program
Declare a character using the java char data type. In the for loop increment the characters from A to Z. Then execute the for loop and print the characters in console. On each execution the characters will be incremented. These characters are iterated as ASCII characters from 65 to 90.
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 Alphapets a to z using for loop
package com.digitizedpost.examples;
public class PrintAlphabets {
public static void main(String[] args) {
displayLowerCaseAlphapets();
}
private static void displayLowerCaseAlphapets() {
char character;
for (character = 'a'; character <= 'z'; ++character) {
System.out.format("%c ", character);
}
}
}
For lowercase alphabets, ASCII characters 97 to 122 are iterated.
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