Instead of writing F or B, sometimes people use E♯ or C♭ to write these notes. Are there any advantages to this notation?
NReilingh gave a good general-case answer. I'll give you a specific case just to demonstrate that the concept is useful.
First consider a C major chord. C-E-G, right? Then you make it into a minor chord by flattening the third, to get C-E♭-G. So far, so good.
Now, consider an A♭ major chord: it's spelled A♭-C-E♭. But what happens when you want to make it minor? You need to flatten the third again, so you get A♭-C♭-E♭.
You might think that it is somehow "simpler" to spell it as a B, but this is only superficial. It would imply, for example, that the first two notes (A♭ and B) are adjacent to each other in the scale -- that there is no note in between them (because, after all, A and B are adjacent in the alphabet). But if you look at the full scale, there is a note between them: B♭. The whole point of a "diatonic" scale is that you can only have a single note for each letter. So if 'B' is already used in a B♭, then the next note higher needs to be some kind of C. And since it is a half step lower than a regular C, it must be a C♭.
Similarly, the whole point of a tertian chord (such as a triad) is that it's built from "every other note" of a scale (e.g. a "stack of thirds"), so seeing some kind of A next to some kind of B sends a musician a signal that this isn't a proper third, and that it might instead be some type of inversion or suspension, which wouldn't be the case.
So in short, you have to look at the note's position within the context of a complete scale to know how it is properly named.
B and Cb are different notes. One is a kind of B and the other is a kind of C. Information about harmony is contained both in the note name and any accidental alterations to it — C to any kind of E is a third, and C to any kind of F is a fourth, and those intervals have different meanings, even if they sound "the same".
And these pitches are only the same when played in 12-tone equal temperament. That's not the only tuning system out there.
Music is fundamentally made up of intervals, which are ratios of pitches (sound frequencies). The "simpler" the ratio, as in a fraction with smaller numbers, the more consonant the interval. For example: the perfect octave is 2:1, the perfect fifth is 3:2, major third is 5:4, the diminished fourth is 32:25.
To produce music, we chain the intervals together, and at least in tonal music, we want them to all be relatively consonant. The problem is, the number of pitches that can be reached by combining intervals is infinite, so to keep things manageable we limit the pitches by enharmonically using the same pitches for D♯ and E♭, amongst other pairs.
In diatonic music in standard Western music theory, we use a sequence of tones to build our scales, and from that heritage we label the notes with the familiar letters A-G for the seven steps in a scale. From C major, the whole tones are C-D, D-E, F-G, G-A, A-B, and the other two are the diatonic semitones E-F and B-C (diatonic means "between tones"). These two intervals form the basis of diatonic music.
Music would be a bit more boring if we couldn't change keys. In doing so, there is another type of semitone, the chromatic semitone. This is from D♭-D and D-D♯, and all other analogous intervals. Note how the letter does not change, but you add an accidental; "chromatic" relates to the note changing colour on a keyboard. The main relationship between the three is that:
- Diatonic semitone + chromatic semitone = whole tone
From this we can figure out that B-C♯ is a whole tone, C♯-D is a diatonic semitone, and so forth. Chromatic alterations are "unnatural" relative to the diatonic framework (look at the name "accidental"), and so an important rule is the following:
- Diatonic semitones are NOT equivalent to chromatic semitones
So even though because of enharmonic equivalence, D♯ and E♭ may be played the exact same way, in the musical scale and context, they are not the same musical note.
Here's one way you can prove it on any instrument of your choice:
- Play the B major scale (B-C♯-D♯-E-F♯-G♯-A♯-B)
- Play the major third B-D♯
- Now play the C harmonic minor scale (C-D-E♭-F-G-A♭-B-C)
- Play the diminished fourth B-E♭
The two intervals B-(D♯/E♭) sound drastically different, even though you are playing the exact same keys! The reason is, you are aware of the musical context (whichever scale is being used), and your brain will interpret the interval as constructed appropriately within that particular scale. Notice that the two scales have 5 sharps and 3 flats respectively: they are about as far apart as two scales could get.
To recap, writing A♭-B-E♭ is incompatible with the implied musical context of A♭ minor, and even if on paper it's easier to write, your brain will interpret it as A♭-C♭-E♭ anyway, because it leads to using less musically distant accidentals (which require "borrowing" notes from scales with very different key signatures, or, if you're fancy, modulation).
End note: the concept of whole tones only applies to meantone tuning systems, of which 12-EDO (the familiar twelve equal semitones in an octave) is one. This specifically means 9:8, the major tone, and 10:9, the minor tone, are conflated. Their difference is illustrated by four consecutive fifths vs. two octaves and a major third (the math ratios are inconsistent).
You're asking quite an advanced question to which there can be many different answers, all true; the idea is the harmonic context. As the man said, in a scale there is A B C♯ D E F♯ G♯ A. Now clearly that last G♯ couldn't be A♭, because the scale demands that the note before the top A, be a G. But if it's a normal G, the scale doesn't come out right. So we must make it a G♯.
Now let's say there's a chord in a piece of music where the harmonic context requires C flat minor. This isn't a key you will ever find a piece of music in, but it is nonetheless a key; and in some rare harmonic contexts, you will find a chord or some notes in C flat minor. Then we will find C♭ as the tonic; and what will be the seventh note in that scale? In C♭ harmonic minor the seventh note of the scale will be B♭. In C♭ melodic minor the seventh note of the scale descending will be B♭♭, B double-flat. On the piano keyboard, it is the same note as A, but it isn't really; it's a very different note; it's B double-flat. The note before C is B, but it has to be changed if it is to be right for C flat minor. Someone playing the violin would be able to show you the difference between A and B double-flat in the harmonic context of C flat melodic minor. Perhaps you could find a competent violinist and ask. Hope that helps.
One way of thinking about it is to avoid the equal temperament trap and assuming things like G#=A♭. This is not the case. The theory side of it is based on harmonic context in which you can not have 2 of the "same" note (e.g. D♭ and D#) in one scale. For instance, the A♭m (aeolian) scale goes as such:
A♭, B♭, C♭, D♭, E♭, F♭, G♭, A♭.
You cannot have B♭ and B in the same scale so C♭ is used.
The other aspect is in en-harmonics. I am not an expert but I know that C♭ is slightly sharper than a B by as little as a quarter or fifth of a semitone. Without en-harmonics such as this, you have equal temperament which is technically out of tune as none of the notes will be absolutely as they should be as the note A for instance will be a different frequency depending on what chord is being played, what key the music is in, and what octave it is at, even if the instrument is tuned perfectly in all cases.
En-harmonics are fascinating and I suggest looking more into them. But the root of my answer here is that the advantage of using notes such as C♭ and E# is that they are the correct way to write them if you are playing a chord of A♭m or D♭ respectively.