The difference between enhanced for loop and ordinary for loop

Summary of the differences between enhanced for loop and ordinary for loop:

1. The enhanced for loop is simple to write and does not require subscripts when traversing arrays. It is mainly used for traversing arrays or collections. The loop will end execution when the array or collection is traversed.

2. An ordinary for loop requires three conditions, including loop variables, loop end conditions and changes in loop variables. When traversing data, if you need to use subscripts, you can use an ordinary for loop.
For example : when receiving array elements from the keyboard, you are prompted to enter the first element. If you use the enhanced for loop, you need to define additional variables.

Scanner sc = new Scanner(System.in);
		int[] arr = new int[10];
		for(int i=0;i<arr.length;i++) {
    
    
			System.out.println("请输入第"+(i+1)+"个数据:");
		}

Guess you like

Origin blog.csdn.net/Turniper/article/details/114188902