Printing the Alphabets A-Z using loops in Java

Hey Everyone! In this tutorial, we’ll be printing the Alphabets from A-Z using loops concept in Java.

How to Print Alphabets A-Z using loops in Java

To understand this program we must first understand the working of a “for loop”.

FOR LOOP

It is used to iterate statements or a part of the program several times until the desired condition is met.
The syntax of this loop is:  for(initialisation; condition; iteration).

In this example, we initialise the variable with the starting letter ‘A’ and iterate over the variable until ‘Z’ is encountered and for each iteration, we print the value of the variable.

We use the format specifier “%c”.
%c specifies that the single variable is a character.
Hence must use System.out.printf() to print each character.

Here is the source code for our “.java” file:

Java Code for Printing Alphabets from A to Z :

class Alphabets
{
  public static void main(String[]args)
  {
    System.out.print("Printing Alphabets A-Z:"+"\n");  // we use the escape sequence \n to print the characters in a new line.
    for(int i = 'A' ; i <= 'Z'; i++)  // initialise the variable with the starting letter and iterate the loop till the desired character occurs.
    {
      System.out.printf("%c ", i);  // A space should be given beside %c to maintain the spacing between letters.
    }
  }
}

Output:

How to Print Alphabets A-Z using loops in Java

Hence we print alphabets A to Z using loops. Hope you understand the code 🙂

Learn more:

Leave a Reply

Your email address will not be published. Required fields are marked *