English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin program uses loops to display characters from A to Z

Kotlin Examples in Full

In this program, you will learn to use while loops to print English letters in Kotlin. You will also learn to print only uppercase and lowercase letters.

Example1Display uppercase A to Z

fun main(args: Array<String>) {
    var c: Char
    c = 'A'
    while (c <= 'Z') {
        print("$c ")
        ++c
    }
}

When the program is run, the output is:

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

Like Java, you can use a for loop to iterate from A to Z, because they are stored as ASCII characters in Kotlin.

Therefore, internally, you can loop to display65to90 to print English letters.

Here is the equivalent Java code:Java Program to Display English Letters

Make slight modifications, and you can display lowercase letters, as shown in the following example.

Example2:Display lowercase a to z

fun main(args: Array<String>) {
    var c: Char
    c = 'a'
    while (c <= 'z') {
        print("$c ")
        ++c
    }
}

When the program is run, the output is:

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

You just need to replace 'A' with 'a' and 'Z' with 'z' to display lowercase letters. In this example, in the inner loop97to122.

Kotlin Examples in Full