Results 1 to 2 of 2

Thread: SWITCH....CASE statement in C#

  1. #1
    Join Date
    Jun 2008
    Posts
    107

    SWITCH....CASE statement in C#

    The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body.

    This kind of statements are used to pass the control of the process to a specified execution statements whose constant value matches the condition expression, i.e., the Control is transferred to the case statement whose constant-expression matches expression. The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the jump-statement transfers control out of the case body.

    switch (expression)
    {
    case constant-expression:
    statement
    jump-statement
    [default:
    statement
    jump-statement]
    }

    Code:
    using System;
    class SwitchTest
    {
        public static void Main()    
        {
        Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
        Console.Write("Please enter your selection: ");
        string s = Console.ReadLine();
        int n = int.Parse(s);
        int cost = 0;
        switch(n)
        {
        case 1:
            cost += 25;
            break;
        case 2:
            cost += 25;
            goto case 1;
        case 3:
            cost += 50;
            goto case 1;
        default:    
            Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
            break;
           }
        if (cost != 0)   
            Console.WriteLine("Please insert {0} cents.", cost);
            Console.WriteLine("Thank you for your business.");
        }   
    }

  2. #2
    Join Date
    May 2008
    Posts
    10

    Re: SWITCH....CASE statement in C#

    C# has a switch statement just like the one found in C/C++ and Java. But the C# switch has an important difference: it does not allow fall-through between cases.

Similar Threads

  1. How to use switch statement in java
    By Raju Chacha in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 06:40 PM
  2. Need Codings for PHP Switch Statement?
    By NGV BalaKrishna in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 10:31 PM
  3. JavaScript Switch Statement
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 03:33 PM
  4. How to use SQL Case Statement Syntax
    By Suzane in forum Software Development
    Replies: 3
    Last Post: 30-07-2009, 02:47 PM
  5. Switch Statement in PHP
    By Projectkmo in forum Software Development
    Replies: 2
    Last Post: 31-03-2009, 12:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,371,299.62233 seconds with 17 queries