Pages

Monday, September 25, 2017

Easy tutorial for C / C ++ Control statement 2 (switch case statements)

Easy tutorial for C / C ++ Control statement 2 (switch case statements)

switch case statements:

- As a conditional control statement, switch case statements execute a specific execute statement when a specific value is encountered.

- The structure of the switch case statement is as follows.

switch ('expression') {

case  n1 : ' execute statement 1';
          break;

case  n2 : ' execute statement 2';
          break;

case  n3 : ' execute statement 3';
          break;

default: 'execute statement 0';

}

- If the value of 'expression' in the ' switch statement ' equals to n1, 'execute statement 1' is executed. The 'switch statement' ends by a 'break' statement. That is, other execution statements are not executed.

- When the value of 'expression' equals to n2, 'execute statement 2' is executed. And the 'switch statement' ends by a 'break' statement.

- If the value of 'expression' is not n1, n2 or n3, 'execute statement 0' is executed.

- If there is no 'break' statement in the case statements, the  ' switch statement ' does not exit immediately, but executes the execute statements until it encounters the 'break' statement or until it exits the switch statement.

Example Code

#include <iostream>
using namespace std;

int main() {

     int A, B;

          A = 3 ;
          B = 1 ;

   switch(A){
         case 1 : cout<<" A = 1 ! "<<endl ;
                  break;
         case 2 : cout<<" A = 2 ! "<<endl ;
                  break;
         case 3 : cout<<" A = 3 ! "<<endl ;
                  break;
         default : cout<<" Wow"<<endl ;
                            }

   switch(B){
         case 1 : cout<<" B = 1 ! "<<endl ;
                  break;
         case 2 : cout<<" B = 2 ! "<<endl ;
                  break;
         case 3 : cout<<" B = 3 ! "<<endl ;
                  break;
    default : cout<<" Wow "<<endl ;
                            }
          return 0;
}

results:

A = 3 !
B = 1 !

- 3 and 1 were assigned to integer type variables A and B, respectively.

- Since the value of A in the first 'case' statement is 3, "A = 3 !" corresponding to case 3 is printed.

- In case of B, since the value is 1, "B = 1 !" is printed.


- I will remove the break statement and execute above example.

Example Code

#include <iostream>
using namespace std;

int main() {

     int A, B;

          A = 3 ;
          B = 1 ;

   switch(A){
         case 1 : cout<<" A = 1 ! "<<endl ;
         case 2 : cout<<" A = 2 ! "<<endl ;
         case 3 : cout<<" A = 3 ! "<<endl ;
         default : cout<<" Wow"<<endl ;
                            }

   switch(B){
         case 1 : cout<<" B = 1 ! "<<endl ;
         case 2 : cout<<" B = 2 ! "<<endl ;
         case 3 : cout<<" B = 3 ! "<<endl ;
         default : cout<<" Wow "<<endl ;
                           }
          return 0;
}

results:

 A = 3 !
 Wow
 B = 1 !
 B = 2 !
 B = 3 !
 Wow

- If you remove the break statements in the previous example and execute it, you get the above results.

- First, in case A, the value of A is 3, so "A = 3!" is printed.
- However, since there is no break statement, the following execute statement is executed and thus "Wow" is printed.

- In case of B, the execution statement of case 1 is executed and "b = 1!" is printed.

- However, since there is no break statements, the remaining execution statements continue to execute.

No comments:

Post a Comment