C Language Conditional control statement (iii)

Source: Internet
Author: User
Tags constant expression printf

3.3.2switch statement
An if statement can only handle one of the options, and when it comes to one of several possible implementations, it is implemented with If...elseif or even multiple nested if, and the program becomes more verbose and less readable when there are more branches. The C language provides a switch switch statement that deals specifically with multiple branch cases, making the program concise.
The general format of the switch statement is:
switch< expression >
case constant Expression 1: statement sequence 1;
Break
Case constant Expression 2: statement sequence 2;
Break
...
case constant Expression N: statement n;
Break
Default: Statement n+1;
Where the value of a constant expression must be an integer, a character or an enumeration type, each statement sequence allows multiple statements, does not need to be processed by a compound statement, and if the statement sequence I is empty, the corresponding break statement can be removed. Figure 3-7 is the flowchart for the switch statement.
In special cases, if multiple values of a switch expression need to execute the same statement, you can use the following format:

Switch (i)
{
CASE1:
CASE2:
CASE3: statement 1;
Break
CASE4:
CASE5: statement 2;
Break
Default: Statement 3;
}

When the value of integer variable i is 1, 2, or 3 o'clock, execute statement 1, when I is 4 or 5 o'clock, execute statement 2, otherwise, execute
Statement 3.
[Example 3-9] Enter the month and print it for a few days in 1999.
The procedure is as follows:
#include <stdio.h>
Main ()
{
int month;
int day;
printf ("Please input the month number:");
scanf ("%d", &month);
Switch (month)
{
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:day=31;
Break
Case 4:
Case 6:
Case 9:
Case 11:day=30;
Break
Case 2:day=28;
Break
Default:day=-1;
}
If Day=-1
printf ("Invalid Month input!\n");
Else
printf ("1999.%dhas%ddays\n", month,day);
}
3.3.3 Program Application Examples
[Example 3-10] to solve the unary two-Ax2+bx+c=0,a equation, B, c by the keyboard input.
Analysis: The factors A, B, C consider the following circumstances
1) If a=0:
①b<>0, then x=-c/b;
②b=0, then: ①c=0, then x amorphous root;
②c<>0, then X has no solution.
2) If a<>0;
①b2-4ac>0, there are two unequal real roots;
②b2-4ac=0, with two equal real roots;
③b2-4ac<0, there are two conjugated complex roots.
Complete with a nested if statement. The procedure is as follows:
#include <math.h>
#include <stdio.h>
Main ()
{
float a,b,c,s,x1,x2;
doublet;
printf ("Please input a,b,c:");
scanf ("%f%f%f", &a,&b,&c);
if (a==0.0)
if (b!=0.0)
printf ("The root is:%f\n",-c/b);
ElseIf (c==0.0)
printf ("X is inexactive\n");
Else
printf ("No root!\n");
Else
{
S=b*b-4*a*c;
if (s>=0.0)
if (s>0.0)
{
T=SQRT (s);
x1=-0.5* (b+t)/A;
x2=-0.5* (B-T)/A;
printf ("There are two different roots:%fand%f,\xn1", x2);
}
Else
printf ("There are two equal roots:%f\n", -0.5*b/a);
Else
{
T=SQRT (-s);
x1=-0.5*b/a;/* Real Department * *
X2=abs (0.5*t/a); * The absolute value of the imaginary part * *
printf ("There are two virtual roots:");
printf ("%f+i%f\t\t%f-i%f\n", x1,x2,x1,x2);
}
}
}
The results of the operation are as follows:
RUN
Please input a,b,c:123
There are two virtual roots:
-1.000000+i1.000000-1.000000-i1.000000
Rnu
pleaseinputa,b,c:253
There are two different roots:-1.500000and-1.000000
Rnu
Please input a,b,c:003¿
No root!

Related Article

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.