strengthening for loops

Source: Internet
Author: User
The For loop is open-bound. Its general form is: for (< initialization >; < conditional expressions >; < increments >) statements; Initialization is always an assignment statement, which is used to assign an initial value to a loop control variable; A conditional expression is a relational expression that determines when to exit a loop; Increment defines how the loop control variable changes after each loop. Between these three sections with ";" Separate. For example: for (i=1; i<=10; i++) statements; In the above example, "I" is assigned an initial value of 1, to determine whether "I" is less than or equal to 10, if the execution of the statement, followed by an increase of 1. Then re-judge until the condition is false, i.e. i>10, the end loop.

Directory

Some points to be aware of
strengthening for loops
For loops in PHP
Expand
Edit this paragraph
Some points to be aware of

1. The statement in the For loop can be a statement body, but the statements that participate in the loop are enclosed in "{" and "}".
2. The "Initialize", "conditional expression", and "increment" in the For loop are all selections, which can be defaulted, but ";" Cannot default. Initialization is omitted to indicate that the loop control variable is not assigned an initial value. If a conditional expression is omitted, it becomes a dead loop when no other processing is done. If an increment is omitted, the loop control variable is not manipulated, and a statement that modifies the loop control variable can be added to the body of the statement.
3. For loops can have multiple layers of nesting.
Cases:
#include <stdio.h>
int main (void)
{
int I, j, K;
printf ("I j k\n");
For (i=0, i<2; i++) for (j=0; j<2; j + +) for (k=0; k<2; k++)
printf (%d%d%d\n ", I, J, K);
return 0;
}
The output is: I j k 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
Edit this paragraph
strengthening for loops

J2SE 1.5 provides another form of a for loop. With this form of for loop, you can iterate over objects of types such as arrays and collection in a simpler way. This article describes how to use this loop in a specific way, explaining how to define a class that can be traversed like this, and explain some common issues with this mechanism.
In a Java program, "one-by-one"-or "traverse"-an element in an array or collection, is typically implemented with a for loop (of course, it is not possible to use other kinds of loops, but it is not known because the length of the word for is shorter, Or because the meaning of the word for is compared with this kind of operation, at this time the for loop is much more common than other loops.
For iterating over an array, this loop is generally written like this:
Listing 1: The traditional way to iterate through an array
/* Create an array */
int[] integers = {1, 2, 3, 4};
/* Start traversal */
for (int j = 0; J < Integers.length; J + +) {
int i = integers[j];
System.out.println (i);
}
For the traversal of the collection object, this loop is usually taken in this form:
Listing 2: The traditional way to traverse a collection object
/* Build a collection */
String[] strings = {"A", "B", "C", "D"};
Collection stringlist = java.util.Arrays.asList (strings);
/* Start traversal */
for (Iterator ITR = Stringlist.iterator (); Itr.hasnext ();) {
Object str = Itr.next ();
System.out.println (str);
}
In the latest version of the Java language,--j2se 1.5, another form of a for loop has been introduced. With this form of a for loop, it is now possible to traverse the work in a simpler way.
1. Second for loop
Not strictly speaking, the second type of Java for loop is basically this format:
for (loop variable type loop variable name: object to be traversed) loop body
With this syntax, an operation that iterates through an array can be written like this:
Listing 3: An easy way to iterate through an array
/* Create an array */
int[] integers = {1, 2, 3, 4};
/* Start traversal */
for (int i:integers) {
System.out.println (i);/* output "1", "2", "3", "4" in turn */
}
The For loop used here is considered to be such a form during compilation:
Listing 4: Equivalent code for an easy way to iterate through an array
/* Create an array */
int[] integers = {1, 2, 3, 4};
/* Start traversal */
for (int variable name = 0; variable name a < integers.length; variable name + +) {
SYSTEM.OUT.PRINTLN (variable name a);/* output "1", "2", "3", "4"/*
}
The "variable name" is a name that is generated automatically by the compiler and does not cause confusion.
This can be done by traversing a collection operation:
Listing 5: A simple way to traverse collection
/* Build a collection */
String[] strings = {"A", "B", "C", "D"};
Collection list = java.util.Arrays.asList (strings);
/* Start traversal */
for (Object str:list) {
System.out.println (str);/* output "A", "B", "C", "D" in turn */
}
The For loop used here is considered to be the form during compilation: Listing 6: Equivalent code for a simple way to traverse collection/* Build a collection */
String[] strings = {"A", "B", "C", "D"};
Collection stringlist = java.util.Arrays.asList (strings);
/* Start traversal */
For (Iterator variable name b = List.iterator (); variable name B. Hasnext ();) {
SYSTEM.OUT.PRINTLN (variable name B. Next ());/* output "A", "B", "C", "D" in turn */
}
The "variable name B" Here is also a name that is automatically generated by the compiler and does not cause confusion.
Because during compilation, the J2SE 1.5 compiler will consider this form of a for loop as the corresponding traditional form, so there is no need to worry about performance issues.
Edit this paragraph
For loops in PHP

<?php
The For loop in PHP is basically consistent with the C language, as follows:
for ($i =1; $i <=100; $i + +) {
Echo ' This is the first '. $i ' Times ';
}
With a For loop, a simple array can be looped out
$array =array (' 0 ' = ' a0 ', ' 1 ' = ' = ' B1 ', ' 2 ' = ' C2 ', ' d ' = ' D3 ');
for ($i =0; $i <=count ($array); $i + +) {
echo $array [$i];
}
It is important to note that for arrays that do not have a number, a Foreach loop is recommended to iterate through the array.
?>
  • 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.