Switch Case



  http://eglobiotraining.com/
http://eglobiotraining.com/
• In programming, a switch, case, select or inspect
  statement is a type of selection control mechanism
  that exists in most imperative programming languages
  such as Pascal, Ada, C/C++, C# programming language
  , Java, and so on. It is also included in several other
  types of programming languages. Its purpose is to
  allow the value of a variable or expression to control
  the flow of program execution via a multiway branch
  (or "goto", one of several labels). The main reasons for
  using a switch include improving clarity, by reducing
  otherwise repetitive coding, and (if the heuristics
  permit) also offering the potential for faster execution
  through easier compiler optimization in many cases.

                      http://eglobiotraining.com/
•     char key;
•   C#                                            •     printf("Enter an arithmetic operatorn");
•                                                 •     scanf("%c",&key);

•   In C# programming, every case block           •     switch (key)
    that contains any statements must             •     {
                                                  •       case '+':
    have a reachable end point, or                •         add();
    triggers a compilation error.In               •         break;
    programming usually, this is a break          •         case '-':
    statement, but any jump statement             •           subtract();
    can be used – such as return, goto or         •           break;
    throw – or the switch can simply end          •         case '*':
    with an infinite loop. Case fall-             •           multiply();
    through is only permitted when there          •           break;

    are no statements between one case            •         case '/':
    statement and the next. If fall-              •           divide();
                                                  •           break;
    through is otherwise desired, it must
    be made explicit with the goto case           •         default:
    construct. C# programming also                •          printf("invalid keyn");
                                                  •          break;
    allows the use of non-integer case            •     }
    values, such as Strings.

                              http://eglobiotraining.com/
•    Select Case n
                                            •     Case Is < -5
                                            •       MsgBox("n is less than -5")
• Visual Basic .NET                         •     Case -4 To -1
                                            •
•                                           •
                                                    MsgBox("n is between -4 and -1")
                                                  Case 0
• In programming, Visual Basic              •       MsgBox("n is 0")
                                            •     Case 2, 4, 6, 8
  .NET, the switch statement is             •       MsgBox("n is even")
  called "Select Case                       •     Case 1, 3, 5, 7, 9
  programming", and fall-through            •       MsgBox("n is odd")
                                            •     Case Else
  to later blocks is not supported.         •       MsgBox("only single-digit numbers are
  However, ranges and various                    allowed.", vbCritical)
                                            •    End Select
  constructs from If statements are         •
  both supported                            •    Visual FoxPro
                                            •
                                            •    Visual FoxPro:
                                            •
                                            •    Do Case
                                            •    Case field_1 = "X"
                                            •     Replace field_b With 1
                                            •    Case field_1 = "Y"
                                            •     Replace field_b With 2
                                            •    Case field_1 = "Z"
                                            •     Replace field_b With 3
                                            •    Endcase




                            http://eglobiotraining.com/
•     #include <iostream>
                                                              using namespace std;
                                                              int main(void)
•   Haskell                                                   {
                                                                char grade;
                                                                cout << "Enter your grade: ";
                                                                cin >> grade;
                                                                switch (grade)
•   Haskell's case construct, unlike C-influenced               {
    programming languages, has no fall-through                  case 'A':
    behaviour. It is a programming expression which               cout << "Your average must be between 90 - 100"
                                                                     << endl;
    returns a value, and it can deconstruct values                break;
    using pattern matching.                                     case 'B':
                                                                  cout << "Your average must be between 80 - 89"
                                                                     << endl;
                                                                  break;
                                                                case 'C':
                                                                  cout << "Your average must be between 70 - 79"
                                                                     << endl;
                                                                  break;
                                                                case 'D':
                                                                  cout << "Your average must be between 60 - 69"
                                                                     << endl;
                                                                  break;
                                                                default:
                                                                  cout << "Your average must be below 60" << endl;
                                                                }
                                                                return 0;
                                                              }




                                          http://eglobiotraining.com/
•     #include<iostream>
                                                  •             using namespace std;

