Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of Switch Statements [closed]

If the total of this textbox is: PostDiscountTextBox.Text = $500.00, how do we make Switch statements stating that if the Post-discount Cost amount is between 0.00 and 999.99, display a message box with the message of "This amount qualifies for 'A-100' frequent flier miles" and an "OK" button?

Will someone provide an example of a switch statement?

I only have this so far, and I don't think it follows anything at all. Will someone guide me through this? Thank you.

        switch (PostDiscountCostTextBox.Text)
        {
            case (0.00 < && PostDiscountCostTextBox.Text <= 999.00)

Thank everyone who helped, but I am trying to figure out how to use a switch statement that evaluates Post-discount Cost based on a range of numeric values (nothing about an if statement). Yes, many cases will be put, and these will be my first two cases. Will someone be kind enough to provide me an example so that I could fill the rest of my cases in? I have lots. Thank you.

If the Post-discount Cost amount is between 0.00 and 999.99, display a message box with the message of "This amount qualifies for 'A-100' frequent flier miles." and an "OK" button. No title bar text or icon should be used.

If the Post-discount Cost amount is between 1,000.00 and 1,499.99, display a message box with the message of "This amount qualifies for 'B-500' frequent flier miles." and an "OK" button. No title bar text or icon should be used.

like image 202
user2085275 Avatar asked Feb 23 '13 22:02

user2085275


People also ask

What is an example of switch statement?

So, printf(“2+3 makes 5”) is executed and then followed by break; which brings the control out of the switch statement. Other examples for valid switch expressions: switch(2+3), switch(9*16%2), switch(a), switch(a-b) etc.

How do you close a switch statement?

You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.

How does a switch statement work?

The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.


2 Answers

That kind of switch usage is not allowed in C#.

Here is an example of proper switch usage

switch(n)       
{         
   case 1:   
      cost += 25;
      break;                  
   case 2:            
      cost += 25;
      break;           
   case 3:            
      cost += 50;
      break;         
   default:            
      Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");            
      break;      
 }

Your example should be transform into if-elseif-else statement:

if(first_case_predicate)
{

}
else if(another_predicate)
{

}
else
{
    // like 'default' of switch
}
like image 181
MarcinJuraszek Avatar answered Oct 19 '22 06:10

MarcinJuraszek


You cannot check for ranges in switch, you should use a chain of else if. See Is using decimal ranges in a switch impossible in C#?

like image 3
wRAR Avatar answered Oct 19 '22 07:10

wRAR