Java中的决策(if, if-else, switch, break, continue, jump)
编程中的决策与现实生活中的决策类似。在编程中也会面临一些情况,我们希望在某些条件满足时执行某个代码块。
编程语言使用控制语句来控制基于某些条件的程序的执行流程。这些语句被用来根据程序状态的变化使执行流前进和分支。
Java的选择语句
- if
- if-else
- nested-if
- if-else-if
- switch-case
- jump – break, continue, return
1.if: if语句是最简单的决策语句。它用于决定某个语句或语句块是否被执行,即如果某个条件为真,则执行某个语句块,否则不执行。
语法
if(condition)
{
// Statements to execute if
// condition is true
}
在这里,评估后的 条件 将是真或假。if语句接受布尔值–如果值为真,那么它将执行它下面的语句块。
如果我们没有在 if(条件) 后面提供大括号'{‘和’}’,那么默认情况下,if语句将认为眼前的一个语句在其块内。比如说。
if(condition) //Assume condition is true
statement1; //part of if block
statement2; // separate from if block
// Here if the condition is true
// if block will consider statement1 as its part and executes in only true condition
// statement2 will be separate from the if block so it will always executes whether the condition is true or false.
例子
// Java program to illustrate If statement without curly block
import java.util.*;
class IfDemo {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("Inside If block"); // part of if block(immediate one statement after if condition)
System.out.println("10 is less than 15"); //always executes as it is outside of if block
// This statement will be executed
// as if considers one statement by default again below statement is outside of if block
System.out.println("I am Not in if");
}
}
输出
Inside If block
10 is less than 15
I am Not in if
时间复杂度: O(1)
辅助空间: O(1)
2.if-else : 单独的if语句告诉我们,如果一个条件为真,它将执行一个语句块,如果条件为假,它将不执行。但是,如果我们想在条件为假的情况下做其他事情,该怎么办?这里就有else语句。我们可以将else语句与if语句一起使用,以便在条件为假时执行一个代码块。
语法:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
例子
// Java program to illustrate if-else statement
import java.util.*;
class IfElseDemo {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
输出
i is smaller than 15
时间复杂度: O(1)
辅助空间: O(1)
3.嵌套if: 嵌套if是指一个if语句是另一个if或else的目标。嵌套的if语句意味着一个if语句在一个if语句里面。是的,java允许我们在if语句中嵌套if语句。也就是说,我们可以把一个if语句放在另一个if语句中。
语法
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
例子
// Java program to illustrate nested-if statement
import java.util.*;
class NestedIfDemo {
public static void main(String args[])
{
int i = 10;
if (i == 10 || i<15) {
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println(
"i is smaller than 12 too");
} else{
System.out.println("i is greater than 15");
}
}
}
输出
i is smaller than 15
i is smaller than 12 too
时间复杂度: O(1)
辅助空间: O(1)
4.if-else-if阶梯: 在这里,用户可以在多个选项中做出决定。if语句从上往下执行。只要控制if的条件之一为真,与该 “if “相关的语句就会被执行,而梯子的其他部分则被绕过。如果没有一个条件是真的,那么最后的else语句将被执行。一个 “if “块可以有尽可能多的 “else if “块,但一个 “if “块只允许有一个 “else “块。
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
例子
// Java program to illustrate if-else-if ladder
import java.util.*;
class ifelseifDemo {
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
输出
i is 20
时间复杂度: O(1)
辅助空间: O(1)
5.switch-case: switch语句是一个多路分支语句。它提供了一种简单的方法,根据表达式的值将执行调度到代码的不同部分。
语法
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
- 表达式可以是byte、short、int char或枚举的类型。从JDK7开始, 表达式 也可以是String类型的。
- 不允许有重复的大小写值。
- 默认语句是可选的。
- break语句是在switch内部使用的,用于终止一个语句序列。
- break语句是必要的,如果没有break关键字,switch块中的语句就会落空。
- 如果省略break关键字,执行将继续到下一个案例。
6.跳转: Java支持三种跳转语句: break、continue 和 return。 这三个语句将控制权转移到程序的另一部分。
- break: 在Java中,断裂主要用于。
- 终止switch语句中的一个序列(上面讨论过)。
- 退出一个循环。
- 作为goto的一种 “文明 “形式使用。
- continue: 有时,强制一个循环的早期迭代是很有用的。也就是说,你可能想继续运行这个循环,但在这个特定的迭代中停止处理其主体中的剩余代码。这实际上是一个刚过循环主体的Goto,到循环的终点。continue语句就执行了这样的动作。
例子
// Java program to illustrate using
// continue in an if statement
import java.util.*;
class ContinueDemo {
public static void main(String args[])
{
for (int i = 0; i < 10; i++) {
// If the number is even
// skip and continue
if (i % 2 == 0)
continue;
// If number is odd, print it
System.out.print(i + " ");
}
}
}
输出
1 3 5 7 9
时间复杂度: O(1)
辅助空间: O(1)
- return: return语句是用来明确地从一个方法中返回。也就是说,它使程序控制权转移回方法的调用者。
例子
// Java program to illustrate using return
import java.util.*;
public class Return {
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");
if (t)
return;
// Compiler will bypass every statement
// after return
System.out.println("This won't execute.");
}
}
输出
Before the return.
时间复杂度: O(1)
辅助空间: O(1)