Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Basic structure of the second procedure
A branch structure
on Single Branch structure
Format:
if (an expression)
{
Statement
}
where "expression" is the criterion, as long as the value of the expression is not 0, the condition is considered to be valid.
Geneva Dual-branch structure
Format:
if (an expression)
Statement 1;
Else
Statement 2;
Where the value of the expression is not 0, then "statement 1" is executed, otherwise "statement 2" is executed.
//enter two integers, output these two numbers in order from small to large#include<stdio.h>intMain () {intA; intb; inttemp; //prompts the user to enter a two integerprintf"Please enter a two integer: \ n"); //accepts two integers entered by the userscanf"%d%d",&a,&b); //Compare Output if(a>b) {temp=b,b=A; printf ("%d%d\n", Temp,a); } Else{printf ("%d%d\n", A, b); } return 0;}
Geneva Multi-branch structure
Format:
if (expression 1)
Statement 1;
else if (expression 2)
Statement 2;
..........
else if (expression N)
Statement N;
Else
Statement n+1;
Where the values of each expression are evaluated sequentially, if the value of an expression is true, then the corresponding statement after execution executes, the entire if statement ends, and the remaining statements are not executed; if none of the statements is true, then the last else statement is executed.
//input Three number x1,x2,x3, three number of output by small to large order#include<stdio.h>intMain () {//define four variables intx1,x2,x3,temp; //Prompt user to enter three numbersprintf"Prompt user to enter three numbers: \ n"); //accept data entered by the userscanf"%d%d%d",&x1,&x2,&x3); //Compare and Output if(x1>x2) {Temp=x1;x1=x2;x2=temp; } if(x1>x3) {Temp=x1;x1=x3;x3=temp; } if(x2>x3) {Temp=x2;x2=x3;x3=temp; } printf ("%d,%d,%d\n", x1,x2,x3); return 0;}
if Nesting of statements
Format:
if (expression 1)
if (expression 2) statement 1;
Else Statement 2;
Else
if (expression 3) statement 3;
Else Statement 4;
Where you pay attention to the pairing relationship of else with If. The C language stipulates that else is always the closest to it and is not paired with an if other else pair.
to Conditional Operators
Conditional operator "? : "is a trinocular operator whose function is equivalent to a simple if-else statement.
Format:
Expression 1? Expression 2: Expression 3
Its function is to calculate the value of "Expression 1" first, if the value of "Expression 2" is the value of the entire conditional expression, if False, take the value of "Expression 3" is the value of the entire expression.
Attention:
* Conditional operators have precedence over assignment operators, lower than relational operators and arithmetic operators
Example: Max =a>b?a:b equivalent max= ((a>b)? a:b)
* The binding of the conditional operator is "from right to left"
A>b?a:c>d?a:d equivalent A>b?a: (C>D?A:D)
* Expressions 2 and 3 can be not only numeric expressions, but also assignment expressions and function expressions
* The expression type can be different, and the type of the conditional expression value is the type with the higher type in Expression 2 and expression 3.
x>y?1:1.5//x>y When the return value is 1.0
//a park ticket ticket price is 10 yuan per person, one purchase ticket full 30, each can receive one yuan less, write a charge procedure#include <stdio.h>intMain () {intNumber,sum,money,balance;//Defining Variablesprintf ("number of input: \ n"); scanf ("%d",&Number ); if(number>= -) {sum= number*9; } Else if(number*Ten< the) {sum=Ten*Number ; } Else{sum= the; } printf ("should be subject to a ticket of%d yuan \ n", sum); printf ("actually collect money: \ n"); scanf ("%d",&Money ); Balance= money-sum; printf ("balance=%d\n", balance); return 0;
The switch statement
Basic format for switch:
Switch ()
{
case constant Expression 1: statement Group 1; "Break;"
Case constant Expression 2: statement Group 2; "Break;"
...............................
case constant Expression N: statement group N; "break;"
[Default: Statement group N +1;]
}
//enter two integers to output the larger one.#include<stdio.h>intMain () {intb; printf ("Please enter a two integer: \ n");//Prompt user for data entryscanf"%d%d", &a,&b);//Accept Data//judging the output if(a>b) {printf ("max=%d\n", a); } Else{printf ("max=%d\n", B); } return 0;
Attention:
The expression following the L switch can only be an integer, a character, an enum-type expression
The statement following the case is executed when the value of the expression is equal to the value of one of the following. Then, the process control is transferred to the next case where the statement continues execution. If you do not want to continue executing the break statement causes the process to jump out of the switch structure, which terminates the switch statement execution.
If the value of an expression does not match any constant expression, execute the statement following the default
Each constant expression must be different, or there will be a contradiction
Dark Horse programmer------C Language Learning Notes---program basic structure