SlideShare una empresa de Scribd logo
1 de 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 have a                              {
                                                                   case '+':
     reachable end point, or triggers a                               add();
   compilation error.In programming                                   break;
usually, this is a break statement, but any
                                                                    case '-':
  jump statement can be used – such as                              subtract();
return, goto or throw – or the switch can                             break;
  simply end with an infinite loop. Case
                                                                    case '*':
fall-through is only permitted when there
                                                                    multiply();
    are no statements between one case                                break;
statement and the next. If fall-through is
    otherwise desired, it must be made                              case '/':
                                                                     divide();
explicit with the goto case construct. C#                             break;
  programming also allows the use of
 non-integer case values, such as Strings.                          default:
                                                             printf("invalid keyn");
                                                                      break;
                                                                       }

                          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
                                                         MsgBox("n is 0")
                                                          Case 2, 4, 6, 8

             In                                        MsgBox("n is even")
                                                         Case 1, 3, 5, 7, 9

 programming, Visual                                    MsgBox("n is odd")
                                                            Case Else

Basic .NET, the switch              MsgBox("only single-digit numbers are allowed.", vbCritical)
                                                           End Select

    statement is called                                  Visual FoxPro

        "Select Case                                     Visual FoxPro:

  programming", and                                         Do Case

   fall-through to later                               Case field_1 = "X"
                                                      Replace field_b With 1

blocks is not supported.                               Case field_1 = "Y"
                                                      Replace field_b With 2

  However, ranges and                                  Case field_1 = "Z"
                                                      Replace field_b With 3

various constructs from                                     Endcase



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



                   HTTP://EGLOBIOTRAINING.COM/
#include<iostream>
                                                        using namespace std;

                                                             int main()
                                                                  {
           Pascal                                                        int a;
                                                                       cin >> a;
                                                                    if ( a <= 10 )
                                                                           {
In Programming, Pascal does                          10" << 'n';
                                                                           cout << "Below

not allow “fall through”, but                                              }
                                                                          else
   has ranges and comma                                                    {
    separated literal lists.                                                  if ( a < 60 )
                                                                                     {

                                                    cout << "Below 60" << 'n';
                                                                              }
                                                                        }
                                                                    return 0;
                                                                 }


                    HTTP://EGLOBIOTRAINING.COM/
