3.2 Code structure (order + branch + loop) + function

Source: Internet
Author: User

Code structure

Typically, the execution structure of the code is divided into three types: sequential structure, branching structure, circular structure order structure

The code executes from the top down. Branching structure

Given the conditions, the results should be different under various conditions.

Branching structure: The corresponding code is executed selectively by the judgment of the condition.

There are two types of branching structures: If branch, switch branch if branch

The simplest grammar

if (conditional expression) {

The result returned by the conditional expression is true

Block of code to execute

}

Standard branch syntax

if (conditional expression) {

True the code block to execute

}else{

False The block of code to execute

}

Complex (compound) syntax

if (conditional expression 1) {

code block that satisfies condition 1

}elseif (conditional expression 2) {

code block that satisfies condition 2

}...

Switch Branch

A switch branch is a block of code that is judged on a fixed condition and selectively executes the corresponding condition.

Grammar

switch (conditional expression) {

Case Condition 1:

code block

Break Jumps out of the current branch execution, switch terminates execution

Case Condition 2:

....

Default

All are not satisfied with the case that the execution

}

The difference between if and switch

1. If conditional expressions are usually comparison or logical operations, and switch's conditional expressions are usually fixed values

2. The IF branch can only satisfy one condition, but switch may perform multiple conditions (no break)

3. If the condition is judged sequentially, and switch is a cyclic judge

The trinocular operation and the if structure are interchangeable. Loop structure

A looping structure is a piece of code that needs to be executed repeatedly within a specified range. (iteration)

Loops in PHP: For,while,do-while,foreach

foreach is a loop provided in PHP specifically for an array traversal. For loop

The For loop is the loop with the most conditions required, and the condition is controlled within the loop.

Grammar

for (expression 1; expression 2; expression 3) {

Loop Body: code block

}

Expression 1: Initialization of the loop condition, can have multiple expressions, separated by commas between expressions

Expression 2: Conditional-judgment expression, judging the boundary value of a condition

Expression 3: Conditional change expression, usually self-operating operation

The For loop can have no conditions

Loop control

Within the loop, the loop is processed by setting certain conditions: Resume loop continue and break out of loop

For Loop process

for (expression 1; expression 2; expression 3) {

Loop Body 4

}

Process: ① Conditional initialization: Execution expression 1;② execution condition: expression 2;③ satisfies condition: execution loop body: Loop body 4;④: Execution loop condition change: expression 3;⑤: Repeating ②③④ until ③ step, not meeting condition end While loop

You only need to judge the loop condition and control the cycle of the cyclic condition change within the loop.

Grammar

while (conditional expression) {

Loop body

Loop control

Change of the cyclic variable

}

Do-while Cycle

Consistent with the while loop, the difference is that the Do-while loop is bound to execute a loop body, while the while is not necessarily.

Grammar

do{

Loop body

Loop control

Cyclic condition Change

}while (conditional expression);

99 Multiplication Table

Homework:

1. Make a diamond

2. Make a hollow diamond

4 Layer Diamond

*

* *

* *

* *

* *

* *

* *

* *

* Template Label

PHP code needs to be embedded in the HTML, PHP a structure will need to include part of the HTML, if the use of curly braces will cause the code to become ugly. When PHP is embedded, you can use template tags instead of the original braces

Opening braces: Using colons:

Closing brace: use end + corresponding tag +;

for () {

}

Template tags

For ():

ENDfor;

PHP Nested HTML execution flow

Template markup supported by PHP

If:if:endif;

For:for (): endfor;

While:while (): Endwhile;

Foreach:foreach (): Endforeach; Function

1. What is a function?

A block of code that can be called repeatedly (multiplexed)

2. Why should I have a function?

To reuse a block of code in many places.

Core idea: Classification of modular programming functions

Functions are divided into two categories: system functions and custom functions

System functions: After the system has been set, the user only needs to call the function

Custom functions: The user must define the function before use (the called function must exist in memory (code snippet) beforehand)

Component of Function: function name, parameter list, function body, return value

Grammar

Function name (argument list) {

function body

Return value: Return the data to be returned

}

Parameter list: Can be no, or can be multiple

Return value: Can not

Calling functions

function calls are directly using the function name (parameter list)

function definition and invocation principle

function naming

There are two common ways of naming functions: Camel Hump and Underline method

Hump method: First letter lowercase, followed by the first letter uppercase parameters & arguments

Formal parameters: The parameter used when defining a function is called a formal parameter, and there is no value at the time of definition, which can be used internally.

Argument: The actual argument passed in when the function is called is called an argument

If a formal parameter is specified when the function is defined, the corresponding argument must be specified at the time of the call

Memory Analysis Diagram

The relationship of formal participation in real parameters

1. If a formal parameter is specified when defining a function, the call must pass in the actual argument

2. The number of arguments must not be less than the number of formal parameters, more than the part will be ignored

3. The position of the argument must correspond to the formal parameter

4. The name of the formal parameter has no relation to the name of the argument.

Job: Print a hollow diamond in conjunction with a function, using the function's parameters to control the number of layers

3.2 Code structure (order + branch + loop) + function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.