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 lowercasefor(alphabet = "a"; alphabet <= "z"; alphabet++)// for uppercasefor(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 alphabetsSystem.out.println("lowercase alphabets:");char lowercaseAlphabet;for(lowercaseAlphabet = 'a'; lowercaseAlphabet <= 'z'; lowercaseAlphabet++){System.out.print(lowercaseAlphabet);}System.out.println("---------------------");// print uppercase alphabetsSystem.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
lowercaseAlphabetwhich represents each alphabet. - Line 6: We use the syntax explained earlier to print lowercase alphabets.
- Line 12: We also create a variable called
uppercaseAlphabetwhich represents each uppercase alphabet. - Line 13: We use the same syntax and logic to print all the uppercase alphabets.