C Language-IF statements

Source: Internet
Author: User

1, general form if (expression) expression 1 Else expression 2: expression set (true) executes expression 1, otherwise an expression 2 is executed.

Scope of application: True and false, right and wrong, switch, the opposite condition

Note: If you include only one statement in the IF statement, you can omit {} But it is best not to omit it, if there are multiple statements, be sure to add {}

If else does not contain any statements, you can omit the else

2, Branch if (expression 1) {statement 1}else if (expression) {statement 2} else if (expression 3) {statement 3} ..... Else (statement 4)

Expression 1 is established immediately executes statement 1, otherwise the expression 2, expression 2 is immediately executed statement 2, otherwise the expression 3, expression 3 is immediately executed statement 3, expression 1, expression 2, expression 3 is not true, then execute statement 4

The conditions in a branch if statement are mutually exclusive and cannot be duplicated or contained in each other. If so, we must re-clarify the process of thinking.

If the condition is a continuous range, all cases are covered.

Scope of application: conditions mutually exclusive, there is no coincidence conditions, and conditions are a continuous range of time with

3. Nested form:

if (expression) {

if (expression 1)

{

Statement 1//expression and expression 1 also established

}else{

Statement 2//expression formed and expression 1 not established

}

}else{

if (expression 2) {

Statement 3//Expression not valid and expression 2 established

}else{

Statement 4//expression is not valid and expression 2 is not valid

}

}

Scope of use: suitable for progressive progression, from large to small subdivision conditions, can be nested

Three-mesh operator expression? Statement 1: Statement 2 is equivalent to an if (expression) statement 1 Else Statement 2;

1. Use the IF branch to selectively output the largest number of two numbers:

    int a=2;     int b=3;     if (a>B)    {        printf ("2");    }     Else     {        printf ("3");    }

2. Use the IF statement to determine whether a number entered from the keyboard is odd or even

intA=0;//The variable is best initialized when it is defined. printf ("Please enter a number:"); scanf ("%d",&a); if(a%2==0)//The remainder of a divided by 2 is judged, if the remainder is 0, then a is an even number, otherwise a is an odd number. {printf ("%d is an even number \ n", a); }    Else{printf ("%d is an odd number \ n", a); }

3. The C language model of the piecewise function in mathematics: If x is 0, the value of y is 1000, if x is not 0,y the value is x 1,

floaty=0;//Defining Variables    intx=0; printf ("Please enter x:");//inputscanf"%d",&x); if(x!=0)//Judging{y=(float)1/x;    The result of the operation must be cast to float type. }    Else{y= +; }

4, enter three numbers, find out the maximum and output:

intA=0; intb=0; intC=0; inttemp=0; printf ("Please enter three number: \ n"); scanf ("%d%d%d",&a,&b,&c); if(a>b) {temp=A; }    Else{Temp=b; }    if(temp>c) {printf ("max=%d\n", temp); }    Else{printf ("max=%d\n", c); }

5, three the second algorithm to find the maximum value: first assume the first number is the largest, and then compare with the second number, get the largest, and finally the third number compared:

intA=0; intb=0; intC=0; intmax=0; printf ("Please enter three number: \ n"); scanf ("%d%d%d",&a,&b,&c); Max=a;//selection method to find the maximum value of three numbers    if(max<b) {max=b; }    if(max<c) {Max=C; } printf ("The maximum number is%d", Max);

6, judge a two-digit digit is odd and 10 is even: first judge the number of two digits, and then judge the subsequent conditions

intA=0; printf ("Enter a number:"); scanf ("%d",&a); if(a>9&&a< -&&a%2==1&&a/Ten%2==0) {printf ("A is a two-digit number, and the digits are odd, and 10 bits are even \ n"); }    Elseprintf ("A does not meet the criteria \ n");

7. Determine if an input number is an integer:

floatA; scanf ("%f",&a); printf ("the number entered is%f", a); if(A-(int) a==0) {printf ("A is an integer"); }    Else{printf ("A is not an integer"); }

8. Sort four numbers using bubble sort (without a for loop) assuming the first number is the smallest, then comparing the number of subsequent numbers to the last digit, then comparing the first number with the number before the last digit, and then the second small number, and placed in the penultimate position, Repeat the loop until you reach the maximum position in the first place.

intA=0; intb=0; intC=0; intD=0; inttmp=0; printf ("Please enter four numbers:"); scanf ("%d%d%d%d",&a,&b,&c,&d); if(a<b) {tmp=b; b=A; A=tmp; }    if(b<c) {tmp=C; C=b; b=tmp; }    if(c<d) {tmp=D; D=C; C=tmp; }        if(a<b) {tmp=b; b=A; A=tmp; }    if(b<c) {tmp=C; C=b; b=tmp; }    if(a<b) {tmp=b; b=A; A=tmp; } printf ("from big to small is%d,%d,%d,%d\n", a,b,c,d);

9, according to the price discount conditions for the payment of the calculation: if the price of three commodity has a more than 50 or three commodity prices and ultra-high 100, the total price discount 85%.

floatAprice,bprice,cprice; floatSumprice; printf ("Please enter the price of three items:"); scanf ("%f%f%f",&aprice,&bprice,&Cprice); if(aprice> -|| Bprice> -|| Cprice> -|| Aprice+bprice+cprice> -) {Sumprice= (aprice+bprice+cprice) * (1-0.15); }    ElseSumprice=aprice+bprice+Cprice; printf ("The amount payable is%.1f\n", Sumprice);

10. Use the branch if statement to determine the maximum number of three numbers:

int a,b,c,m;    scanf ("%d%d%d",&a,&b,&c);         if (a>=b&&a>=c) {        m=A;    }     Else if (b>=a&&b>=c) {        m=b;    }     Else m=C;     printf (" maximum value is%d\n", M);

11. Use nested IF statements to find the largest number in three numbers

inta,b,c,m; scanf ("%d%d%d",&a,&b,&c); if(a>b) {if(c>a) M=C; Elsem=A; }Else{        if(c>b) M=C; Elsem=b; } printf ("The biggest is:%d\n", m);

12, using the three mesh operator to find the largest number of three, method one:

int a,b,c,m;        scanf ("%d%d%d",&a,&b,&c);        M= (((a>b) a:b) >c? ( (a>b)? a:b): c);        printf (" maximum value is%d\n", M);

13, using three mesh operator to find the largest number of three, method two:

int a,b,c,m;    scanf ("%d%d%d", &a, &b, &c);     = A;     = m > B? m:b;     = m > C?   M:c; printf (" maximum value is%d\n", M);

14, use three mesh operator to find the largest number of three numbers, method three:

int a,b,c,m;    scanf ("%d%d%d", &a, &b, &c);     = (a>=b && a>=c)?   A: (b>=a && b>=c b:c); printf (" maximum value is%d\n", M);

15, using three mesh operator to find the largest number of three, method four

int a,b,c,m;    scanf ("%d%d%d", &a, &b, &c);     = A>=b?   (A>=c a:c): (B>=c b:c); printf (" maximum value is%d\n", M);

C Language-IF statements

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.