<title>Process Control of the program (i)</title> On the process Control of Java programs (i)
From a structured programming perspective, the program has three structures:
- Sequential structure
- Select structure
- Loop structure
1. Sequential structure
is the program from the top to the next line of execution, the middle no judgment and jump.
2.if Conditional Statement ①
if(条件语句)
{
执行语句块
}
Where the conditional statement can be any kind of logical expression, if the conditional statement returns a result of true, executes the execution statement in the later brace pair, executes the other program code later, and the program skips the execution statement in the brace pair after the conditional statement.
Note: If there is only one statement in the curly braces, you can also omit this pair of curly braces
Such as:
int x=0
if(x==1)
System.out.println("x=1");
Ii
if(条件语句)
{
执行语句块1
}
else
{
执行语句块2
}
This format adds an ELSE clause after the IF clause, on the basis of the single if statement above, and when the conditional statement returns FALSE, the subordinate clause of the later part is executed, such as:
int x=0;
if(x==1)
System.out.println("X=1");
else
System.out.println("X!=1");
If the value of x equals 1 prints "x=1", otherwise "x!=1" will be printed.
For If......else ... Statement, there is a more concise notation:
变量=布尔表达式? 语句1:语句2;
The following code:
if(x>0)
y=x;
else
y=:-x;
Can be simply written as:
y=x>0?x:-x;
③
if(条件语句1)
{
执行语句块1
}
else if(条件语句2)
{
执行语句块2
}
……
else if(条件语句n)
{
执行语句块n
}
else
{
执行语句块n+1
}
If statements are nested:
When using the if nested statements, it is best to use {} to determine the level of each other's relationships.
Such as:
if(x==1)
{
if(y==1)
System.out.println("x=1,y=1");
else
System.out.println("x=1,y!=1");
}
else if(x !=1)
{
if(y==1)
System.out.println("x!=1,y=1");
else
System.out.println("x!=1,y!=1");
}
Process Control in Java (i)