C Language-switch statements

Source: Internet
Author: User

Switch (the value of an expression) {

Case 1:

Statement 1

Break

Case 2:

Statement 2

Break

Case 3:

Statement 3

Break

Case 4:

Statement 4

Break

......

Default

Default statement

Break

1, suitable for the discrete value of the switch judgment, can be converted into a continuous range

2, pay attention to case penetrating, break the use.

3. You can define each case value as an enum enumeration or use a macro definition to define an alias

1. Use switch statement completion: Enter a simple two-digit subtraction operation, using switch to calculate the value of the equation in various cases by converting the subtraction notation, which is a simple calculator:

intb; floatC; CharSign ; printf ("Please enter the number to be calculated:"); scanf ("%d",&a); GetChar ();    After entering the first digit, press ENTER to confirm that using GetChar to read the sign will be wrong to read the carriage return, so first use GetChar to read this character. printf ("Please enter the operation to be performed:"); Sign=GetChar (); printf ("Please enter the number to be calculated:"); scanf ("%d",&b); Switch(sign) { Case '+': C=a+b;  Break;  Case '-': C=a-b;  Break;  Case '*': C=a*C;  Break;  Case '/':            if(b) {C=a/b; }Elseprintf ("the divisor cannot be 0\n"); } printf ("%d%c%d=%f\n", a,sign,b,c);

2. Use the Helen formula to calculate the area of the triangle. The area of Helen's formula is, where p= (A+b+c)/2,a,b,c is a triangular three-edged length.

Note: This procedure should first determine whether the input of the three sides of the triangle is greater than the third side, the difference between the two sides is less than the third side of the condition, if not meet the conditions, it is not a triangle, can not use the Helen formula down calculation, the program ignores this judgment, assuming that three side length in line with the edge of the triangle This judgment is reflected in the next procedure.

float A,b,c,p,s;    printf (" please input triangle three edge length: \ n");    scanf ("%f%f%f", &a, &b, &c);         = (a+b+c)/2;     = sqrt (double) p * (P-A) * (p-b) * (P-c));        printf (" the area of the triangle is%.2f\n", s);

3, input triangle three side length, judge whether the triangle is right triangle or equilateral, isosceles, General triangle. Complete with if nesting.

floatA, B, C; printf ("please input triangle three edge length: \ n"); scanf ("%f%f%f", &a, &b, &c); if(a>0&& b>0&& c>0) {        if(a+b>c && a+c>b && b+c>a) {if(A==b && b==c) {printf ("input is a positive triangle \ n"); }Else if(a==b | | b==c | | a==c) {               if(a*a==b*b+c*c | | a*a+b*b==c*c | | a*a+c*c==b*b) printf ("input is isosceles right triangle \ n"); Elseprintf"input is isosceles triangle \ n"); }Else if(a*a==b*b+c*c | | a*a+b*b==c*c | | a*a+c*c==b*b) {printf ("input is right triangle \ n"); }Elseprintf"input is a general triangle \ n"); }        Elseprintf"input is not a triangle \ n"); }Else{printf ("input is not a triangle \ n"); }

4. Enter a percentile score, and use the switch statement to convert this score to the A,b,c,d hierarchy and output:

int score=0;
Char grade= ' 0 ';
scanf"%d",&score); if(score>=0&& score<= -) { Switch(score/Ten) { Case Ten: Case 9: Grade='A'; Break; Case 8: Grade='B'; Break; Case 7: Grade='C'; Break; Case 6: Grade='D'; Break; default: Grade='N'; Break; } printf ("the grade is converted to%c!!! \ n", grade); }Elseprintf ("Enter an illegal!!!! \ n");

C Language-switch statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.