Home  >  Article  >  Daily Programming  >  Three major process controls and two ways to interrupt loops in PHP

Three major process controls and two ways to interrupt loops in PHP

**熬夜选手
**熬夜选手Original
2020-04-30 16:43:2036browse

Three major process controls and two ways to interrupt the cycle in PHP

Three major process structures:

Sequential structure:

The natural state of program running is to run the program from front to back (top to bottom).

Branch structure:

During the running of the program, different points (others) are executed according to the different results of the judgment conditions (true or false). The branch is no longer executed).

Loop structure:

During the running of the program, depending on the different results of the judgment condition (true or false), it is decided whether to execute it again or not. Execute again.

if statement

1. Single branch

//形式:
if (  )//条件判断
{
//如果条件满足,就执行这里
}

2.Double branch

//形式:
if (   ) //条件判断
{
//如果条件满足,就执行这里
}
else
{
//如果条件不满足,就执行这里
}

3.Multiple branches

//形式:
if (   ){  //条件判断1
//分支1;
}
elseif(   ){  //条件判断2
//分支2;
}
elseif(   ){  //条件判断3
//分支3;
}

switch branch statement of branch structure

//语法:
switch( )
{
case  值1:   //如果$v1 等于 这个“值1”,就执行本分支
分支语句1;
break;   //表示跳出该分支,也就是跳出switch语句。
case  值2:   //如果$v1 等于 这个“值2”,就执行本分支
分支语句2;
break;
。。。。。。  //可以更多的分支
default:
默认分支; 
}

Note:

When a branch meets the conditions and the branch is executed, if there is no break statement in the branch, at this time, the program The process will "directly enter" the next branch to continue execution, and the switch will not end until break is encountered.

While loop statement of loop structure

//while循环语法:
while(条件判断)
{
。。。。循环体语句;
}

//循环结构之do while循环语句
do while循环语法:
do
{
。。。。循环体语句;
 
}while(条件判断)

Description:

First execute the loop body once, and then perform conditional judgment:

If true: continue to execute the loop body, and then perform conditional judgment again, and so on;

If it is not true: Exit the loop and execute subsequent statements.

The for loop statement of the loop structure

//for循环语法:
for(循环变量初始化1; 循环条件判断2; 循环变量的改变3){
。。。。。。。。循环体语句块4;
}

Interruption of the loop

Loop is a grammatical form that will continue to execute the loop body as long as the conditions are met according to the given conditions.

However, we can also artificially interrupt the loop during the loop process (loop body).

There are two ways to interrupt the loop:

continueInterruption:

Meaning: Interruption The current loop body (that is, subsequent statements will no longer be executed) continues with the statements to be executed in the next loop.

Grammar form:

continue [$n]; //Indicates which level of loop is to be interrupted and continues to the next level of the loop.

break interrupt:

Meaning: Stop (jump out) the current loop (that is, terminate the loop completely) and execute the statements after the loop.

Grammar form:

break [$n]; //Indicates how many levels of loops are to be interrupted.

Summary:

#By learning process control and continue and break statements, we can flexibly control our code to achieve the effect we want. Hope it can be helpful to everyone.

The above is the detailed content of Three major process controls and two ways to interrupt loops in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn