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.
// for lowercasefor(alphabet = "a"; alphabet <= "z"; alphabet++)// for uppercasefor(alphabet = "A"; alphabet <= "Z"; alphabet++)
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.
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);}}}
In the code above:
lowercaseAlphabet
which represents each alphabet.uppercaseAlphabet
which represents each uppercase alphabet.