If, while usage differences in different languages

Source: Internet
Author: User
Tags php language php tutorial

If, while usage differences in different languages


If, while, and other branch loop structures inherit the external scope, that is, the external variables are visible inside the branch loop structure.

However, the C language does not support the internal scopes of if, while and other branch loop structures, whereas PHP does not.

In PHP, if, while, and other local variables declared in the branch loop structure are still valid after the branch loop structure.

For example, variables declared in a function are invisible outside the function.

C language:

# Include <stdio. h>

Int foo ()
{
Int bar;
Bar = 3;
}

Int main ()
{
Printf ("% dn", bar );
Return 0;
}

Error:
Scope. c: In function 'main ':
Scope. c: 11: Error: 'bar' is not declared (used for the first time in this function)
Scope. c: 11: Error: (even if it appears multiple times in a function, each undeclared identifier
Scope. c: 11: Error: only one report is reported in the function .)

PHP language:

<? Php Tutorial
Function foo ()
{
$ Bar = 1;
}

Echo $ bar;

Error:
Notice: Undefined variable: bar in scope. php on line 7
PHP Notice: Undefined variable: bar in scope. php on line 7

 

The if, while, and other loop branch structures inherit the external scope, that is, the external variables are visible inside the loop branch structure.

Similar:

C language:

# Include <stdio. h>

Int main ()
{
Int foo = 1;
If (1)
    {
Int bar = foo;
Printf ("% dn", bar );
    }

While (1)
    {
Int bar = foo;
Printf ("% dn", bar );
Break;
    }

Return 0;
}

Output:

1

1

PHP language:

<? Php
$ Foo = 1;
If (1)
{
$ Bar = $ foo;
Echo $ bar, "n ";
}

While (1)
{
$ Bar = $ foo;
Echo $ bar, "n ";
Break;
}

Output:

1

1

But it's different.

C language:

# Include <stdio. h>

Int main ()
{
Int foo = 1;
If (1)
    {
Int bar = foo;
Printf ("% dn", bar );
    }

Printf ("% dn", bar );

While (1)
    {
Int bar = foo;
Printf ("% dn", bar );
Break;
    }

Printf ("% dn", bar );

Return 0;
}

Error:

Scope. c: In function 'main ':
Scope. c: 12: Error: 'bar' is not declared (used for the first time in this function)
Scope. c: 12: Error: (even if it appears multiple times in a function, each undeclared identifier
Scope. c: 12: Error: only one report is reported in the function .)

PHP language


<? Php
$ Foo = 1;
If (1)
{
$ Bar = $ foo;
Echo $ bar ++, "n ";
}

Echo $ bar, "n ";

While (1)
{
$ Bar2 = $ foo;
Echo $ bar2 ++, "n ";
Break;
}

Echo $ bar2, "n ";


Output:

1

2

1

2

1. IF statement

IF statements are an important feature of most languages. They execute program segments based on conditions. PHP's IF statement is similar to C:

If (expr)

Statement
 

As discussed in the expression, expr is calculated as its true value. If expr is TRUE, PHP executes the corresponding statement. If it is FALSE, ignore it.

If $ a is greater than $ B, the following example shows 'a is bigger than B ':

If ($ a> $ B)

Print "a is bigger than B ";
 

Generally, you want to execute more than one statement according to the condition. Of course, you do not need to add IF judgment to each statement. Instead, you can combine multiple statements into a statement group.
If statements can be nested in other IF statements, allowing you to flexibly execute various parts of the program with conditions.

2. ELSE statements

Generally, you want to execute a statement when the specified conditions are met. If the conditions are not met, execute another statement. ELSE is used to do this. ELSE extends the IF statement and executes another statement when the IF statement expression is FALSE. For example, if $ a is greater than $ B, 'A is bigger than B 'is displayed. Otherwise, 'A is NOT bigger than B' is displayed ':

If ($ a> $ B ){
Print "a is bigger than B ";
}
Else {
Print "a is NOT bigger than B ";
}
 


3. ELSEIF statement

