How to print all alphabets in the uppercase and lowercase in Java

Overview

Using a few tricks in Java, we can print the alphabets both in lowercase and uppercase. Lowercase letters start from "a" and end with "z". Uppercase alphabets begin with "A" and end with "Z". It is also worth noting that alphabets increase value. For this reason, we shall use the for loop to print the alphabets from the first to the last in each case.

Syntax

// for lowercase
for(alphabet = "a"; alphabet <= "z"; alphabet++)
// for uppercase
for(alphabet = "A"; alphabet <= "Z"; alphabet++)
Syntax to print alphabets in uppercase and lowercase

In the syntax above, we used a for loop to print all alphabets. For lowercase, we start with "a", and for every loop iteration, the alphabet increases in value, that is from "a" to "b" , "c" , etc. to "z"; hence, the condition alphabet <="z". The same logic applies to uppercase alphabets.

Example

class HelloWorld {
public static void main( String args[] ) {
// print lowercase alphabets
System.out.println("lowercase alphabets:");
char lowercaseAlphabet;
for(lowercaseAlphabet = 'a'; lowercaseAlphabet <= 'z'; lowercaseAlphabet++){
System.out.print(lowercaseAlphabet);
}
System.out.println("---------------------");
// print uppercase alphabets
System.out.println("UPPERCASE ALPHABETS:");
char uppercaseAlphabet;
for(lowercaseAlphabet = 'A'; lowercaseAlphabet <= 'Z'; lowercaseAlphabet++){
System.out.print(lowercaseAlphabet);
}
}
}

Explanation

In the code above:

  • Line 5: We create a variable called lowercaseAlphabet which represents each alphabet.
  • Line 6: We use the syntax explained earlier to print lowercase alphabets.
  • Line 12: We also create a variable called uppercaseAlphabet which represents each uppercase alphabet.
  • Line 13: We use the same syntax and logic to print all the uppercase alphabets.

Free Resources