Home  >  Article  >  Backend Development  >  Sample code sharing for for loop encountering return in php

Sample code sharing for for loop encountering return in php

黄舟
黄舟Original
2017-06-25 10:19:483026browse


First look at the printing results and return values ​​of the following methods:

public static void main(String[] args) {
		System.out.println("返回值:" + testResult());

	}
	
	public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			System.out.println("-------------->开始:" + i);
			if(i == 3) {
				return true;
			}
			System.out.println("-------------->结束:" + i);
		}
		return true;
		
	}

Printing results:

---------- ---->Start: 1

-------------->End: 1

---------- ---->Start: 2

-------------->End: 2

---------- ---->Start: 3

Return value: true, indicating that returning a value in for is equivalent to exiting the loop.

1) Suppose we reconstruct the testResult method and extract the logic in for into a separate method:

public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			test1(i);
		}
		return true;
		
	} 
	
	public static  void  test1(int i) throws NullPointerException{
		System.out.println("-------------->开始:" + i);
		if(i == 3) {
			return;
		}
		System.out.println("-------------->结束:" + i);
	}

Also placed in the main method. It’s just that the reconstruction method is directly called in the for loop of the testResult method, and the printed result is:

------------->Start: 1

------------->End: 1

-------------->Start: 2

------------->End: 2

-------------->Start: 3

------------->Start: 4

-------------->End: 4

------------->Start: 5

-------------->End: 5

Return value: true

This shows that the test1(i) method uses the return; statement to interrupt when it tries to reach i=3; but the loop is still completed.

2) You might as well give a return value to the method called in the for loop, as follows:

public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			return test2(i);
		}
		return true;
		
	} 

public static  boolean  test2(int i) throws NullPointerException{
		System.out.println("-------------->开始:" + i);
		if(i == 3) {
			return true;
		}
		System.out.println("-------------->结束:" + i);
		return false;
	}

The print result is as follows:

----- --------->Start: 1

------------->End: 1

Return value: false

This shows that calling a method with a boolean return value in for will cause the method to break before reaching i=3 and return a boolean value.

3) When you need to return a boolean value based on conditions in a for loop. If the code in the for loop needs to be restructured into a method, it should have a return value, but the return value cannot be boolean. We might as well use String instead, and use the returned String mark in the for loop to determine whether to exit the loop~ ~

The transformation is as follows:

public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			String flag =  test3(i);
			if("yes".equals(flag)) {
				return true;
			}
		}
		return true;
		
	} 

public static  String  test3(int i) throws NullPointerException{
		System.out.println("-------------->开始:" + i);
		if(i == 3) {
			return "yes";
		}
		System.out.println("-------------->结束:" + i);
		return "no";
	}

Print result:

-------------->Start: 1

------------->End: 1

------------->Start: 2

------------->End: 2

------------->Start: 3

Return value: true

It means that the effect of the original code in the for loop was not refactored~

The above small The example is a summary of my experience of errors when refactoring similar code, because in the actual code, the code in for is repeated several times, but also because the code in for needs to return a boolean value based on the judgment condition. During the reconstruction process, I first changed it to test1(i), then changed it to test2(i), and finally changed it to test3(i) to achieve the effect before reconstruction.

The above is the detailed content of Sample code sharing for for loop encountering return in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn