Original PHP Tutorial: loop statements

Source: Internet
Author: User
While // while is to first judge the condition, if it is true, it is executed and executed multiple times, knowing that the condition is invalid

While // while is to first judge the condition, if it is true, it is executed and executed multiple times, knowing that the condition is invalid

Syntax:

While (expression)

Execute a statement repeatedly;

 

While (expression ){

Execute this loop body repeatedly

}

------------------------------------------------------------------------

Do-while // do-while is the condition that is first executed and then judged

------------------------------------------------------------------------

For

Syntax:

For (initial activation; conditional expression; increment ){

Loop body

}

------------------------------------------------------------------------

While example:

$ Num = 0;

While ($ num <100 ){

Echo "this is the result of executing the {$ num} output.
";

$ Num ++;

}

------------------------------------------------------------------------

While Example 2: // 100 rows, 10 columns, rows, color-changing tables

Echo'

This is a 1000 grid table. ';

Echo'

$ A = 0;

While ($ a <1000)

{

If ($ a % 20 = 0) {$ bg = '# fff111';} else {$ bg = '# ffff ';}

If ($ a % 10 = 0) {echo'

Echo'

$ A ++;

If ($ a % 10 = 0) {echo'

}

Echo'

'; ';} '; ';}
'. $ .'
';

?>

------------------------------------------------------------------------

For example: // you can write multiple expressions.

For ($ I = 0, $ j = 100; $ I <10 & $ j> 50; $ I ++, $ j-= 10 ){

Echo "this is the result of executing $ j in the first $ I loop.
";

}

------------------------------------------------------------------------

For loop instance // 99 multiplication table

For ($ I = 1; $ I <= 9; $ I ++ ){

For ($ j = 1; $ j <= $ I; $ j ++ ){

Echo "{$ j} * {$ I} =". $ I * $ j .'';

}

Echo'
';

}

?>

------------------------------------------------------------------------

Several loop-related statements

Break; // skip loop

Continue; // jumps out of this loop and does not affect other loops.

Exit;

Return;

------------------------------------------------------------------------

Example of break and continue

For ($ I = 0; I I <100; $ I ++ ){

If ($ I = 50)

Break;

Echo "$ I ###################
";

}

For ($ I = 0; I I <100; $ I ++ ){

If ($ I % 3 = 0)

Continue;

Echo "$ I ###################
";

}

?>

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.