Tips for using C _ branch and loop statements and branch statements
Branch statement if Statement AnalysisIf statements are used to select execution statements based on conditions. Else cannot exist independently and always matches the nearest if Else statements can be followed by other if statements. Notes for comparing null values in if Statements
Sample Code
Bool variables should appear directly in the condition and should not be compared.
if(b)
{
//statement 1
}
else
{
//statement 2
}
When the variable is compared with the 0 value, the 0 value should appear on the left side of the comparison symbol (so that the compiler can find the error of writing = As =)
int i = 1;
if( 0 == i)
{
//statement 1
}
else
{
//statement 2
}
Float variables cannot be compared with 0 values directly, so precision must be defined.
#define EPSION 0.00000001
float f = 0.0;
if((-EPSION <= f) && (f <= EPSION ))
{
//statement 1
}
else
{
//statement 2
}
Switch Statement Analysis
Switch statement corresponding to a single condition score
The case statement branch must have a break; otherwise, the Branch will overlap.
The default statement must be added to handle special cases.
The Value in the case statement can only be of integer or limit type.
Case statement arrangement order
Sort statements in alphabetical order
The normal situation is put at the beginning, and the exception is put at the end
The default statement is only used to handle real default conditions.
In fact, if and switch statements can be exchanged.
Loop statement
Basic Method of loop statements
Use a conditional expression to determine whether to execute a loop body condition expression following the if statement expression principle
Do, while,
The do statement is executed first and then determined. The loop body executes the while statement at least once before the judgment and execution. The loop body may not execute the for statement before the judgment and execution, which is more concise than the while statement.
Do-while loop
While loop Running Mode
For Loop Running Mode
We can see that the for loop has an initial zone and a loop termination zone more than the while loop.
Difference between break and continue
Break indicates that the execution of the terminate loop continue indicates that the current loop is terminated and the next loop is entered.
Do_while
#include
#include
int func(int x)
{
int i = 0;
int ret = 0;
int* p = (int*)malloc(sizeof(int)*n);
do
{
if(NULL == P) break;
if(n < 5) break;
if(n > 10) break;
for(i = 0;i < n;i++)
{
p[i] = i;
printf("%d\n",p[i]);
}
ret - 1;
}while(0);
free(p);
return ret;
}
int main()
{
if(func(10))
{
printf("OK\n");
}
else
{
printf("ERROR\n");
}
return 0;
}
In the above Code, break indicates that the pointer p is released no matter how it is returned. Prevent Memory leakage.
Change code
#include
#include
int func(int x)
{
int i = 0;
int ret = 0;
int* p = (int*)malloc(sizeof(int)*n);
if(NULL == P)return ret;
if(n < 5)return ret;
if(n > 10)return ret;
for(i = 0;i < n;i++)
{
p[i] = i;
printf("%d\n",p[i]);
}
ret - 1;
free(p);
return ret;
}
int main()
{
if(func(10))
{
printf("OK\n");
}
else
{
printf("ERROR\n");
}
return 0;
}
This may cause memory leakage. When n <5 or n> 10, the pointer p is not released. Memory leakage may occur.