Process Control
3 Structure control program running in C language
1> sequential structure: The default process structure that executes each statement in the order in which it is written
2> selection Structure: Judge a given condition by deciding what code to execute according to the result of the decision
3> Loop structure: Executes a piece of code repeatedly when a given condition is true
1. Select structure
If statement
The first structure of the 1>if
if (condition)
{//The following statement is executed when the condition is established
Statement 1;
Statement 2;
.......
}
Example:
#include <stdio.h>
int main ()
{
Number
int cout = 0;
if (cout>=50);
{
printf ("Classes!!! \ n ");
}
return 0;
}
The second structure of the 2>if
if (condition)
{//The following statement is executed when the condition is established
Statement 1;
Statement 2;
.......
}
Eles
{
}
Example:
#include <stdio.h>
int main ()
{
Number
int cout = 0;
if (cout>=50);
{
printf ("Classes!!! \ n ");
}
Else
{
If the condition is not established, the following code will be executed
printf ("No classes!!! \ n ")
}
return 0;
}
The third structure of 3>if
if (condition 1)
{//The following statement is executed when the condition is established
Statement 1;
Statement 2;
.......
}
else if (condition 2)
{
}
else if (condition 3)
{
}
else if (condition 4)
{
}
Else
{
}
Example:
#include <stdio.h>
int main ()
{
int a = 10;
if (a==10)
{
printf ("a==10" \ n);
}
else if (a==5)
{
printf ("a==5" \ n);
}
else if (a==3)
{
printf ("a==3" \ n);
}
Else
{
Pintf ("A is another value \ n");
}
}
The fourth structure of 4>if
if ()
Statement 1;
Statement 2;
If the condition is set to execute statement 1, Statement 2 will also execute, if the condition is not true, the statement does not execute, the following will be executed
5>if Statement Usage Note
*if the condition compares the size, the constant value is placed on the left and the variable is on the right.
*if (); This semicolon cannot be written, which causes the condition to execute only an empty statement, losing the role of IF
{
}
* The following use must be noted
#include <stdio.h>
int main ()
{
if (10>6)
int a = 5; The scope of this A is not clear, so this code is wrong
return 0; If you want to define a new variable in the statement after the IF, you must use the curly braces {}
}
Switch statement
1> syntax structure
Switch (value)
{
Case value 1;
Statement 1;
Break Break: Exit the entire switch statement
If there is no break behind the case, the statements in all the following will be executed until Bresk is encountered
Case value 2;
Statement 2;
Break
Default:
Statement 3;
Break
}
2>switch Statement Usage Note
* Also on the scope of the problem, if you define a new variable in the case statement, will cause this variable scope confusion, system error.
Workaround: Define a new variable with {}, explicit variable scope
3>if and Switch comparisons
If the function of the statement can be completed, switch will not be able to complete
In some cases the IF statement and switch can be interchanged
Switch can complete the statement if all can be done
Dark Horse programmer-------------C-Language Process Control-select structure