•   Pascal                                        •             int main()
                                                  •             {
                                                  •                          int a;
•   In Programming, Pascal does not allow         •                          cin >> a;
    “fall through”, but has ranges and            •                          if ( a <= 10 )
    comma separated literal lists.                •                          {
                                                  •                                           cout << "Below
                                                       10" << 'n';
                                                  •                          }
                                                  •                          else
                                                  •                          {
                                                  •                                           if ( a < 60 )
                                                  •                                           {
                                                  •
                                                                cout << "Below 60" << 'n';
                                                  •                                           }
                                                  •                          }
                                                  •                          return 0;
                                                  •             }




                                  http://eglobiotraining.com/
•    #include<iostream>
                                                        •              using namespace std;

                                                        •             int main()
                                                        •             {
                                                        •                             char myinput;
                                                        •                             cin >> myinput;
•   Perl
•                                                       •                             switch (myinput)
•   Perl 5.10 has a powerful built in switch            •                             {
    statement called given, where the programming       •                                            case 'a':
    cases are called when:                              •
                                                                      cout << "Run program 1n";
                                                        •
                                                                      break;
                                                        •                                               case 'b':
                                                        •                                                           {
                                                        •
                                                                      cout << "Run program 2n";
                                                        •
                                                                      cout << "Please Waitn";
                                                        •
                                                                      break;
                                                        •                                                           }
                                                        •                                               default:
                                                        •
                                                                      cout << "Invalid choicen";
                                                        •
                                                                      break;
                                                        •                             }
                                                        •                             return 0;
                                                        •             }




                                        http://eglobiotraining.com/
Looping




http://eglobiotraining.com/
http://eglobiotraining.com/
• In programming, very often when you write code, you want
  the same block of code to run a number of times. You can use
  looping statements in your code to do this.
•
• In programming, the JavaScript programming have the
  following looping statements:
•
• While programming- loops through a block of code while a
  condition is true
• Do...while programming- loops through a block of code
  once, and then repeats the loop while a condition is true
• For programming- run statements a specified number of times


                        http://eglobiotraining.com/
•
                                                         •    #include <iostream>
•   while
•                                                        •    using namespace std;
•   In programming while statement will
    execute a block of code while a condition is         •    int main()
    true..                                               •    {
                                                         •      int x;

                                                         •      x = 0;
                                                         •      do {
                                                         •        // "Hello, world!" is printed at least one
                                                              time
                                                         •        // even though the condition is false
                                                         •        cout<<"Hello, world!n";
                                                         •      } while ( x != 0 );
                                                         •      cin.get();
                                                         •    }




                                         http://eglobiotraining.com/
• Do...while                                •
                                            •
                                            •
                                                  /**
                                                   ** This example contains a switch statement that performs
                                                   ** the same statement for more than one case label.
                                            •      **/

• In Programming the                        •     #include <stdio.h>

                                            •     int main(void)

  do...while statement                      •
                                            •
                                                  {
                                                    int month;



  will execute a block of                   •
                                            •
                                            •
                                                      /* Read in a month value */
                                                      printf("Enter month: ");
                                                      scanf("%d", &month);


  code once, and then it                    •
                                            •
                                            •
                                                      /* Tell what season it falls into */
                                                      switch (month)
                                                      {

  will repeat the loop                      •
                                            •
                                            •
                                                        case 12:
                                                        case 1:
                                                        case 2:
                                            •             printf("month %d is a winter monthn", month);
  while a condition is                      •

                                            •
                                                          break;

                                                          case 3:

  true                                      •
                                            •
                                            •
                                                          case 4:
                                                          case 5:
                                                            printf("month %d is a spring monthn", month);
                                            •               break;

• The Java programming                      •
                                            •
                                                          case 6:
                                                          case 7:
                                            •             case 8:
  language also provides                    •
                                            •
                                                            printf("month %d is a summer monthn", month);
                                                            break;


  a do-while                                •
                                            •
                                            •
                                                          case 9:
                                                          case 10:
                                                          case 11:
                                            •
  statement, which can
                                                            printf("month %d is a fall monthn", month);
                                            •               break;

                                            •             case 66:

  be expressed as                           •
                                            •
                                            •
                                                          case 99:
                                                          default:
                                                            printf("month %d is not a valid monthn", month);

  follows:                                  •

                                            •
                                                      }

                                                      return(0);
                                            •     }




                            http://eglobiotraining.com/
