Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Limiting variable between 2 number

1. ## Limiting variable between 2 number

Dear all,

My program have 4 directional switches with the value shown below:

up = -2
right = -3
down = -5
left = -9

I'll to write something like........

count = 1;

if(switch==-3)
{
count++;
}
if(switch==-9)
{
count--;
}

switch(count);
{
case '1':
printf("........");
break;

case '2':
printf("........");
break;

case '3'
printf(".........");
break;
}

Is it possible to limit the count between 1 and 3 (mimimum is 1 and maximum is 3 no matter how many 'left' or 'right' i press), so that the program will display something. Cause if i don't limit 'count', 'count' will also increase/decrease by the amount of time i pressed.

Value of count does no change when i press 'up' or 'down'.

Hope you get wat i mean

west_dweller

• It depends on how you want to limit it, if it will go back to zero after hitting 3, then you could use the modulus operator, but if you want it to stay at three when it hits it, then a simple check could be implemented. By the way, I think you'll have to change the name of your variable 'switch', for it interferes with a C keyword.

• Code:
```if(switch==-3)
{
count++;
}
if(switch==-9)
{
count--;
}

if (count <= 1) count++;
if (count >= 3) count--;

switch(count);
{
case '1':
printf("........");
break;

case '2':
printf("........");
break;

case '3'
printf(".........");
break;
}```
As Dr. Evil said, you are definetly going to have to switch that switch, if you catch my meaning.
(If I misread the question, my apologies)

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•