Java Program to Display Characters from A to Z using Loop

1. Introduction

In Java, characters are represented using a Unicode character set. Each character corresponds to a unique Unicode value. The characters 'A' to 'Z' have Unicode values from 65 to 90. In this blog post, we will write a Java program that uses a loop to display characters from 'A' to 'Z'.

2. Program Steps

1. Define a class named DisplayCharacters.

2. Inside the main method, use a for loop.

3. Initialize the loop variable i to 65 (Unicode value of 'A').

4. Continue the loop as long as i is less than or equal to 90 (Unicode value of 'Z').

5. In each iteration, cast i to char and print the character.

6. Increment i in each iteration.

3. Code Program

public class DisplayCharacters { // Step 1: Define a class named DisplayCharacters

    public static void main(String[] args) { // Main method

        // Step 3: Initialize i to 65 and Step 4: Continue as long as i is less than or equal to 90
        for (int i = 65; i <= 90; i++) {
            // Step 5: Cast i to char and print the character
            System.out.print((char) i + " ");
        }
    }
}

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

4. Step By Step Explanation

Step 1: The class DisplayCharacters is defined.

Steps 3 and 4: Inside the main method, a for loop is used, initializing i to 65 and continuing as long as i is less than or equal to 90.

Step 5: In each iteration of the loop, i is cast to char to convert the Unicode value to a character, which is then printed.

Step 6: The loop variable i is incremented in each iteration, moving on to the next character in the sequence from 'A' to 'Z'.

Comments