Teaching syntactic sugar When I show my code to students I tutor, they ask about "all those things in my code", which refers (quite vaguely) to things such as shortened ifs, post\preincrement etc.

They are confused by these things, but once I explain those things, I see that the students constantly use them (not in excess).

This got me wondering what are some of the benefits of teaching syntactic sugar during regular teaching (as part of the lesson plans in the curriculum). What might the students gain from learning syntactic sugar during lessons?

Examples of things I had in mind:

int x;
if(condition){
    x=5;
} else{
    x=2;
}


is identical to:

int x = condition ? 5 : 2;


Also:

int epoch = 0;

do{
    someEpochDepenentOperation(epoch,x);
} while(++epoch &lt; 100);


would be:

int epoch = 0;

do{
    someEpochDepenentOperation(epoch,x);
    epoch++;
} while(epoch &lt; 100);
The ?: operator is not syntactic sugar (OK in most languages it is, and no guarantees/checks are made). It is a different construct. 

if predicate then action1 else action2 is procedural

predicate ? value1 : value1 or value1 if predicate else value2 is functional

The first does something, the second evaluates to something. The value is that the functional form tells you that you are doing something functional, that there are no side effects. Thus making the code easier to read (once you have the vocabulary).

In this example

int x;
if(condition){
    x=5;
} else{
    x=2;
}


the reader must ask, can x remain uninitialized? What are all the values that x can take? Does anything else change or happen?

Where as for this example

int x = condition ? 5 : 2;


We see that we initialize x, to 5 or 2. That is it. We could also write

const int x = condition ? 5 : 2;


This tells us that x is x: it never changes.