•    #include <iostream>

                                                       •    using namespace std; // So the program can
                                                            see cout and endl
•   for
•
                                                       •    int main()
•   In programming the “for” statement will
    execute a block of code a specified number         •    {
    of times                                           •      // The loop goes while x < 10, and x
                                                            increases by one every loop
                                                       •      for ( int x = 0; x < 10; x++ ) {
                                                       •        // Keep in mind that the loop condition
                                                            checks
                                                       •        // the conditional statement before it
                                                            loops again.
                                                       •        // consequently, when x equals 10 the
                                                            loop breaks.
                                                       •        // x is updated before the condition is
                                                            checked.
                                                       •        cout<< x <<endl;
                                                       •      }
                                                       •      cin.get();
                                                       •    }



                                       http://eglobiotraining.com/
• The Infinite Loop:        • #include <iostream>
                              using namespace std;
• A loop becomes infinite     int main () { for( ; ; ) {
  loop if a condition never   printf("This loop will
  becomes false. The for      run forever.n"); }
  loop is traditionally       return 0; }
  used for this purpose.
  Since none of the three
  expressions that form
  the for loop are
  required, you can make
  an endless loop by
  leaving the conditional
  expression empty.
                       http://eglobiotraining.com/
• While Loop
                                        •     #include <iostream.h>
                                        •
                                        •     int main(void) {

• In programming, For                   •                 int x = 0;
                                        •                 int y = 0;
                                        •                 bool validNumber = false;
  repeating C programming               •
                                        •                  while (validNumber == false) {
  statements whiles a                   •
                                              and 10: ";
                                                                             cout << "Please enter an integer between 1


  condition is true,the                 •
                                        •
                                              endl;
                                                                              cin >> x;
                                                                              cout << "You entered: " << x << endl <<

  while provides a the                  •
                                        •                                     if ((x < 1) || (x > 10)) {
  necessary mechanis
                   m.                   •
                                              x is not between 1 and 10!"
                                                                                                     cout << "Your value for

                                        •                                         << endl;
                                        •                                                          cout << "Please re-
                                              enter the number!" << endl << endl;
                                        •                                    }
                                        •                                    else
                                        •                                                          validNumber = true;

                                        •                  }
                                        •
                                        •                  cout << "Thank you for entering a valid number!" << endl;
                                        •
                                        •                  return 0;
                                        •     }




                        http://eglobiotraining.com/
• Input : In any programming language input
  means to feed some data into program.
  Programming can be given in the form of file or
  from command line. C programming language
  provides a set of built-in functions to read given
  input and feed it to the program as per
  requirement.
•
• Output : In any programming language output
  means to display some data on screen, printer or
  in any file. C programming language provides a
  set of built-in functions to output required data.

                    http://eglobiotraining.com/
http://eglobiotraining.com/
Professor: Erwin M. Globio

HTTP://EGLOBIOTRAINING.COM/

More Related Content

PPTX
Final Exam in FNDPRG
PPTX
Final exam
PPSX
VBScript in Software Testing
PPTX
Jangsehyun final requirement
PPTX
Final requirement (2)
PDF
PDF
Os Borger
PDF
A Post About Analyzing PHP
Final Exam in FNDPRG
Final exam
VBScript in Software Testing
Jangsehyun final requirement
Final requirement (2)
Os Borger
A Post About Analyzing PHP

What's hot (7)

PPTX
Castro
PDF
Javascript - Tutorial
PDF
OOPs difference faqs- 4
PDF
javascript teach
PPT
Vb script
PDF
PPT
Vb script
Castro
Javascript - Tutorial
OOPs difference faqs- 4
javascript teach
Vb script
Vb script
Ad

Similar to Final exam (20)

PPTX
Programming - Marla Fuentes
PPTX
PPTX
Final project powerpoint template (fndprg) (1)
PPTX
Computer programming
PDF
C++ Chapter II
PPSX
Ramirez slideshow
PDF
Cpp reference card
PPTX
Final requirement in programming
PPT
Fundamentals of programming angeli
PPTX
Lab # 3
PPTX
Final requirement in programming niperos
PDF
Fundamentals of programming)
PPTX
Final requirement
PPTX
Final requirement in programming vinson
PPT
Final requirement
PPTX
Final project powerpoint template (fndprg) (1)
PDF
C++ Course - Lesson 1
PPTX
Powerpoint presentation final requirement in fnd prg
PPTX
Project in programming
PPTX
Final requirement
Programming - Marla Fuentes
Final project powerpoint template (fndprg) (1)
Computer programming
C++ Chapter II
Ramirez slideshow
Cpp reference card
Final requirement in programming
Fundamentals of programming angeli
Lab # 3
Final requirement in programming niperos
Fundamentals of programming)
Final requirement
Final requirement in programming vinson
Final requirement
Final project powerpoint template (fndprg) (1)
C++ Course - Lesson 1
Powerpoint presentation final requirement in fnd prg
Project in programming
Final requirement
Ad

