Java Program to Display Alphabets using loop

Java Program to Display Alphabets using loop

What is  a Character?

In Java, a char is a data type that represents a single character. It is a type of primitive data type.
The char data type is used to store character values, such as letters, digits, and symbols. It is represented by the char keyword in Java and can hold any value in the Unicode character set, which includes characters from many different languages and scripts.

In this Article, we will write a program using different methods to Display Alphabets using loop.

What is an Alphabet in Java?

In Java, the alphabet refers to the set of letters that are used in the language to represent characters. In the Java programming language, characters are represented using the char data type, which is a 16-bit Unicode character. The alphabet in Java includes the 26 letters of the English alphabet (A-Z and a-z), as well as various symbols and special characters.

Program to Display Alphabets using loop :

We can print two types of Alphabets in java, these are :

  • Lowercase Alphabets
  • Uppercase Alphabets.

Example using for loop for Lowercase Alphabets :

Run
import java.util.*;

public class Main {
  public static void main(String[] args) {
      
    // Initializing initial character for the loop
    char c;
    
    // Loop to prints Alphabets from A to Z
    for (c = 'a'; c <= 'z'; c++) {
        
      // Printing the lowercase character    
      System.out.print(c + " ");
    }
  }
}
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

Explanation:

This program uses a for loop with an initialization of char c = ‘a’ which is the starting point of lowercase alphabets, the condition of the loop is c <= ‘z’ which is the ending point of lowercase alphabets. Each time the loop iterates, the variable c is incremented by 1, which corresponds to the next letter in the alphabet.
The System.out.print(c + ” “) is used to print the alphabets on the same line with a space between each alphabet.

Example using for loop for Uppercase Alphabets :

Run
import java.util.*;

public class Main {
  public static void main(String[] args) {
      
    // Initializing initial character for the loop
    char c;
    
    // Loop to prints Alphabets from A to Z
    for (c = 'A'; c <= 'Z'; c++) {
        
      // Printing the Uppercase character    
      System.out.print(c + " ");
    }
  }
}
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

Explanation:

This program uses a for loop with an initialization of char c = ‘A’ which is the starting point of uppercase alphabets, the condition of the loop is c <= ‘Z’ which is the ending point of uppercase alphabets. Each time the loop iterates, the variable c is incremented by 1, which corresponds to the next letter in the alphabet.
The System.out.print(c + ” “) is used to print the alphabets on the same line with a space between each alphabet.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription