Leveraging the Power of Conditional or Ternary Operators

Hi everyone. I'm new to this forum, and fairly new to C++, having gravitated toward it from Java due to some of it's limitations. After making an interesting discovery about ternary or conditional operators, as they are sometimes called, I developed this simple program which demonstrates their power. In some cases, it merely condenses what would otherwise be more extensive if/else statements, but in a few cases they provide even greater efficiency. I've encountered many who know little about them, or comprehend them but don't see the benefits, so I figured this heavily commented example could aid others, particularly beginners, in seeing their potential uses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*  Conditional/Ternary Operator Madness

    A simple sample console program with heavy
    commenting and strategic spacing, designed
    to demonstrate the potential uses of the
    conditional or ternary operators.       */

//required libraries
#include <cstdlib>//required for rand function
#include <iostream>//required for cout function
#include <ctime>//required for time function
//required to prevent the need for repeated use of 'std::'
using namespace std;

//global variables using deliberately descriptive names
int num,one,two,three,four,five,six,seven,eight,nine,ten;

//function to output random numbers within a specific range
int random(int low,int high)
{
    return rand()%(high-low+1)+low;
}


int main()
{
    srand(time(NULL));//set random number seed

    //loop ten times, generating random number between 1 and 10,
    //while counting the frequency of each number generated
    for (int i=1; i<11; i++)
    {
        num=random(1,10);//assign variable a random number between 1 and 10

        //conditional or ternary operator used to increment correct variable
        ((num==1) ? one  : (num==2) ? two : (num==3) ? three : (num==4) ? four :
         (num==5) ? five : (num==6) ? six : (num==7) ? seven : (num==8) ? eight:
         (num==9) ? nine : ten)++;

        //conditional or ternary operator used to add spacing
        //when the number of iterations is even
        cout<<((i%2) ? "" : "\t");
        cout<<i;//output number of iterations through the loop
        //conditional or ternary operator used to output proper
        //suffix such as 1st, 2nd, 3rd, etc.
        cout<<((i==1) ? "st" : (i==2) ? "nd" : "th");
        //conditional or ternary operator used to correct spacing on 10th iteration
        cout<<((i!=10)? "  " : " ");
        cout << "Number Generated:\t" << num;//output number generated with spacing

        //conditional or ternary operator used to go to next line
        //when the number of iterations is even
        cout<<((i%2) ? "" : "\n");
    }//end of first loop

    cout<<"\n\t\tNumber:     Frequency:";

    //loop ten times, outputting each number and its frequency by percentage
    for (int i=1; i<11; i++)
    {
        //go to next line while adding indentation
        cout<<endl<<"\t\t  ";
        cout<<i<<"          ";//output number of iterations and add spacing
        //use conditional or ternary operator to alter spacing on 10th iteration
        //(the reverse of it's previous usage)
        cout<<((i==10)? " " : "  ");
        //use conditional or ternary operator to output frequency of each value by percentage
        cout<<((i==1) ? one   : (i==2) ? two   : (i==3) ? three :
               (i==4) ? four  : (i==5) ? five  : (i==6) ? six   :
               (i==7) ? seven : (i==8) ? eight : (i==9) ? nine  : ten)<<"0%";
    }//end of second loop
    cout<<"\n\n\n\n\n";//add empty lines before program termination
}
Last edited on
Topic archived. No new replies allowed.