Final exam

  • 1. Switch Case http://eglobiotraining.com/
  • 3. • In programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as Pascal, Ada, C/C++, C# programming language , Java, and so on. It is also included in several other types of programming languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. http://eglobiotraining.com/
  • 4. char key; • C# • printf("Enter an arithmetic operatorn"); • • scanf("%c",&key); • In C# programming, every case block • switch (key) that contains any statements must • { • case '+': have a reachable end point, or • add(); triggers a compilation error.In • break; programming usually, this is a break • case '-': statement, but any jump statement • subtract(); can be used – such as return, goto or • break; throw – or the switch can simply end • case '*': with an infinite loop. Case fall- • multiply(); through is only permitted when there • break; are no statements between one case • case '/': statement and the next. If fall- • divide(); • break; through is otherwise desired, it must be made explicit with the goto case • default: construct. C# programming also • printf("invalid keyn"); • break; allows the use of non-integer case • } values, such as Strings. http://eglobiotraining.com/
  • 5. Select Case n • Case Is < -5 • MsgBox("n is less than -5") • Visual Basic .NET • Case -4 To -1 • • • MsgBox("n is between -4 and -1") Case 0 • In programming, Visual Basic • MsgBox("n is 0") • Case 2, 4, 6, 8 .NET, the switch statement is • MsgBox("n is even") called "Select Case • Case 1, 3, 5, 7, 9 programming", and fall-through • MsgBox("n is odd") • Case Else to later blocks is not supported. • MsgBox("only single-digit numbers are However, ranges and various allowed.", vbCritical) • End Select constructs from If statements are • both supported • Visual FoxPro • • Visual FoxPro: • • Do Case • Case field_1 = "X" • Replace field_b With 1 • Case field_1 = "Y" • Replace field_b With 2 • Case field_1 = "Z" • Replace field_b With 3 • Endcase http://eglobiotraining.com/
  • 6. #include <iostream> using namespace std; int main(void) • Haskell { char grade; cout << "Enter your grade: "; cin >> grade; switch (grade) • Haskell's case construct, unlike C-influenced { programming languages, has no fall-through case 'A': behaviour. It is a programming expression which cout << "Your average must be between 90 - 100" << endl; returns a value, and it can deconstruct values break; using pattern matching. case 'B': cout << "Your average must be between 80 - 89" << endl; break; case 'C': cout << "Your average must be between 70 - 79" << endl; break; case 'D': cout << "Your average must be between 60 - 69" << endl; break; default: cout << "Your average must be below 60" << endl; } return 0; } http://eglobiotraining.com/
  • 7. #include<iostream> • using namespace std; • Pascal • int main() • { • int a; • In Programming, Pascal does not allow • cin >> a; “fall through”, but has ranges and • if ( a <= 10 ) comma separated literal lists. • { • cout << "Below 10" << 'n'; • } • else • { • if ( a < 60 ) • { • cout << "Below 60" << 'n'; • } • } • return 0; • } http://eglobiotraining.com/
  • 8. #include<iostream> • using namespace std; • int main() • { • char myinput; • cin >> myinput; • Perl • • switch (myinput) • Perl 5.10 has a powerful built in switch • { statement called given, where the programming • case 'a': cases are called when: • cout << "Run program 1n"; • break; • case 'b': • { • cout << "Run program 2n"; • cout << "Please Waitn"; • break; • } • default: • cout << "Invalid choicen"; • break; • } • return 0; • } http://eglobiotraining.com/
  • 11. • In programming, very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to do this. • • In programming, the JavaScript programming have the following looping statements: • • While programming- loops through a block of code while a condition is true • Do...while programming- loops through a block of code once, and then repeats the loop while a condition is true • For programming- run statements a specified number of times http://eglobiotraining.com/
  • 12. • #include <iostream> • while • • using namespace std; • In programming while statement will execute a block of code while a condition is • int main() true.. • { • int x; • x = 0; • do { • // "Hello, world!" is printed at least one time • // even though the condition is false • cout<<"Hello, world!n"; • } while ( x != 0 ); • cin.get(); • } http://eglobiotraining.com/
  • 13. • Do...while • • • /** ** This example contains a switch statement that performs ** the same statement for more than one case label. • **/ • In Programming the • #include <stdio.h> • int main(void) do...while statement • • { int month; will execute a block of • • • /* Read in a month value */ printf("Enter month: "); scanf("%d", &month); code once, and then it • • • /* Tell what season it falls into */ switch (month) { will repeat the loop • • • case 12: case 1: case 2: • printf("month %d is a winter monthn", month); while a condition is • • break; case 3: true • • • case 4: case 5: printf("month %d is a spring monthn", month); • break; • The Java programming • • case 6: case 7: • case 8: language also provides • • printf("month %d is a summer monthn", month); break; a do-while • • • case 9: case 10: case 11: • statement, which can printf("month %d is a fall monthn", month); • break; • case 66: be expressed as • • • case 99: default: printf("month %d is not a valid monthn", month); follows: • • } return(0); • } http://eglobiotraining.com/
  • 14. #include <iostream> • using namespace std; // So the program can see cout and endl • for • • int main() • In programming the “for” statement will execute a block of code a specified number • { of times • // The loop goes while x < 10, and x increases by one every loop • for ( int x = 0; x < 10; x++ ) { • // Keep in mind that the loop condition checks • // the conditional statement before it loops again. • // consequently, when x equals 10 the loop breaks. • // x is updated before the condition is checked. • cout<< x <<endl; • } • cin.get(); • } http://eglobiotraining.com/
  • 15. • The Infinite Loop: • #include <iostream> using namespace std; • A loop becomes infinite int main () { for( ; ; ) { loop if a condition never printf("This loop will becomes false. The for run forever.n"); } loop is traditionally return 0; } used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty. http://eglobiotraining.com/
  • 16. • While Loop • #include <iostream.h> • • int main(void) { • In programming, For • int x = 0; • int y = 0; • bool validNumber = false; repeating C programming • • while (validNumber == false) { statements whiles a • and 10: "; cout << "Please enter an integer between 1 condition is true,the • • endl; cin >> x; cout << "You entered: " << x << endl << while provides a the • • if ((x < 1) || (x > 10)) { necessary mechanis m. • x is not between 1 and 10!" cout << "Your value for • << endl; • cout << "Please re- enter the number!" << endl << endl; • } • else • validNumber = true; • } • • cout << "Thank you for entering a valid number!" << endl; • • return 0; • } http://eglobiotraining.com/
  • 17. • Input : In any programming language input means to feed some data into program. Programming can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement. • • Output : In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data. http://eglobiotraining.com/
  • 19. Professor: Erwin M. Globio HTTP://EGLOBIOTRAINING.COM/