博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
if_Keyword
阅读量:3926 次
发布时间:2019-05-23

本文共 5576 字,大约阅读时间需要 18 分钟。

Java if,if else,nested if, if else if Statement with Examples

In this quick article, we will learn Java if statement and different types of if statement in Java, which is used to test the condition. It checks boolean condition: true or false.

There are various types of if statement in java.

  • if Statement
  • nested if Statement
  • if-else Statement
  • if-else-if Statement
  • Java if Statement
    The Java if statement tests the condition. It executes the if block if a condition is true.

Syntax

if(condition){  //code to be executed  }

Here, if statement may be a single statement or a compound statement enclosed in curly braces (that is, a block).

The statements get executed only when the given condition is true. If the condition is false then the statements inside if statement body is completely ignored.

Java if Statement Example

package net.javaguides.corejava.controlstatements.ifelse;public class IfStatementExample {    public static void main(String[] args) {        int x, y;        x = 10;        y = 20;        if (x < y) {            System.out.println("x is less than y");        }        x = x * 2;        if (x == y) {            System.out.println("x now equal to y");        }        x = x * 2;        if (x > y) {            System.out.println("x now greater than y");        }        // this won't display anything        if (x == y)            System.out.println("you won't see this");    }}

Output:

x is less than yx now equal to yx now greater than yJava Nested if Statement

When there is an if statement inside another if statement then it is called the nested if statement.

Syntex

if(condition_1) {   Statement1(s);   if(condition_2) {      Statement2(s);   }}

Java Nested if Statement example

package net.javaguides.corejava.controlstatements.ifelse;public class NetstedIfStatementExample {    public static void main(String[] args) {        int num = 50;        if (num < 100) {            System.out.println("number is less than 100");            if (num == 50) {                System.out.println("number equal to 50");                if (num > 40) {                    System.out.println("number is greater than 40");                }            }        }    }}

Output:

number is less than 100number equal to 50number is greater than 40Java if-else Statement

The Java if-else statement also tests the condition. It executes the if block if a condition is true otherwise else block, is executed.

Syntex

if(condition){       statement 1; //code if condition is true  }else{       statement 2; //code if condition is false  }

Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is an expression that returns a boolean value.

The if works like this: If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is executed.

Java if-else Statement Example

The following program, IfElseDemo, assigns a grade based on the value of a test score: an A for a score of 90% or above, a B for a score of 80% or above, and so on.

package net.javaguides.corejava.controlstatements.ifelse;public class IfElseDemo {    public static void main(String[] args) {        int testscore = 76;        char grade;        if (testscore >= 90) {            grade = 'A';        } else if (testscore >= 80) {            grade = 'B';        } else if (testscore >= 70) {            grade = 'C';        } else if (testscore >= 60) {            grade = 'D';        } else {            grade = 'F';        }        System.out.println("Grade = " + grade);    }}

Output:

Grade = CNote that the value of test score can satisfy more than one expression in the compound statement: 76 >= 70 and 76 >= 60. However, once a condition is satisfied, the appropriate statements are executed (grade = 'C';) and the remaining conditions are not evaluated.Java if-else-if Statement

A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder.

Syntex

if(condition1){  //code to be executed if condition1 is true  }else if(condition2){  //code to be executed if condition2 is true  }  else if(condition3){  //code to be executed if condition3 is true  }  ...  else{  //code to be executed if all the conditions are false  }

The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed.

If none of the conditions is true, then the final else statement will be executed. The final else acts as a default condition; that is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place.

Java if-else-if Statement Example

Here is a program that uses an if-else-if ladder to determine which season a particular month is in.

package net.javaguides.corejava.controlstatements.ifelse;public class IfElseIfStatementExample {    public static void main(String args[]) {        int month = 4; // April        String season;        if (month == 12 || month == 1 || month == 2)            season = "Winter";        else if (month == 3 || month == 4 || month == 5)            season = "Spring";        else if (month == 6 || month == 7 || month == 8)            season = "Summer";        else if (month == 9 || month == 10 || month == 11)            season = "Autumn";        else            season = "Bogus Month";        System.out.println("April is in the " + season + ".");    }}

Output:

April is in the Spring.```s

转载地址:http://abgrn.baihongyu.com/

你可能感兴趣的文章
题目26 孪生素数问题
查看>>
java web 连接mysql数据库
查看>>
java 多线程简介
查看>>
docker架构
查看>>
Docker Client创建与命令执行
查看>>
springMVC学习笔记
查看>>
PageRank算法与特征向量和特征值(eigenvector和eigenvalue)
查看>>
HITS算法--从原理到实现
查看>>
MapReduce原理
查看>>
zookeeper原理
查看>>
MapReduce入门
查看>>
WEB服务器、应用程序服务器、HTTP服务器区别
查看>>
小白入门:大型网站技术架构负载均衡技术
查看>>
归并排序(JAVA)
查看>>
对Java Serializable(序列化)的理解和总结
查看>>
Netty Buffer(缓冲)
查看>>
Docker简单介绍
查看>>
.ftl文件 是什么文件
查看>>
数据结构与算法--栈、队列(队列)
查看>>
动态规划
查看>>