0% found this document useful (0 votes)
712 views7 pages

The Switch Statement in C++

The switch statement in C++ simplifies complex if-then-else structures. The variable to be tested comes after the switch, and can be a variable, expression, or function result. Different code blocks are executed depending on the case, with an optional default. If-then-else statements allow executing code conditionally based on boolean tests, and can be nested to deep levels with multiple statements enclosed in curly braces.

Uploaded by

vikramuk
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
712 views7 pages

The Switch Statement in C++

The switch statement in C++ simplifies complex if-then-else structures. The variable to be tested comes after the switch, and can be a variable, expression, or function result. Different code blocks are executed depending on the case, with an optional default. If-then-else statements allow executing code conditionally based on boolean tests, and can be nested to deep levels with multiple statements enclosed in curly braces.

Uploaded by

vikramuk
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

The Switch statement in

C++
The switch statement
simplifies complicated 'if then
else' constructs in C++.  

The variable to be tested


comes after the switch
statement, and it can be
variable, an expression or the
result of a function.
switch(x)
  {
     case 1 :
        {
           dosomecode1;
           break;
        }
     case 2 :
        {
           docodefor2;
           break;
        }
     case 3 :
     case 4 :
     case 5 :
     case 6 :
        {
            docodefor3456;
            break;
        }
     default :
        {
            dodefaultcode;
        }
   }
If – Then - Else
The simplest form of an 'if - then' construct
expressed in C is as follows.     
If (x == 10)
     DoSomething(x);

If an if - then type construct has more than


one statement to execute C uses curly
braces to enclose the statements as
follows...

If (x > 10)
     {
        DoFirstThing;
If – Then - Else
C also allows the use of any non
zero variable as the test in an if
statement.  As long as the
variable is non-zero the test
evaluates true and the following
statement executes.

•     If (x) DoSomething;


• The exclamation point indicates the not
statement in C.  In the following example if 'not x'
 the statement following will execute
   If (!x) DoSomething;

The else construct uses the following syntax...


•    If (x == 20)
     {
        DoSomething;
        DoAnotherThing;
     }
   else
     {
        DoSomethingElse;
        DoSomeOtherThing;
     }
• If statements can be nested as follows...
•    If (x > 10)
     If (x < 20)
         DoSomething;

• Nesting can also become more complex by


including multiple statements, which then
requires the use of the curly braces to
delimit the blocks of code...
•     if (x > 10)
      {
         DoSomethingOne;
         If (x < 20)
           {
              DoSomeOtherthing;
             If (x = 15)
               {
                   DoThatThing;
                   DoThisThing;
               }
            }
       }
   
else  if (x > 0)
         {
            DoSomethingElse;
            If (x = 1)
              {
                 DoWhatEver;
                 DoWhatEverElse;
              }
         }
   else {
            DoThis;
            DoThat;
          }

You might also like