#include<iostream>
                                                       using namespace std;


                                                            int main()
                                                                {
                                                                       char myinput;
                                                                    cin >> myinput;


                                                                    switch (myinput)



         Perl
                                                                            {
                                                                                     case 'a':

                                                   cout << "Run program 1n";

                                                              break;
                                                                                     case 'b':


    Perl 5.10 has a                                cout << "Run program 2n";
                                                                                                 {




powerful built in switch                            cout << "Please Waitn";



   statement called
                                                              break;
                                                                                                 }
                                                                                     default:

   given, where the                                cout << "Invalid choicen";



programming cases are                                         break;
                                                                            }



     called when:
                                                                         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>

                                                       using namespace std;
               while
                                                             int main()
                                                                   {
In programming while statement                                   int x;
will execute a block of code while a
                                                               x = 0;
         condition is true..
                                                                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>



do...while statement will                                                int main(void)

                                                                                {


execute a block of code                                                     int month;




  once, and then it will
                                                                   /* Read in a month value */

                                                                     printf("Enter month: ");

                                                                      scanf("%d", &month);



 repeat the loop while a                                        /* Tell what season it falls into */

                                                                          switch (month)


    condition is true                                                            {

                                                                              case 12:

                                                                               case 1:



The Java programming                                                           case 2:

                                                           printf("month %d is a winter monthn", month);

                                                                                 break;


language also provides a                                                       case 3:



        do-while
                                                                               case 4:

                                                                               case 5:

                                                           printf("month %d is a spring monthn", month);



statement, which can be                                                          break;



                                                                               case 6:


  expressed as follows:                                                        case 7:

                                                                               case 8:

                                                          printf("month %d is a summer monthn", month);

                                                                                 break;



                                                                               case 9:

                                                                              case 10:

                                                                              case 11:

                                                            printf("month %d is a fall monthn", month);

                                                                                 break;



                      HTTP://EGLOBIOTRAINING.COM/                             case 66:

                                                                              case 99:

                                                                               default:
#include <iostream>

                                        using namespace std; // So the program can see
                                                        cout and endl

                                                             int main()
               for                                                {
                                        // The loop goes while x < 10, and x increases by
                                                          one every loop
   In programming the “for”                       for ( int x = 0; x < 10; x++ ) {
statement will execute a block of        // Keep in mind that the loop condition checks
                                           // the conditional statement before it loops
code a specified number of times                               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:
  A loop becomes infinite loop if a
 condition never becomes false. The             #include <iostream> using
 for loop is traditionally used for this     namespace std; int main () { for( ; ; )
   purpose. Since none of the three             { printf("This loop will run
  expressions that form the for loop             forever.n"); } return 0; }
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) {
                                                                       int x = 0;

In programming, For                                                    int y = 0;
                                                              bool validNumber = false;

      repeating C                                           while (validNumber == false) {

    programming                                                 cout << "Please enter an integer between 1
                                                            and 10: ";


  statements whiles a
                                                                                    cin >> x;
                                                                 cout << "You entered: " << x << endl <<
                                                               endl;


 condition is true,the                                                      if ((x < 1) || (x > 10)) {


 while provides a the                               x is not between 1 and 10!"
                                                                                       cout << "Your value for

                                                                                      << endl;

 necessary mechanis  m.                           the number!" << endl << endl;
                                                                                       cout << "Please re-enter

                                                                                        }
                                                                                       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://www.slideshare.net/florimaycasakit2012/final-exam-
14680755




                 HTTP://EGLOBIOTRAINING.COM/
Professor: Erwin M. Globio




HTTP://EGLOBIOTRAINING.COM/

Más contenido relacionado

La actualidad más candente

Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
Core Lee
 
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
 
Chapter 2 basic element of programming
Chapter 2 basic element of programming Chapter 2 basic element of programming
Chapter 2 basic element of programming
Zul Aiman
 

La actualidad más candente (10)

Textmate shortcuts
Textmate shortcutsTextmate shortcuts
Textmate shortcuts
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new features
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Deep C
Deep CDeep C
Deep C
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Chapter 2 basic element of programming
Chapter 2 basic element of programming Chapter 2 basic element of programming
Chapter 2 basic element of programming
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Phpquick
PhpquickPhpquick
Phpquick
 
Tesseract. Recognizing Errors in Recognition Software
Tesseract. Recognizing Errors in Recognition SoftwareTesseract. Recognizing Errors in Recognition Software
Tesseract. Recognizing Errors in Recognition Software
 

Destacado (8)

Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Final requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary ClemenceFinal requirement for programming-Bonifacio, Mary Clemence
Final requirement for programming-Bonifacio, Mary Clemence
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
Herramientas para programar en C
Herramientas para programar en CHerramientas para programar en C
Herramientas para programar en C
 
Elementos basicos c
Elementos basicos cElementos basicos c
Elementos basicos c
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c language
 
Selection statements
Selection statementsSelection statements
Selection statements
 

Similar a Final Exam in FNDPRG

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
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
trish_maxine
 
Final requirement in programming niperos
Final requirement in programming   niperosFinal requirement in programming   niperos
Final requirement in programming niperos
markings17
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentes
mfuentessss
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
Ganesh Samarthyam
 
Perl training-in-navi mumbai
Perl training-in-navi mumbaiPerl training-in-navi mumbai
Perl training-in-navi mumbai
vibrantuser
 
javascript teach
javascript teachjavascript teach
javascript teach
guest3732fa
 
JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_White
guest3732fa
 

Similar a Final Exam in FNDPRG (20)

Final exam
Final examFinal exam
Final exam
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Final requirement in programming
Final requirement in programmingFinal requirement in programming
Final requirement in programming
 
Castro
CastroCastro
Castro
 
Final requirement in programming niperos
Final requirement in programming   niperosFinal requirement in programming   niperos
Final requirement in programming niperos
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)
 
Final requirement in programming vinson
Final requirement in programming  vinsonFinal requirement in programming  vinson
Final requirement in programming vinson
 
Lab # 3
Lab # 3Lab # 3
Lab # 3
 
22 Jop Oct 08
22 Jop Oct 0822 Jop Oct 08
22 Jop Oct 08
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_
 
9-java language basics part3
9-java language basics part39-java language basics part3
9-java language basics part3
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentes
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
 
Perl training-in-navi mumbai
Perl training-in-navi mumbaiPerl training-in-navi mumbai
Perl training-in-navi mumbai
 
javascript teach
javascript teachjavascript teach
javascript teach
 
JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_White
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Final requirement
Final requirementFinal requirement
Final requirement
 

Final Exam in FNDPRG

  • 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 have a { case '+': reachable end point, or triggers a add(); compilation error.In programming break; usually, this is a break statement, but any case '-': jump statement can be used – such as subtract(); return, goto or throw – or the switch can break; simply end with an infinite loop. Case case '*': fall-through is only permitted when there multiply(); are no statements between one case break; statement and the next. If fall-through is otherwise desired, it must be made case '/': divide(); explicit with the goto case construct. C# break; programming also allows the use of non-integer case values, such as Strings. default: printf("invalid keyn"); break; } 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 MsgBox("n is 0") Case 2, 4, 6, 8 In MsgBox("n is even") Case 1, 3, 5, 7, 9 programming, Visual MsgBox("n is odd") Case Else Basic .NET, the switch MsgBox("only single-digit numbers are allowed.", vbCritical) End Select statement is called Visual FoxPro "Select Case Visual FoxPro: programming", and Do Case fall-through to later Case field_1 = "X" Replace field_b With 1 blocks is not supported. Case field_1 = "Y" Replace field_b With 2 However, ranges and Case field_1 = "Z" Replace field_b With 3 various constructs from Endcase If statements are both supported HTTP://EGLOBIOTRAINING.COM/
  • 6. Haskell #include <iostream> using namespace std; int main(void) { char grade; cout << "Enter your grade: "; cin >> grade; switch (grade) Haskell's case { case 'A': construct, unlike C- cout << "Your average must be between 90 - 100" << endl; influenced programming break; case 'B': languages, has no fall- cout << "Your average must be between 80 - 89" << endl; break; through behaviour. It is a case 'C': cout << "Your average must be between 70 - 79" programming expression << endl; break; which returns a value, and case 'D': cout << "Your average must be between 60 - 69" it can deconstruct values << endl; break; using pattern matching. default: cout << "Your average must be below 60" << endl; } return 0; } HTTP://EGLOBIOTRAINING.COM/
  • 7. #include<iostream> using namespace std; int main() { Pascal int a; cin >> a; if ( a <= 10 ) { In Programming, Pascal does 10" << 'n'; cout << "Below not allow “fall through”, but } else has ranges and comma { separated literal lists. if ( a < 60 ) { cout << "Below 60" << 'n'; } } return 0; } HTTP://EGLOBIOTRAINING.COM/
  • 8. #include<iostream> using namespace std; int main() { char myinput; cin >> myinput; switch (myinput) Perl { case 'a': cout << "Run program 1n"; break; case 'b': Perl 5.10 has a cout << "Run program 2n"; { powerful built in switch cout << "Please Waitn"; statement called break; } default: given, where the cout << "Invalid choicen"; programming cases are break; } called when: 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> using namespace std; while int main() { In programming while statement int x; will execute a block of code while a x = 0; condition is true.. 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> do...while statement will int main(void) { execute a block of code int month; once, and then it will /* Read in a month value */ printf("Enter month: "); scanf("%d", &month); repeat the loop while a /* Tell what season it falls into */ switch (month) condition is true { case 12: case 1: The Java programming case 2: printf("month %d is a winter monthn", month); break; language also provides a case 3: do-while case 4: case 5: printf("month %d is a spring monthn", month); statement, which can be break; case 6: expressed as follows: case 7: case 8: printf("month %d is a summer monthn", month); break; case 9: case 10: case 11: printf("month %d is a fall monthn", month); break; HTTP://EGLOBIOTRAINING.COM/ case 66: case 99: default:
  • 14. #include <iostream> using namespace std; // So the program can see cout and endl int main() for { // The loop goes while x < 10, and x increases by one every loop In programming the “for” for ( int x = 0; x < 10; x++ ) { statement will execute a block of // Keep in mind that the loop condition checks // the conditional statement before it loops code a specified number of times 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: A loop becomes infinite loop if a condition never becomes false. The #include <iostream> using for loop is traditionally used for this namespace std; int main () { for( ; ; ) purpose. Since none of the three { printf("This loop will run expressions that form the for loop forever.n"); } return 0; } 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) { int x = 0; In programming, For int y = 0; bool validNumber = false; repeating C while (validNumber == false) { programming cout << "Please enter an integer between 1 and 10: "; statements whiles a cin >> x; cout << "You entered: " << x << endl << endl; condition is true,the if ((x < 1) || (x > 10)) { while provides a the x is not between 1 and 10!" cout << "Your value for << endl; necessary mechanis m. the number!" << endl << endl; cout << "Please re-enter } 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/