Home > Java > JavaBase > body text

How to jump out or terminate if statement in java

王林
Release: 2019-11-20 17:04:04
Original
19605 people have browsed it

How to jump out or terminate if statement in java

1. break

break: Break out of the current loop; but if it is a nested loop, you can only break out of the current layer of loops, and only break layer by layer can Break out of all loops.

for (int i = 0; i < 10; i++) {  
            if (i == 6) { 
                break;  
               // 在执行i==6时强制终止循环,i==6不会被执行  
                }
            System.out.println(i);  
        }  
  
  
输出结果为0 1 2 3 4 5 ;6以后的都不会输出
Copy after login

2. continue

continue: Terminate the current loop, but do not jump out of the loop (the statements after continue in the loop will not be executed), and continue to execute the loop according to the loop conditions.

for (int i = 0; i < 10; i++) {  
    if (i == 6)  {
        continue;  
      // i==6不会被执行,而是被中断了    
       } 
       System.out.println(i);  
   }
   
   输出结果为0 1 2 3 4 5 7 8 9;只有6没有输出
Copy after login

3. return

(1)return Exits from the current method, returns to the statement of the called method, and continues execution.

(2)return Returns a value to the statement that calls the method. The data type of the return value must be consistent with the type of the return value in the method declaration.

(3) Return can also be followed by no parameters. Without parameters, it returns empty. In fact, the main purpose is to interrupt the execution of the function and return to the calling function.

Special note: Methods with a return value of void must use return to jump out from a certain judgment.

Recommended tutorial: java quick start

The above is the detailed content of How to jump out or terminate if statement in java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template