AD

The for loop Xiangjie php

This is a very primary topic. However, if we really have understood and properly used it is beneficial for life. So, today special share to the website (javaeye**) of you beginners.

for loop syntax is:
for ($ exp1; $ exp2; $ exp3) (loop body algorithm code)
Of which:
$ Exp1 is initialized
$ Exp2 is terminating
$ Exp3 is increasing or decreasing the rules, the three expressions are separated by a comma can be used to specify the number of cycle parameters.

1, a simple loop example
for ($ i = 0; $ i <100; $ i + +) (loop body algorithm code)
for ($ i = 3; $ i <100; $ i + = 2) (loop body algorithm code)

2, when the cycle parameters have been initialized
for (; $ i <100; $ i + +) (loop body algorithm code)
for (; $ i <100; $ i + = 2) (loop body algorithm code)

3, conditions, sample conditions initialization initialize the benefits that can speed up the process of speed detection function to remove the conditions or complex expressions.
for ($ i = 0, $ j <count ($ arr); $ i <$ j; $ i + +) (loop body algorithm code)
Principle: initialization run only once, condition and increment expression is in each cycle must be executed. Therefore, this treatment, the speed certainly accelerated.

4, multi-cycle
for ($ i = 0, $ j = 10; $ i <= 10; $ i + +, $ j -) (loop body algorithm code)
Note: The above condition is only one condition.
for ($ i = 0, $ j = 10; $ i <= 10 & & $ j> 5; $ i + +, $ j -) (loop body algorithm code)
Note: The above conditions for multiple conditions. Once the condition is false, it will terminate the cycle.

5, increasing or decreasing the complex calculation of the cumulative results of 1-5, Bin value to $ j
for ($ i = 1, $ j = 0; $ i <= 5; $ j + = $ i + +);
echo $ j;
1-5 factorial calculation results, value to $ j Bin
for ($ i = 1, $ j = 1; $ i <= 5; $ j *= $ i + +);
echo $ j;
Note: The combination of multi-cycle variable expression can reduce the code to write nested for loop. On the other hand, it will reduce the code readability.

6, infinite loop
for (;;){
if ($ expression)
break; / / infinite loop within the conditions must be terminated.
)

7, further flexibility can be further combined portfolio, according to the actual needs of the program, such as:
for (;; $ j *= $ i + +) (/ / no initialization, unconditional, and only incremental
if ($ expression)
break; / / infinite loop within the conditions must be terminated.
)

8, no cycle parameters
$ Array = array ("value1", "value2", "value3");
for (reset ($ array); current ($ array); next ($ array)) (/ / Here, the three expressions are implemented using the function. And this method, the actual realization of exactly the same with the foreach function.
echo ("Element". key ($ array). "contains". current ($ array). "<br/>");
)
So, do not have to cycle parameters, as long as the realization of a true iterative, for loop can function properly. Looking back, we look for loop syntax:
for ($ exp1; $ exp2; $ exp3) (loop body algorithm code)
Of which:
$ Exp1 is initialized
$ Exp2 is terminating
$ Exp3 is increasing or decreasing the rules

Can be seen, the above program so they can run, because it is consistent with the syntax.
标签: lt, amp, principle, gt 5, increment, parameters, comma, code 3, expressions, loop body, loop syntax, variable expression, algorithm code, speed detection, cumulative results
分类: PHP
时间: 2010-08-28

相关文章

  1. Routing table Xiangjie

    Routing table shows (Xiangjie route print) Read nello routing table, to get this piece of paper, a more detail ...
  2. mod_rewrite Xiangjie (Reprinted)

    mod_rewrite Xiangjie 2004-04-23 15:18 pm From: Linux documents are shown: Www.8s8s.coM Address: unknown Apache ...
  3. Oracle statement optimization rules Xiangjie 53

    Oracle statement optimization rules Xiangjie 53 1. The types of ORACLE optimizer ORACLE optimizer There are 3 ...
  4. MySQL triggers Xiangjie

    First-come, instance, do the following Xiangjie delimiter $ $ create trigger orderitem_ident_bi before insert ...
  5. Utility Script Xiangjie: a touch scrolling information bar stop

    Utility Script Xiangjie: a touch scrolling information bar stop In our unit on the first floor hall to erect a ...
  6. Code Optimization 2 - ready to work outside in the loop

    Recently in IC rotation, look at a lot of code, found in many places need to optimize the following section of ...
  7. Session Xiangjie (change)

    Session Xiangjie (change) Abstract: Although the session mechanism in the web application has been adopted for ...
  8. OpenSessionInView Xiangjie

    OpenSessionInView Xiangjie http://www.javaeye.com/topic/32001
  9. Java's Enhanced for loop and variable parameters

    Syntax: for (type variable name: a collection of variable names) (...) Note: Iteration variable must be in () ...
  10. java multi-threading Xiangjie

    1: Understanding   multi-threading multi-threading is such a mechanism, which allows the program concurrentl ...
  11. hibernate configuration Xiangjie

    1, hibernate.cfg.xml configuration Xiangjie Hibernate can be a description of the file properties properties f ...
  12. Foreach loop: ArrayList, Primitive data, HashMap

    import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class ForEachLoop { public ...
  13. dos batch command Xiangjie

    1. A simple internal command batch Introduction 1.Echo Command Open the Echo Request Echo feature on or off, o ...
  14. JDK tool for java command Xiangjie

    JDK tool for java command Xiangjie SCJP compulsory content, very useful, even if you are using a IDE. 1, view ...
  15. JDK tools javac command Xiangjie

    JDK tools javac command Xiangjie SCJP tests of the content, understanding of this right to master IDE developm ...
  16. BlackBerry blackberry error code system error Xiangjie

    101 Previous startup failed When the jvm to start the process, before the start of a project failed, equipment ...
  17. Map of the several ways to loop through

    The existing data: Map<String,String> map=new HashMap<String,String>(); map.put("username&quo ...
  18. Java for the operation of Xiangjie Cookie

    Java for the operation of Xiangjie Cookie Original Long Dok 2009-08-06 This article give you details about the ...
  19. JAVA-IO Xiangjie

    JAVA-IO Xiangjie 1. Input and Output 1. Stream represents the ability to output data in any data source, or an ...