Java Print Alphabet Sample

The Java sample code below prints out the alphabet to the screen in both upper case and lower case. You can accomplish this task using either a standard for loop or a while loop. I printed the alphabet out on a single line for visual purposes using the System.out.print() method. You can use the System.out.println() method to print out each character on a new line.


Java Print Alphabet Source Code

package com.jcd.java.tutorials;

public class JavaPrintAlphabet 
{

	public static void main(String[] args) 
	{

		//Define a character variable
		char jcdChar;
		
		System.out.println("Print Lower Case Alphabet For Loop: ");
		 
		//Use standard for loop to 
	    for( jcdChar = 'a' ; jcdChar <= 'z' ; jcdChar++ )
	    {
	         System.out.print(jcdChar);
	    }
	    
	    System.out.println("\n\nPrint Upper Case Alphabet For Loop: ");
		 
		//Use standard for loop to 
	    for( jcdChar = 'A' ; jcdChar <= 'Z' ; jcdChar++ )
	    {
	         System.out.print(jcdChar);
	    }
	    
	    
	    //Lower Case Using While Loop Instead 
	    char jcdCharLower = 'a';
	    
	    System.out.println("\n\nPrint Lower Case Alphabet While Loop: ");
	    
	    while (jcdCharLower <= 'z') 
	    {
	      System.out.print(jcdCharLower);
	      
	      //Increment char
	      jcdCharLower++;
	    }
	    
	    //Upper Case Using While Loop Instead 
	    char jcdCharUpper = 'A';
	    
	    System.out.println("\n\nPrint Upper Case Alphabet While Loop: ");
	    
	    while (jcdCharUpper <= 'Z') 
	    {
	      System.out.print(jcdCharUpper);
	      
	      //Increment char
	      jcdCharUpper++;
	    }
	    
	}

}


Java Print Alphabet Output

Print Lower Case Alphabet For Loop: 
abcdefghijklmnopqrstuvwxyz

Print Upper Case Alphabet For Loop: 
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Print Lower Case Alphabet While Loop: 
abcdefghijklmnopqrstuvwxyz

Print Upper Case Alphabet While Loop: 
ABCDEFGHIJKLMNOPQRSTUVWXYZ


Add a Comment

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