Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The switch statement is used to execute one block of code dependent on a particular value. In a sense, the switch statement can be thought of as a form of an if statement: the code</p> <pre><code>switch (avg) { case 1 : { /* code block 1 */ } break; case 2 : { /* code block 2 */ } break; default : { /* code block default */ } break; } </code></pre> <p>can be read as</p> <pre><code>if (1 == avg) { /* code block 1 */ } else if (2 == avg) { /* code block 2 */ } else { /*code block default */ } </code></pre> <p>Your switch statement can be read as </p> <pre><code>if (1 == avg) { if ((avg &gt;= 80) &amp;&amp; (avg &lt;= 100)) { cout &lt;&lt; " Your Average is: " &lt;&lt; avg &lt;&lt; endl; cout &lt;&lt; "You Grade is A+ " &lt;&lt; endl; } } else if... </code></pre> <p>and there's no way avg can == 1 and be greater than 80 and less than 100, which is why you don't get any output. </p> <p>In C++, the switch statement doesn't lend itself well to testing for ranges; I'd just use an if statement:</p> <pre><code>if ( (avg&lt;=100) &amp;&amp; (avg &gt;=80)) { // you get an A } else if ... </code></pre> <p>But, if you really really need to use a switch, there are a few ways to go about it:</p> <pre><code>switch (avg) { case 100: case 99: case 98: ... case 80 : { /* you get an A+ */ break; } case 79 : case 78 : ... case 70 : { /* you get a A */ break: } etc. </code></pre> <p>This is ugly, but it <em>is</em> a switch statement. A cleaner way to use the switch statement could be to "force" avg to a discrete value, instead of a range. </p> <pre><code>int percentile = avg / 10; // integer division. </code></pre> <p>Now range is going to be between 0 and 10, assuming avg is between 0 and 100. This will make for a slightly cleaner switch:</p> <pre><code>switch (percentile) { case 10 : case 9: case 8: /* You get an A+ */ break; case 7: /* You get an A */ break; case 6: /* You get a B+ */ break; etc. </code></pre> <p>Hope this is helpful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload