I am pretty new with coding and compiling small programs in C and I have looked at the multiple threads regarding this issue and yet still haven't found or understood the answer I am looking for. I guess could someone explain in terms a kindergartener would understand, Why an if statement would be better than a switch or vise versa?
The reason I'm asking is because I find it easier mentally, to accomplish the task in a switch vs an if.
Should I practice more if's?
Without getting me lost in complex issues, will there be scenarios where switch cases will not work later on in complex programming?
The example I have is I choose to make a Celsius to Fahrenheit conversion with a switch instead of an if. Was this a "poor choice"?
Any feed back would be greatly appreciated especially if I can grasp the answer given.
Here's my code
{
int cel, fah;
char answer = 0;
printf("What would you like to convert from? Type: 'C' Celsius / 'F' for Fahrenheit\t");
scanf(" %c", &answer);
switch(answer){
case 'C':
case 'c':
printf("Enter Temperatur in Celsius:\t");
scanf("%d", &cel);
fah = (cel * 1.8) + 32;
printf("The Temperature in Fahrenheit is: %d\t\n", fah);
break;
case 'f':
case 'F':
printf("Enter Temperature in Fahrenheit:\t");
scanf("%d", &fah);
cel = (fah - 32) / 1.8;
printf("The Temperature in Celsius is: %d\t\n", cel);
break;
default:
printf("ERROR!!!\n");
}
return 0;