C++ Code Example: Display Characters from A to Z using for loop

In this code example, all letters of the alphabet are passed through and output with a for loop.

Code Example

#include <iostream>
using namespace std;

int main () {
    char c;

    for (c = 'A'; c <= 'Z'; c++) {
        cout << c << ", ";
    }
    
    return 0;
}

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

Code Explanation

Here’s how the code works:

  1. Declare a char variable c.
  2. In the for loop header, set the initial value of c to 'A'.
  3. The loop will execute as long as c is less than or equal to 'Z'.
  4. In each iteration of the loop, the current value of c is printed to the console, followed by a comma and a space.
  5. After the loop has finished executing, the program returns 0 to indicate successful completion.