1. About the selection program (if statement and switch statement)
A:If statements are divided into simple if statements and IF-ELSE statements.
If statement:
if (expression 1)
{
Statement
}
If-else statement: note Else to be treated as a whole with the first if, and cannot be combined with the second if.
if (expression 1)
{
if (expression 2)
{
Statement 1
}
}
Else
{
Statement 2
}
The B:switch statement is a multi-branched structure program. (Switch executes the program, the match to which item, when the matching item is executed, will continue the subsequent program.) If you do not need to continue with the subsequent program, you need to use the break statement to terminate the program execution. )
switch (expression)
{
case constant Expression 1: statement 1
case constant Expression 1: statement 1
.........
Default: Statement n
}
2. About loops. (While loop, Do-while Loop, for Loop)
A:while Loop (executes the loop until the expression value is false)
while (expression)
B:do-while Loop (calculated first, in the value of the evaluated expression)
Do
{statement}while (expression)
The C:for loop (the value of expression 1 is evaluated first, then the value of expression 2 is evaluated, if true, the loop statement is executed, and expression 3 is evaluated.) The value of the expression 2 is then evaluated until its value is false and the loop is terminated)
for (expression 1; expression 2; expression 3)
3. Array (the subscript of the array element is starting from 0)
A: one-dimensional arrays
Data type array name [shaping constant expression];
B: two-dimensional array
Data type array name [shaping constant expression] [shaping constant expression] ... ;
A combination of multiple statements
For example: 1. Enter 10 student scores and ask for the best results.
int arr[10];
int i;
for (i=0;i<10;i++)
{
printf ("Please enter%d student scores: \ n", i+1);
scanf ("%d", &arr[i]);
}
int max=arr[2];
int A;
for (i=0;i<9;i++)
{
if (Arr[i]>max)
{
Max=arr[i];
}
}
printf ("m=%d\n", Max);
2. Enter a number to insert the number sequentially into the array.
int arr[8]={3,5,7,20,35,47,55};
int num;
printf ("Please enter a number: \ n");
scanf ("%d", &num);
int index=7;
int i=0;
for (i=0;i<index;i++)
{
if (num>=arr[i]&&num<arr[i+1])
{
index=i+1;
Break
}
if (Num<arr[0])
{
index=0;
Break
}
}
for (i=7;i>index;i--)
{
ARR[I]=ARR[I-1];
}
Arr[index]=num;
for (i=0;i<8;i++)
{
printf ("%d", arr[i]);
}
When writing code, be aware of:
1, defines the data type of the variable, in the output must correspond to the relative format string description. (int->%d;char->%c;char ARR[]->%S;FLOAT->%D;DOUBLE->%LF)
2. Like if (), while (), for () 、..... Cannot add ";" in the back.
3.for () The three expressions in parentheses must be ";" Separated.
4. As in the following case, when entering a character, the preceding sentence if followed by a newline code, you must follow the GetChar () to the line-break code to receive it, or the line-wrapping code will default to the next character bit.
printf ("Please enter a number: \ n", 1);
scanf ("%d", num);
GetChar ();
printf ("Please enter%d characters: \ n", 1);
scanf ("%c", &arr[i]);
5. When defining variables, this variable can only be used in the body of the function you define.
About C language