how can i use the switch statement to find a value in a range?
example like,

#include <stdio.h>
void main()
{
	int monthly_sales;
	double monthly_income;

	printf("Enter your monthly sales amount > ");
	scanf("%d", &monthly_sales);

	switch(monthly_sales)
	{
	case 50000: // i want make this in a range, greater than or equal.
		{
			monthly_income = 375 + ( monthly_sales * 16 / 100);
			printf("\nMonthly Income = $ %.2f\n\n", monthly_income);
			break;
		}
	}
}

so how do with it ?
by the way, i would like to ask about how to write the pseudocode in switch statement ?
thanks.

Recommended Answers

All 20 Replies

>how can i use the switch statement to find a value in a range?
Short answer: you don't.
Long answer: You need a case for each value in the range, which obviously doesn't scale well at all.

Use an if..else chain if you want ranges.

>how can i use the switch statement to find a value in a range?
Short answer: you don't.
Long answer: You need a case for each value in the range, which obviously doesn't scale well at all.

Use an if..else chain if you want ranges.

yeah.. i know about that.
by the way, i also had tried assign the monthly_sales into a variable, eg: a = 50000
in the case 50000 i change to case 'a' and also the printf there.
but there is cannot work, i can't get the display message. :(

yeah.. i know about that.
by the way, i also had tried assign the monthly_sales into a variable, eg: a = 50000
in the case 50000 i change to case 'a' and also the printf there.
but there is cannot work, i can't get the display message. :(

The variable to evaluate in a switch must be an integer and you're using a double.

The variable to evaluate in a switch must be an integer and you're using a double.

i also had tried in integer type.
that is can't work too.
it doesn't show me the printf message.

Use an if() with else if() series of statements.

When Narue speaks, wise coder's listen. Get those ears cleaned out, dude. ;)

Use an if() with else if() series of statements.

When Narue speaks, wise coder's listen. Get those ears cleaned out, dude. ;)

do you mean the whole programming change it into if-else serious statement ?

Use an if() with else if() series of statements.

When Narue speaks, wise coder's listen. Get those ears cleaned out, dude. ;)

Your acolyte veneration is understandable.

i also had tried in integer type.
that is can't work too.
it doesn't show me the printf message.

Are you expecting us to guess what's going on? Accompany the relevant source code of what you have tried if you want a solid answer.

>yeah.. i know about that.
Clearly not, or you wouldn't be having problems. You have a case for 50000. That case will only be executed if you type 50000, not 49999, not 50001 or any other value. You said you wanted a range, and the switch statement is not good for ranges. Remove it and replace it with an if..else chain:

if (monthly_sales < 50000)
    printf("Less than $50,000\n");
else if (monthly_sales > 50000)
    printf("Greater than $50,000\n");
else
    printf("Exactly $50,000\n");

It's just that simple.

Really, I want you keep that switch statement - keep working with it. Then you'll wind up pulling your hair out in frustration, and I'll look a lot better than you, in our forum yearbook pictures, because you'll be nearly bald. ;) ;)

Your acolyte veneration is understandable.


Are you expecting us to guess what's going on? Accompany the relevant source code of what you have tried if you want a solid answer.

this is what i had tried assign the value of monthly sales into a variable, after that only switch statement.

#include <stdio.h>
void main()
{
	int monthly_sales, a;
	double monthly_income;

	printf("Enter your monthly sales amount > ");
	scanf("%d", &monthly_sales);

	if(monthly_sales >= 50000)
		a = monthly_sales;

	switch(monthly_sales)
	{
	case 'a':
		{
			monthly_income = 375 + ( a * 16 / 100);
			printf("\nMonthly Income = $ %.2f\n\n", monthly_income);
			break;
		}
	}
}

>yeah.. i know about that.
Clearly not, or you wouldn't be having problems. You have a case for 50000. That case will only be executed if you type 50000, not 49999, not 50001 or any other value. You said you wanted a range, and the switch statement is not good for ranges. Remove it and replace it with an if..else chain:

if (monthly_sales < 50000)
    printf("Less than $50,000\n");
else if (monthly_sales > 50000)
    printf("Greater than $50,000\n");
else
    printf("Exactly $50,000\n");

It's just that simple.

yeah. i know if-else statement is good for ranges, but the question request do in a switch statement. so i also have to follow the order to do with it. :-/

Really, I want you keep that switch statement - keep working with it. Then you'll wind up pulling your hair out in frustration, and I'll look a lot better than you, in our forum yearbook pictures, because you'll be nearly bald. ;) ;)

Swt, bald... = =
i'll keep trys with it. thanks for helping anyways. :)