ELSEIF, as shown in the name, is a combination of IF and ELSE, similar to ELSE. It extends the IF statement to execute other statements when the IF expression is FALSE. But unlike ELSE, it only executes other statements when the ELSEIF expression is TRUE.

Multiple ELSEIF statements can be used in one IF statement. The first statement with the ELSEIF expression TRUE will be executed. In PHP 3, you can also write 'else if' (two words) and 'elseif' (one word. This is just a small difference in writing (if you are familiar with C, it is also), the results are exactly the same.

The ELSEIF statement is executed only when the IF expression and any earlier ELSEIF expression are both FALSE and the current ELSEIF expression is TRUE.
The following is an IF statement containing the nested formats of ELSEIF and ELSE:

If ($ a = 5 ):
Print "a equals 5 ";
Print "...";
Elseif ($ a = 6 ):
Print "a equals 6 ";
Print "!!! ";
Else:
Print "a is neither 5 nor 6 ";
Endif;
 


4. WHILE statement

WHILE loop is a simple loop of PHP 3. As in C. The basic format of the WHILE statement is:

WHILE (expr) statement

The WHILE statement is very simple. It tells PHP to repeatedly execute nested statements as long as the WHILE expression is TRUE. Check the value of the WHILE expression at the beginning of each loop. Therefore, this execution will not terminate even if its value is changed in the nested statement, and until the loop ends (every time PHP runs a nested statement, it is called a loop ). Similar to the IF statement, you can enclose a group of statements in braces and execute multiple statements in the same WHILE loop:

WHILE (expr): statement... ENDWHILE;

In the following example, all numbers are 1 to 10:

 

/* Example 1 */
$ I = 1;
While ($ I <= 10 ){
Print $ I ++;/* the printed value wocould be $ I before the increment (post-
Increment )*/
}

/* Example 2 */
$ I = 1;
While ($ I <= 10 ):
Print $ I;
$ I ++;
Endwhile;
 
5. DO... WHILE statement

DO... WHILE is very similar to a WHILE loop, but it checks whether the expression is true at the end of each loop, rather than at the beginning of the loop. The main difference between it and the strict WHILE loop is DO .. the first loop of WHILE must be executed (the true value expression is only checked at the end time of the loop), instead of strictly executing the WHILE loop (check the true value expression at the beginning of each loop, if it is set to FALSE at the beginning, the execution will be terminated immediately ).

DO... WHILE loop only has one form:

$ I = 0;
Do {
Print $ I;
} While ($ I> 0 );
 
The preceding loop is executed only once, because after the first loop, when the true value expression is checked, it is calculated as FALSE ($ I is not greater than 0) and the loop is terminated.

6. FOR loop statements

A FOR loop is the most complex loop in PHP. As in C. The syntax of the FOR loop is:

FOR (expr1; expr2; expr3) statement

The first expression (expr1) is unconditionally calculated (executed) at the beginning of the loop ).
The expression expr2 is calculated for each loop. If the result is TRUE, the loop and nested statements continue to be executed. If the result is FALSE, the entire loop is closed.
At the end of each loop, expr3 is calculated (executed). Each expression can be empty. If expr2 is null, the number of cycles is not fixed (PHP defaults to TRUE, like C ). Do not end the loop unless you use a BREAK statement to replace the true value expression of.
Consider the following example. They all show numbers 1 to 10:

/* Example 1 */
For ($ I = 1; $ I <= 10; $ I ++ ){
Print $ I;
}

/* Example 2 */
For ($ I = 1; $ I ++ ){
If ($ I> 10 ){
Break;
}
Print $ I;
}

/* Example 3 */
$ I = 1;
For (;;){
If ($ I> 10 ){
Break;
}
Print $ I;
$ I ++;
}
 
Of course, the first example is obviously the best, but you can find that in the FOR loop, you can use an empty expression in many cases.
In other languages, a foreach statement is used to traverse an array or hash table. PHP uses the while statement and the list () and each () functions to achieve this function.

For more details, see: http://www.111cn.net/phper/18/33202.htm

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.