In this java program, you’ll learn how to display or print uppercase alphabets(A to Z) and lowercase alphabets(a to z) using Loops. This post explains the three possible ways to display or print alphabets using loops: for
, while
, and do..while
Here are the examples to display the alphabets using all three loops in java
In this example, we are using two for
loops, one is used to print upper case letters (A to Z) and another to print lower case letters(a to z) using for
loop.
// Java Program to Display Alphabets (A to Z) Using While Loop
public class JavaPrograms {
public static void main(String[] args) {
char character;
// Display Upper Case Alphabets
System.out.print("Upper Case Alphabets Displayed \n");
for (character = 'A'; character <= 'Z'; ++character)
System.out.print(character + " ");
System.out.print("\n\n");// move to next line
// Display Lower Case Alphabets
System.out.print("Lower Case Alphabets Displayed \n");
for (character = 'a'; character <= 'z'; ++character)
System.out.print(character + " ");
}
}
Upper Case Alphabets Displayed
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
Lower Case Alphabets Displayed
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
In this example, we are using two while
loops, one is used to print upper case letters (A to Z) and another to print lower case letters(a to z) using a while
loop.
// Java Program to Display Alphabets (A to Z) Using While Loop
public class JavaPrograms {
public static void main(String[] args) {
char character = 'A';
// Display Upper Case Alphabets
System.out.print("Upper Case Alphabets Displayed \n");
while (character <= 'Z') {
System.out.print(character + " ");
character++;
}
System.out.print("\n\n");// move to next line
// Display Lower Case Alphabets
character = 'a';
System.out.print("Lower Case Alphabets Displayed \n");
while (character <= 'z') {
System.out.print(character + " ");
character++;
}
}
}
Upper Case Alphabets Displayed
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
Lower Case Alphabets Displayed
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
In this example, we are using two do..while
loops, one is used to print upper case letters (A to Z) and another to print lower case letters(a to z) using do..while
loop.
// Java Program to Display Alphabets (A to Z) Using Do While Loop
public class JavaPrograms {
public static void main(String[] args) {
char character = 'A';
// Display Upper Case Alphabets
System.out.print("Upper Case Alphabets Displayed \n");
do {
System.out.print(character + " ");
character++;
} while (character <= 'Z');
System.out.print("\n\n");// move to next line
// Display Lower Case Alphabets
character = 'a';
System.out.print("Lower Case Alphabets Displayed \n");
do {
System.out.print(character + " ");
character++;
} while (character <= 'z');
}
}
Upper Case Alphabets Displayed
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
Lower Case Alphabets Displayed
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
Comments