个性化阅读
专注于IT技术分析

Java for循环语句

本文概要

在编程语言,循环使用时,一些条件成为真正的重复执行一组指令/功能。有三种类型Java中的循环。

  • for循环
  • while循环
  • do-while循环

Java的For循环VS While循环VS do while循环

对照for循环while循环做while循环
IntroductionThe Java for loop is a control flow statement that iterates a part of the programs multiple times. Java的while循环是控制流语句给出布尔条件的基础上,反复执行程序的一部分。Java do while循环是一个控制流语句,它至少执行程序的一部分,进一步的执行取决于给定的布尔条件。
When to use如果迭代的数量是固定的,所以建议使用循环。If the number of iteration is not fixed,it is recommended to use while loop.如果迭代次数不固定,建议使用while循环。
句法
for(init;condition;incr/decr){  
// code to be executed 
}
while(condition){  
//code to be executed 
}
做{ //要执行的代码 }while(条件);
Example
//for loop  
for(int i=1;i<=10;i++){  
System.out.println(i);  
}  
// while循环 INT I = 1; while(ⅰ<= 10){ 的System.out.println(ⅰ); 我++; }
//do-while loop  
int i=1;  
do{  
System.out.println(i);  
i++;  
}while(i<=10); 
Syntax for infinitive loop对于(;;){ //要执行的代码 }
while(true){  
//code to be executed  
}
do{  
//code to be executed  
}while(true);  

Java的for循环用于迭代程序数次的一部分。如果迭代的数量是固定的,所以建议使用循环。

有在Java中环的三个类型。

  • 简单的for循环
  • 对于-每个或增强的for循环
  • 标记For循环

Java的简单的for循环

for循环简单相同的C / C ++。我们可以初始化变量,检查条件和递增/递减值。它由四个部分组成:

  1. 初始化:这是被一次时在循环开始执行的初始条件。在这里,我们可以初始化变量,或者我们可以使用一个已经初始化的变量。这是一个可选的条件。
  2. 条件:它是每次测试循环的条件执行的第二条件。直到条件为假时继续执行。它必须返回布尔值true或false。这是一个可选的条件。
  3. 声明:循环的语句,直到第二个条件是假的,每次执行。
  4. 递增/递减:它递增或递减变量值。这是一个可选的条件。

句法:

for(initialization;condition;incr/decr){
//statement or code to be executed
}

流程图:

例:

//Java Program to demonstrate the example of for loop
//which prints table of 1
public class ForExample {
public static void main(String[] args) {
    //Code of Java for loop
	for(int i=1;i<=10;i++){
		System.out.println(i);
	}
}
}

输出:

1
2
3
4
5
6
7
8
9
10

Java的嵌套For循环

如果我们有一个对另一个循环内循环,嵌套的for循环它是已知的。内循环执行完全每当外循环的执行。

例:

public class NestedForExample {
public static void main(String[] args) {
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(int j=1;j<=3;j++){
		System.out.println(i+" "+j);
}//end of i
}//end of j
}
}

输出:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

金字塔实施例1:

public class PyramidExample {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
		System.out.print("* ");
}
System.out.println();//new line
}
}
}

输出:

* 
* * 
* * * 
* * * * 
* * * * *

金字塔实施例2:

public class PyramidExample2 {
public static void main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){
for(int j=term;j>=i;j--){
		System.out.print("* ");
}
System.out.println();//new line
}
}
}

输出:

* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*

的Java for-each循环

的for-each循环是用来在Java横动阵列或集合。这是很容易,使用简单的循环,因为我们不需要增加值和使用标符号。

它的工作原理要素基础不是指数。它通过一个在所定义的变量返回元件之一。

句法:

for(Type var:array){
//code to be executed
}

例:

//Java For-each loop example which prints the
//elements of the array
public class ForEachExample {
public static void main(String[] args) {
    //Declaring an array
	int arr[]={12,23,44,56,78};
	//Printing array using for-each loop
	for(int i:arr){
		System.out.println(i);
	}
}
}

输出:

12
23
44
56
78

Java的标记For循环

我们可以有每个Java的for循环的名称。要做到这一点,我们使用标签之前的for循环。如果我们有嵌套的for循环,这样就可以打/继续特定的for循环是非常有用的。

通常情况下,突破并继续关键字休息/持续仅环路最里面。

句法:

labelname:
for(initialization;condition;incr/decr){
//code to be executed
}

例:

//A Java program to demonstrate the use of labeled for loop
public class LabeledForExample {
public static void main(String[] args) {
    //Using Label for outer and for loop
	aa:
		for(int i=1;i<=3;i++){
			bb:
				for(int j=1;j<=3;j++){
					if(i==2&&j==2){
						break aa;
					}
					System.out.println(i+" "+j);
				}
		}
}
}

输出:

1 1
1 2
1 3
2 1

如果你使用破BB ;,这将打破只有内环这是任何循环的默认行为。

public class LabeledForExample2 {
public static void main(String[] args) {
	aa:
		for(int i=1;i<=3;i++){
			bb:
				for(int j=1;j<=3;j++){
					if(i==2&&j==2){
						break bb;
					}
					System.out.println(i+" "+j);
				}
		}
}
}

输出:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java的不定式For循环

如果使用两个分号;;在for循环,这将是不定式的循环。

句法:

for(;;){
//code to be executed
}

例:

//Java program to demonstrate the use of infinite for loop
//which prints an statement
public class ForExample {
public static void main(String[] args) {
    //Using no condition in for loop
	for(;;){
		System.out.println("infinitive loop");
	}
}
}

输出:

infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c

现在,你需要按CTRL + C来退出程序。

赞(0)
未经允许不得转载:srcmini » Java for循环语句

评论 抢沙发

评论前必须登录!