# Thread: Problem with looping a "switch" in C programming

1. ## Problem with looping a "switch" in C programming

Hello and greetings to everyone. (I'm new here)
Anyways.........
I have a problem with my coding in C.

What I'm trying to do is to let a person pick a number between 1 to 3. (the switch function checks your answer and tells you what you have picked)

Then, if the answer is not 1, 2, or 3, the program loops and asks again.

Here is my code
Code:
```#include <stdio.h>

int main()
{
int a, b, c;

printf("Pick between 1, 2 or 3\n");
printf("INPUT A NUMBER:");
scanf("%d", &a);

while((a!=1)||(a!=2)||(a!=3))
{
switch (a)
{
case 1:
printf("You picked 1");
break;

case 2:
printf("You picked 2");
break;

case 3:
printf("You picked 3");
break;

default:
break;
}

}

getchar();

}```
Here's what happens when the program is run.
If you input 1, the line "You picked 1" appears infinitely. (this also happens with 2, 3 with their respective answers)
Finally, if i type in any number (which will lead to default) it shows the line "Your answer is not valid"
Can anyone point out to me what went wrong with my code?
I also tried to do the same thing without using the "switch" function

Code:
```#include <stdio.h>

int main()
{
int a, b, c;

printf("Pick a number from 1, 2 or 3\n");
printf("Input a number:");
scanf("%d", &a);

while(a != 1) /* ,a != 2, a != 3*/
{
printf("INPUT AGAIN:");
scanf("%d", &a);
}

if(a = 1)
{
printf("You inputted 1");
}
else if(a = 2)
{
printf("You inputted 2");
}
else if(a = 3)
{
printf("You inputted 3");
}

}```
but whenever i put the things in the comment box (beside the while function)
the program doesnt work properly.

I would really appreciate it if someone could point out my mistakes because i wanna get creative and improve my programming. Thanks.

2. Look at the condition for your while loop. You're telling it to loop for as long as a != 1, or a != 2, or a != 3. One of those is always going to be true since a can't be 1, 2 and 3 at the same time. Besides, how are you ever going to break out of the while loop if you never change the value of a once you enter the loop?

3. Code:
`while((a!=1)||(a!=2)||(a!=3))`
This condition will always be true, since if a == 1 of those numbers, it does not equal the other two. You should use && instead of ||. That way, if "a" equals one number, the condition will not be true (AND requires all conditions be met, OR requires only one).

4. Think like this:
What is the condition for the looping? What do you want it to do?
You want to loop AS LONG as the input is invalid.
When is the input invalid?
As long as the input is NOT 1 and NOT 2 and NOT 3.

Or if you want, here is an easier way not using inverted logic.
When is the input right?
It's correct if it's one of the following: 1, 2, 3.
That is, if input is 1 or 2 or 3.
Now, invert that logic: !(input == 1 || input == 2 || input == 3) and you basically get the inverted logic. That is, when the input is not right.
Now you can go in and change the expressions inside if you want. Every equal becomes inequal (!=) and every or becomes and.

5. Your second example didn't work because this:
Code:
`if(a = 1)`
assigns 1 to a.
You wanted ==

6. Select the do..while loop will be ok!

Code:
```int flag=0;
do
{
...........
if(a!=1&&a!=2&&a!=3)
flag=1;
else
flag=0;
}
while(flag);```

7. Doing the check in the if statement is the same as doing it in the actual while segment. You don't actually gain anything there, unless you just happen be checking to do something for a special case inside the loop anyway. As mentioned by others, the main thing needing changing was the || to &&.

Quzah.

8. Originally Posted by Steve Cao
Select the do..while loop will be ok!

Code:
```int flag=0;
do
{
...........
if(a!=1&&a!=2&&a!=3)
flag=1;
else
flag=0;
}
while(flag);```
do .. while can be an option but that is not how to do it!

9. I see now.
So the main problem lied in the while conditions.
I would like to thank everyone for their valuable input and ideas. It really broadened my views on C programming concepts. Btw, thanks for the C programming links.