In Java, if statement is used for testing the conditions. The condition matches the statement it returns true else it returns false. There are four types of If statement they are: 在Java中,if语句用于测试条件。 条件与返回true的语句匹配,否则返回false。 有四种类型的If语句: For example, if we...
This is done using a simple if..else statement. We can also check for vowel or consonant using a switch statement in Java. Example 2: Check whether an alphabet is vowel or consonant using switch statement public class VowelConsonant { public static void main(String[] args) { char ch = ...
Note thattheelseblock is optional. We may write a statement as : if(condition){//statement-1} 2. The If-else Example Let’s see an example of anif-elsestatement. In the following program, we check whether the employee’s age is greater than 18. On both bases, we print that the em...
如果else语句的条件表达式为true,则执行else代码块;否则,跳过else代码块,继续执行if语句之后的代码。 下面是一个简单的Java示例,演示了if条件语句的执行流程: publicclassIfExample{publicstaticvoidmain(String[] args){inta =10;intb =20;if(a > b) { System.out.println("a大于b"); }else{ System.out.pr...
在Java 中,可以使用函数式编程进行优化: @Slf4j public class StrategyTest { public static void main(String[] args) { //Use simple example HashMap<String, Strategy> map = new HashMap<>(); map.put("man", () -> log.debug("Execute the logic related to men...")); map.put("woman",...
Now, if we run the program, the output will be: [1] "You are eligible to vote." Example: Check Negative and Positive Number x <- 12 # check if x is positive or negative number if (x > 0) { print("x is a positive number") } else { print("x is a negative number") } Out...
学过c语言的都知道,通常:If(0)之后的代码是不执行的,网上也有详细的说明。 1.1、形式: if (表达式) { 语句... } 1.2、解释: 在执行if语句时,首先会计算表达式的值,如果表达式的值为零,语句不会执行,若非零,则执行语句。由此可见if (0) 表示不执行,if (1)表示要执行。if (x)根据x的值是否为0来决...
inttime=22;if(time<10){System.out.println("Good morning.");}elseif(time<18){System.out.println("Good day.");}else{System.out.println("Good evening.");}// Outputs "Good evening." Try it Yourself » Example explained In the example above, time (22) is greater than 10, so thefi...
Java has the following conditional statements:Use if to specify a block of code to be executed, if a specified condition is true Use else to specify a block of code to be executed, if the same condition is false Use else if to specify a new condition to test, if the first condition ...
The if-then and if-then-else Statements Theif-thenStatement Theif-thenstatement is the most basic of all the control flow statements. It tells your program to execute a certain section of codeonly ifa particular test evaluates totrue. For example, theBicycleclass could allow the brakes to de...