The switch statement is similar to the else-if construct but has more flexibility and a clearer format. Let's create a four function calculator using switch statement:
included files
void main()
{
float num1=1.0,num2=1.0;
char operator;
while(!(num1==0.0 && num2==0.0))
{
printf("Type number, operator, number\n");
scanf("%f %c %f",&num1,&operator,&num2);
switch(operator)
{
case'+':
printf(" = %f",num1 + num2);
break;
case'-':
printf(" = %f",num1 - num2);
break;
case'*':
printf(" = %f",num1 * num2);
break;
case'/':
printf(" = %f"num1 / num2);
break;
default:
printf("Unknown Operator!");
}
printf("\n\n")
}
getch();
}
In the above code, the statement 'case' is working as 'if', while 'default' is working as 'else' statement...
If you have any question, you can ask me freely!
included files
void main()
{
float num1=1.0,num2=1.0;
char operator;
while(!(num1==0.0 && num2==0.0))
{
printf("Type number, operator, number\n");
scanf("%f %c %f",&num1,&operator,&num2);
switch(operator)
{
case'+':
printf(" = %f",num1 + num2);
break;
case'-':
printf(" = %f",num1 - num2);
break;
case'*':
printf(" = %f",num1 * num2);
break;
case'/':
printf(" = %f"num1 / num2);
break;
default:
printf("Unknown Operator!");
}
printf("\n\n")
}
getch();
}
In the above code, the statement 'case' is working as 'if', while 'default' is working as 'else' statement...
If you have any question, you can ask me freely!
0 comments:
Post a Comment