Program: for loops and character symbols



This short program uses for loops to display three different sequences of characters.



Note that for loops can work directly with characters, they are not restricted to working with integer values. The first loop begins with character value A, proceeding on to the next character until reaching Z.


As you can see from the screenshot above, the program can be compiled and executed on an Apple Mac. It will also work on a Raspberry Pi or on a PC with Ubuntu.




Select and copy the C++ source-code at the bottom of this post.
Paste into a text editor, such as Nano or Geany.


Then save the new file, ending in .cpp


I used chars.cpp


To compile from the command-line:


    g++ -o chars.cpp chars


To run from the command-line:


    ./chars




Here's the code:


#include <iostream>    // Displaying text as output on screen


using namespace std;    // Simple names for cout and endl


int main()
{

    char symbol;




    // Display all capital letters
    for ( symbol = 'A'; symbol <= 'Z'; symbol++ )
        cout << symbol;





    // End of the line, move on to the next one
    cout << endl;





    // Display lower-case letters
    for ( symbol = 'a'; symbol <= 'z'; symbol++ )
        cout << symbol;



    cout << endl;




    // Display all numeric digits IN REVERSE ORDER!
    for ( symbol = '9'; symbol >= '0'; symbol-- )
        cout << symbol;



    cout << endl;




    return 0;
}



Find out more - buy the book on Amazon...