Handling an 'else' type situation within a loop
<p>I have a homework problem for my C++ class and the problem wants us to have the user input a wavelength and then output the correct type of radiation. <strong>The point to notice is that there are more Wave Name values than there are Wave Lengths.</strong></p>

<p>My solution is listed below:</p>

<pre><code>const double WAVE_LENGTH[] = { 1e-11, 1e-8, 4e-7, 7e-7, 1e-3, 1e-2 };
const char* WAVE_NAME[] = { "Gamma Rays", "X Rays", "Ultraviolet", "Visible Light", "Infrared", "Microwaves", "Radio Waves" };

double waveLength;

std::cout &lt;&lt; "Enter a wavelength in decimal or scientific notation\nWavelength: ";
std::cin &gt;&gt; waveLength;

for (unsigned short i = 0U; i &lt; 6U; ++i)
{
    if (waveLength &lt; WAVE_LENGTH[i])
    {
        std::cout &lt;&lt; "The type of radiation is " &lt;&lt; WAVE_NAME[i] &lt;&lt; std::endl;
        break;
    }
    if (i == 5U) // Last iteration
        std::cout &lt;&lt; "The type of radiation is " &lt;&lt; WAVE_NAME[i + 1] &lt;&lt; std::endl;
}
</code></pre>

<p>My question is regarding my approach at solving the problem, specifically within the loop. I can't seem to find a way to handle all the situations without creating two conditions inside the loop which seems like it is a poor design. I realize I could use a series of if/else if statements, but I figured a loop is cleaner. Is my approach the best way or is there a cleaner way of coding this?</p>

<p>Thanks!</p>