>but the question request do in a switch statement
You sure are inquisitive for someone who knows everything we've been saying. The answer remains the same from my first post. Allow me to make it absolutely clear: you must have a case for every value in the range. If the range goes from 0 to 50000, that just sucks for you. 'Nuff said. End of thread. kthxbye.

this is what i had tried assign the value of monthly sales into a variable, after that only switch statement.

#include <stdio.h>
void main()
{
	int monthly_sales, a;
	double monthly_income;

	printf("Enter your monthly sales amount > ");
	scanf("%d", &monthly_sales);

	if(monthly_sales >= 50000)
		a = monthly_sales;

	switch(monthly_sales)
	{
	case 'a':
		{
			monthly_income = 375 + ( a * 16 / 100);
			printf("\nMonthly Income = $ %.2f\n\n", monthly_income);
			break;
		}
	}
}

How with that code with monthly_sales equal the character 'a'? Yes, you assigned it to the variable a, but the switch statement is comparing it to the character 'a' which, for an implementation using ASCII or ANSI, will be the value 97. So you are checking to see if monthly_sales is equal to 97.

If you are required to use a switch statement due to the instructor's requirements, then use a if ( ) ... else if ( ) ... else if ( ) ... else ... chain to look at monthly_sales and set an integer or enumerated variable that I would name monthly_sales_range to an integer or char or enumerated constant to indicate the range. Then use a switch statment on monthly_sales_range to select the case for calculating monthly_income.

Here is one (of many) ways to define the variable month_sales_range.

enum sales_range {
   SALES_UNDER_50000, 
   SALES_BETWEEN_50000_75000, 
   SALES_BETWEEN_75000_100000, 
   SALES_OVER_100000
} monthly_sales_range;

Its far far better to have an if-else ladder or nested if-else for the purpose (as mentioned by others)
But still if you have to use switch, you can go for a nested switch construct like this:

int x = 5;
switch(x > 5)
{
    case 1:
         switch(x > 10)
         {
             case 1:
                  printf("\nx > 10\n");
                  break;
             case 0:
                  printf("\n5 < x <= 10\n");
                  break;
         }
    break;
    case 0:
         switch(x > 0)
         {
             case 1:
                  printf("\n0 < x <= 5\n");
                  break;
             case 0:
                  printf("\nx <= 0\n");
                  break;
         }
    break;
}

But let me tell you, this is only for the sake of the requirement of yours !!!
Happy coding :)

Its far far better to have an if-else ladder or nested if-else for the purpose (as mentioned by others)
But still if you have to use switch, you can go for a nested switch construct like this:

int x = 5;
switch(x > 5)
{
    case 1:
         switch(x > 10)
         {
             case 1:
                  printf("\nx > 10\n");
                  break;
             case 0:
                  printf("\n5 < x <= 10\n");
                  break;
         }
    break;
    case 0:
         switch(x > 0)
         {
             case 1:
                  printf("\n0 < x <= 5\n");
                  break;
             case 0:
                  printf("\nx <= 0\n");
                  break;
         }
    break;
}

But let me tell you, this is only for the sake of the requirement of yours !!!
Happy coding :)

hey dude, it cannot work for switch(x > 0)
it give me a warning.

Run the code despite the warning . What O/P did you get ?

I ran this code

int main()
{
        int x=5;

        switch(x >0)
        {
                case 1: printf("In case 1\n");
                        break;
                case 0: printf("In case 0\n");
                        break;
        }

        return 0;
}

and this was my O/P


$ ./test
In case 1
$

hey dude, it cannot work for switch(x > 0)
it give me a warning.

What warning are you getting....which compiler ??? :?:

What warning are you getting....which compiler ??? :?:

the symbol of '>' or '<'
:-O

Let's look at a simpler example:

int i = 5;

switch(i > 2) {
  case 3: printf("\n i equals 3"); break;
  case 4: printf("\n i equals 4"); break;
  case 5: printf("\n i equals 5"); break;
  default: printf(\n i is greater than 5");
};

This code (in a normal program), will compile without warnings or errors in my compiler - BUT IT IS WRONG! Your compiler may warn you about this, or even throw an error.

if you can run it, you may get a wrong answer - in my case I get the "i is greater than 5" message - clearly the wrong answer.

Why?

The switch statement has the format: switch(expression), and that expression needs to resolve to an integral type. i > 2 may resolve to TRUE normally, but it does not resolve ANYTHING for the value of i,

So all the case (statements) can't find a suitable answer. Those statements must be of integral type, as well.

if you add: case 1: printf("\n i equals 1"); to the above switch statement, you see the problem immediately. i > 2 has indeed returned 1, and the switch statement will choose "case 1:", for it's choice.

If you have one of the latest C standard compilers however, please try the above, and post up what happens for you. The standard for C has changed a lot in recent years, and that may include the specifications for the switch statement - but I doubt it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.