SlideShare a Scribd company logo
1 of 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/
•     switch (n)
                                                  •     {
•   C#                                            •       case 0:
•                                                 •        Console.WriteLine("You typed zero.");
                                                  •        break;
•   In C# programming, every case block           •       case 1:
    that contains any statements must             •       case 4:
                                                  •       case 9:
    have a reachable end point, or                •        Console.WriteLine("n is a perfect square.");
    triggers a compilation error.In               •        break;
                                                  •       case 2:
    programming usually, this is a break          •        Console.WriteLine("n is an even number.");
    statement, but any jump statement             •        goto case 3;
    can be used – such as return, goto or         •       case 3:
                                                  •       case 5:
    throw – or the switch can simply end          •       case 7:
    with an infinite loop. Case fall-             •        Console.WriteLine("n is a prime number.");
    through is only permitted when there          •        break;
                                                  •       case 6:
    are no statements between one case            •       case 8:
    statement and the next. If fall-              •        Console.WriteLine("n is an even number.");
                                                  •        break;
    through is otherwise desired, it must         •       default:
    be made explicit with the goto case           •        Console.WriteLine("Only single-digit numbers are
                                                        allowed.");
    construct. C# programming also                •        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/
• Haskell
                                              •     (f:r) -> "Not empty, first item is " ++ show f
                                              •      [] -> "List is empty!"
• Haskell's case construct, unlike C-         •
                                              •    OCaml, F#
  influenced programming                      •
  languages, has no fall-through              •    OCaml and F#'s match construct is like
  behaviour. It is a programming                   Haskell's case above.
  expression which returns a value,           •
  and it can deconstruct values               •    (* OCaml *)
  using pattern matching.                     •     match list with
                                              •      f::r -> "Not empty, first item is " ^
                                                   string_of_int f
                                              •     | [] -> "List is empty!"
                                              •
                                              •    // F#
                                              •     match list with
                                              •     | f::r -> "Not empty, first item is " + string f
                                              •     | [] -> "List is empty!"


                               http://eglobiotraining.com/
• Pascal                                •     case age of

                                        •      0,1: writeln('baby');
• In Programming, Pascal                •      2,3,4: writeln('toddler');
  does not allow “fall                  •      5..12: writeln('kid');
  through”, but has ranges              •      13..19: writeln('teenager');
  and comma separated                   •      20..25: writeln('young');
  literal lists.                        •      else writeln('old');
                                        •     end;




                        http://eglobiotraining.com/
• Perl                                                 •    use feature 'switch';
•                                                      •    given ($foo) {
                                                       •      when (undef) {
•   Perl 5.10 has a powerful built in switch
                                                       •         say '$foo is undefined';
    statement called given, where the
                                                       •      }
    programming cases are called when:
                                                       •      when ("foo") {
                                                       •         say '$foo is the string "foo"';
                                                       •      }
                                                       •      when ([1,3,5,7,9]) {
                                                       •         say '$foo is an odd digit';
                                                       •         continue; # Fall through
                                                       •      }
                                                       •      when ($_ < 100) {
                                                       •         say '$foo is numerically less than 100';
                                                       •      }
                                                       •      when (&complicated_check) {
                                                       •         say 'a complicated check for $foo is true';
                                                       •      }
                                                       •      default {
                                                       •         die "I don't know what to do with $foo";
                                                       •      }
                                                       •    }

                                       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/
• while                              •    while (condition)
•                                    •    {
• In programming while               •    code to be executed
  statement will execute a           •    }
  block of code while a              •
  condition is true..
                                     •



                     http://eglobiotraining.com/
• Do...while                              •    do
•                                         •    {
• In Programming the do...while           •    code to be executed
  statement will execute a block          •    }
  of code once, and then it will          •    while (condition)
  repeat the loop while a
  condition is true
•
• The Java programming
  language also provides a do-
  while statement, which can be
  expressed as follows:
•


                          http://eglobiotraining.com/
• for                                • for (initialization;
•                                      condition; increment)
• In programming the                 • {
  “for” statement will               • code to be executed
  execute a block of code            • }
  a specified number of
  times



                     http://eglobiotraining.com/
•    if (expression)
                                          •    statement;
                                          •
                                          •    or
                                          •
                                          •    if (expression)
                                          •      {
                                          •        Block of statements;
                                          •      }
• If statement                            •
                                          •    or
                                          •
                                          •    if (expression)
                                          •      {
                                          •        Block of statements;
                                          •      }

• In Programming, It takes an
                                          •    else
                                          •      {
                                          •        Block of statements;
  expression in parenthesis               •
                                          •
                                                 }

  and an statement or block of            •
                                          •
                                               or

  statements. if the expression           •
                                          •
                                               if (expression)
                                                 {
  is true then the                        •
                                          •      }
                                                   Block of statements;

  programming statement or                •
                                          •
                                               else if(expression)
                                                 {
  block of statements gets                •
                                          •      }
                                                   Block of statements;

  executed otherwise these                •
                                          •
                                               else
                                                 {
  statements are skipped.                 •
                                          •      }
                                                   Block of statements;

                                          •
                                          •    ? : Operator




                          http://eglobiotraining.com/
•   While Loop                              •     #include
                                            •
• In programming, For repeating C           •     int main(void)
  programming statements whiles             •     {
  a condition is true,the while             •
  provides a the necessary                  •     int loop = 0;
  mechanism.                                •
                                            •     while (loop <=10)
                                            •     {
                                            •     printf(“%d”, loop);
                                            •     ++loop
                                            •     }
                                            •
                                            •     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

Similar to Final exam

Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingShaun Griffith
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLou Loizides
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming IntroLou Loizides
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learningASIT Education
 
javascript teach
javascript teachjavascript teach
javascript teachguest3732fa
 
JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_Whiteguest3732fa
 
Slot Composition
Slot CompositionSlot Composition
Slot CompositionESUG
 
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...DevClub_lv
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCDrsebbe
 
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of Cjeremyrand
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersDiego Freniche Brito
 

Similar to Final exam (20)

Final Exam in FNDPRG
Final Exam in FNDPRGFinal Exam in FNDPRG
Final Exam in FNDPRG
 
Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog Parsing
 
Intro to Perl
Intro to PerlIntro to Perl
Intro to Perl
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming Intro
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learning
 
tick cross game
tick cross gametick cross game
tick cross game
 
C basics
C basicsC basics
C basics
 
javascript teach
javascript teachjavascript teach
javascript teach
 
JSBootcamp_White
JSBootcamp_WhiteJSBootcamp_White
JSBootcamp_White
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 
Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
 
Javascript
JavascriptJavascript
Javascript
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of C
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
14-types.ppt
14-types.ppt14-types.ppt
14-types.ppt
 

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. switch (n) • { • C# • case 0: • • Console.WriteLine("You typed zero."); • break; • In C# programming, every case block • case 1: that contains any statements must • case 4: • case 9: have a reachable end point, or • Console.WriteLine("n is a perfect square."); triggers a compilation error.In • break; • case 2: programming usually, this is a break • Console.WriteLine("n is an even number."); statement, but any jump statement • goto case 3; can be used – such as return, goto or • case 3: • case 5: throw – or the switch can simply end • case 7: with an infinite loop. Case fall- • Console.WriteLine("n is a prime number."); through is only permitted when there • break; • case 6: are no statements between one case • case 8: statement and the next. If fall- • Console.WriteLine("n is an even number."); • break; through is otherwise desired, it must • default: be made explicit with the goto case • Console.WriteLine("Only single-digit numbers are allowed."); construct. C# programming also • 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. • Haskell • (f:r) -> "Not empty, first item is " ++ show f • [] -> "List is empty!" • Haskell's case construct, unlike C- • • OCaml, F# influenced programming • languages, has no fall-through • OCaml and F#'s match construct is like behaviour. It is a programming Haskell's case above. expression which returns a value, • and it can deconstruct values • (* OCaml *) using pattern matching. • match list with • f::r -> "Not empty, first item is " ^ string_of_int f • | [] -> "List is empty!" • • // F# • match list with • | f::r -> "Not empty, first item is " + string f • | [] -> "List is empty!" http://eglobiotraining.com/
  • 7. • Pascal • case age of • 0,1: writeln('baby'); • In Programming, Pascal • 2,3,4: writeln('toddler'); does not allow “fall • 5..12: writeln('kid'); through”, but has ranges • 13..19: writeln('teenager'); and comma separated • 20..25: writeln('young'); literal lists. • else writeln('old'); • end; http://eglobiotraining.com/
  • 8. • Perl • use feature 'switch'; • • given ($foo) { • when (undef) { • Perl 5.10 has a powerful built in switch • say '$foo is undefined'; statement called given, where the • } programming cases are called when: • when ("foo") { • say '$foo is the string "foo"'; • } • when ([1,3,5,7,9]) { • say '$foo is an odd digit'; • continue; # Fall through • } • when ($_ < 100) { • say '$foo is numerically less than 100'; • } • when (&complicated_check) { • say 'a complicated check for $foo is true'; • } • default { • die "I don't know what to do with $foo"; • } • } 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. • while • while (condition) • • { • In programming while • code to be executed statement will execute a • } block of code while a • condition is true.. • http://eglobiotraining.com/
  • 13. • Do...while • do • • { • In Programming the do...while • code to be executed statement will execute a block • } of code once, and then it will • while (condition) repeat the loop while a condition is true • • The Java programming language also provides a do- while statement, which can be expressed as follows: • http://eglobiotraining.com/
  • 14. • for • for (initialization; • condition; increment) • In programming the • { “for” statement will • code to be executed execute a block of code • } a specified number of times http://eglobiotraining.com/
  • 15. if (expression) • statement; • • or • • if (expression) • { • Block of statements; • } • If statement • • or • • if (expression) • { • Block of statements; • } • In Programming, It takes an • else • { • Block of statements; expression in parenthesis • • } and an statement or block of • • or statements. if the expression • • if (expression) { is true then the • • } Block of statements; programming statement or • • else if(expression) { block of statements gets • • } Block of statements; executed otherwise these • • else { statements are skipped. • • } Block of statements; • • ? : Operator http://eglobiotraining.com/
  • 16. While Loop • #include • • In programming, For repeating C • int main(void) programming statements whiles • { a condition is true,the while • provides a the necessary • int loop = 0; mechanism. • • while (loop <=10) • { • printf(“%d”, loop); • ++loop • } • • 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/