Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-11-2008
Member
 
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.");
    }   
}
Reply With Quote
  #2  
Old 10-11-2008
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "SWITCH....CASE statement in C#"
Thread Thread Starter Forum Replies Last Post
How to use switch statement in java Raju Chacha Software Development 2 19-01-2012 06:40 PM
Need Codings for PHP Switch Statement? NGV BalaKrishna Software Development 5 27-01-2010 10:31 PM
JavaScript Switch Statement Chrisch Software Development 3 03-11-2009 03:33 PM
How to use SQL Case Statement Syntax Suzane Software Development 3 30-07-2009 02:47 PM
Switch Statement in PHP Projectkmo Software Development 2 31-03-2009 12:31 PM


All times are GMT +5.5. The time now is 01:24 PM.