Detailed description of Java for loop statements

Source: Internet
Author: User

This article briefly introduces how to use the for loop statement in java. If you need to solve the for statement, you can refer to it.

I. nested application of statements

Statement nesting form. In fact, there are still statements in the statement. There are various forms without fixed formats and routines.

1. Print even numbers

The Code is as follows: Copy code

For (int x = 1; x <= 10; x ++)
{If (x % 2 = 1)
Continue;
System. out. prinln ("x =" + x );
}

Ii. accumulate sum and counter

1. Obtain 1 ~ And print.
Idea: // 1. Define variables to store ever-changing sums.
Int sum = 0;
// 2. Define the variable and record the variable number.
Int x = 1;
// 3. Define the loop and repeat the addition process.
Use while to reflect

The Code is as follows: Copy code
While (x <= 10)
{
Sum = sum + x;
X ++;
}
System. out. println ("sum =" + sum );

Loop note:

Be sure to specify which statements need to participate in the loop and which do not need

Let's look at another instance.

The Code is as follows: Copy code

Class ForTest2
{
Public static void main (String [] args)
{
// Use.
Int sum = 0;
For (int x = 0; x <= 10; x ++)
{
Sum + = x;
}
System. out. println ("for sum =" + sum );
}
}

Package com. test. For_Each;

Import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;

Public class ForTest
{
Public static void main (String args [])
{
Int arr [] = {1, 2, 3, 4, 5, 6, 7, 8 };

/**
* New Writing Method
*/

For (int a: arr)
System. out. println ();

/**
* Old-style Writing
*/
For (int I = 0; I <arr. length; I ++)
System. out. println (arr [I]);


String arr2 [] = {"good", "stream", "Oh ","!! "};
For (String a2: arr2)
System. out. println (a2 );


Int arr3 [] [] = {, 3}, {, 6, },{, 9 }};
For (int a31 []: arr3)
{
For (int a32: a31)
{
System. out. println (a32 );
}
System. out. println ();
}


List <String> list = new ArrayList <String> ();
List. add ("good ");
List. add ("stream ");
List. add ("oh ");
List. add ("!! ");
/**
* Traversal based on the Collection class Length
*/
For (int I = 0; I <list. size (); I ++)
{
System. out. println (list. get (I ));
}


/**
* Traverse by iterator
*/
For (Iterator I = list. iterator (); I. hasNext ();)
{
System. out. println (I. next ());
}

/**
* Based on the New for-each Traversal
*/
For (String element: list)
System. out. println (element );
}
}

Compared with iterator, the set content cannot be conveniently deleted (in fact, iterator is also called internally)

In addition to simple traversal and reading of the content, we do not recommend using enhanced.


The program process is like this. First define two long variables limit and factoritl, and attach the initial values, then execute the for loop to clearly write the program format, you can clearly understand:

The Code is as follows: Copy code

For (int I = 1; I <= limit; I ++ ){
Factoritl = 1;
For (int factor = 2; factor <= I; factor ++ ){
Factoritl * = factor;
System. out. println (I + "I = is" + factoritl );
}
}

Outer for, first define a variable I with the initial value 1, then check if I is less than or equal to limit, then enter the loop body, execute factoritl = 1; then execute the next for loop, first, attach the initial value 2 to the factor, and then check whether the factor is less than or equal to I. If it is true, enter the inner layer for, execute factoritl * = factor; and print it. After the for internal program is executed, execute factor ++, and then check whether factor <= I is true. If it is true, continue executing the for internal program. If it is not true, exit the for loop body and continue executing the next line of code, in this example, after the internal for statement is launched, the comparison operation of the External Loop is continued, and then the internal for statement is executed until the program exits completely.

Related Article

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.