SlideShare une entreprise Scribd logo
1  sur  19
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 allowed.",
  However, ranges and various                    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 statement,                     •
                                            •
                                            •
                                                          case 9:
                                                          case 10:
                                                          case 11:
                                            •
  which can be
                                                            printf("month %d is a fall monthn", month);
                                            •               break;

                                            •             case 66:

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

                                            •         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/

Contenu connexe

Tendances

Javascript - Tutorial
Javascript - TutorialJavascript - Tutorial
Javascript - Tutorialadelaticleanu
 
javascript teach
javascript teachjavascript teach
javascript teachguest3732fa
 
OOPs difference faqs- 4
OOPs difference faqs- 4OOPs difference faqs- 4
OOPs difference faqs- 4Umar Ali
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Heiko Behrens
 

Tendances (8)

Castro
CastroCastro
Castro
 
Javascript - Tutorial
Javascript - TutorialJavascript - Tutorial
Javascript - Tutorial
 
javascript teach
javascript teachjavascript teach
javascript teach
 
OOPs difference faqs- 4
OOPs difference faqs- 4OOPs difference faqs- 4
OOPs difference faqs- 4
 
Vb script
Vb scriptVb script
Vb script
 
Vb script
Vb scriptVb script
Vb script
 
Java8
Java8Java8
Java8
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
 

Similaire à Final exam

JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_Whiteguest3732fa
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)jewelyngrace
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learningASIT Education
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)heoff
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOMSukrit Gupta
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_eShikshak
 
Javascript Basics by Bonny
Javascript Basics by BonnyJavascript Basics by Bonny
Javascript Basics by BonnyBonny Chacko
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programmingtrish_maxine
 
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of Cjeremyrand
 
WT Unit-3 PPT.pptx
WT Unit-3 PPT.pptxWT Unit-3 PPT.pptx
WT Unit-3 PPT.pptxTusharTikia
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C LanguageMohamed Elsayed
 
Smashing The Stack
Smashing The StackSmashing The Stack
Smashing The StackAbhishek BV
 

Similaire à Final exam (20)

JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_White
 
tick cross game
tick cross gametick cross game
tick cross game
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learning
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Java Script basics and DOM
Java Script basics and DOMJava Script basics and DOM
Java Script basics and DOM
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_
 
Variables
VariablesVariables
Variables
 
Short Introduction To "perl -d"
Short Introduction To "perl -d"Short Introduction To "perl -d"
Short Introduction To "perl -d"
 
Java script
Java scriptJava script
Java script
 
Javascript Basics by Bonny
Javascript Basics by BonnyJavascript Basics by Bonny
Javascript Basics by Bonny
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
 
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of C
 
WT Unit-3 PPT.pptx
WT Unit-3 PPT.pptxWT Unit-3 PPT.pptx
WT Unit-3 PPT.pptx
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 
C basics
C basicsC basics
C basics
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
 
Smashing The Stack
Smashing The StackSmashing The Stack
Smashing The Stack
 

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 allowed.", However, ranges and various 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 statement, • • • case 9: case 10: case 11: • which can be printf("month %d is a fall monthn", month); • break; • case 66: expressed as follows: • • • case 99: default: printf("month %d is not a valid monthn", month); • } • 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/