作者:CHAITANYA SINGH

来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=18

当我们需要根据一个条件执行一组语句时,我们需要使用控制流语句。例如,如果一个数字大于零,那么我们想要打印“正数”,但是如果它小于零,那么我们要打印“负数”。在这种情况下,程序中有两个print语句,但根据对输入值的条件比较结果,每次只执行一个print语句。我们将看到如何使用控制语句在java程序中编写这种类型的条件。

下面我们将学习如何根据需求在java程序中使用四种类型的控制语句。在本教程中,我们将介绍以下条件语句:if语句、嵌套if语句、if-else语句、if-else-if语句

1)if语句

if语句包含一个条件,后面是语句或一组语句,如下所示:

1
2
3
if(condition){
  Statement(s);
}

这些语句只有在给定条件判断为true(真)时才会执行。如果条件为false(假),则if语句正文中的语句将被完全忽略。

if语句示例

1
2
3
4
5
6
7
8
9
10
11
12
public class IfStatementExample {
 
   public static void main(String args[]){
      int num=70;
      if( num < 100 ){
      /* This println statement will only execute,
       * if the above condition is true
       */
      System.out.println("number is less than 100");
      }
   }
}

输出:

1
number is less than 100

2)Java中的嵌套if语句

当一个if语句嵌套在另一个if语句中时,这个if语句就被称为嵌套if语句.

嵌套if的结构如下所示:

1
2
3
4
5
6
7
if(condition_1) {
   Statement1(s);
 
   if(condition_2) {
      Statement2(s);
   }
}

如果condition_1(条件1)为true(真),则Statement1语句将会执行。执行完了Statement1语句后,程序走到下一个语句if(condition_2),如果condition_2(条件2)的值为true(真),则Statement2语句就会执行,反之,程序会跳过Statement2(s)语句,继续执行后面的语句。由此可见,只有在condition_1和condition_2都为true(真)的情况下,语句Statement2才会执行。

嵌套if语句示例

1
2
3
4
5
6
7
8
9
10
11
12
13
public class NestedIfExample {
 
   public static void main(String args[]){
     int num=70;
     if(num < 100){ 
       System.out.println("number is less than 100"); 
 
       if(num > 50){
         System.out.println("number is greater than 50");
       }
     }
   }
}

输出:

1
2
number is less than 100
number is greater than 50

3)Java中的if-else语句

if-else语句结构看上去是这样的:

1
2
3
4
5
6
if(condition) {
   Statement(s);
}
else {
   Statement(s);
}

如果if后面的condition(条件)为true(真),则“if”后面的大括号{  }中的语句将执行,如果if后面的condition(条件)为false(假),则“else”后面的大括号{  }中的语句将执行。

if-else语句示例

1
2
3
4
5
6
7
8
9
10
11
12
public class IfElseExample {
 
   public static void main(String args[]){
     int num=120;
     if( num < 50 ){
        System.out.println("num is less than 50");
     }
     else{
        System.out.println("num is greater than or equal 50");
     }
   }
}

输出:

1
num is greater than or equal 50

4)if-else-if 语句

当我们需要检查多个条件时,使用if-else-if语句。在上面的声明语句中,我们只有一个“if”和一个“else”,但是我们可以有多个“else if”,也就是梯状的if语句,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if(condition_1) {
   /*if condition_1 is true execute this*/
   statement(s);
}
else if(condition_2) {
   /* execute this if condition_1 is not met and
    * condition_2 is met
    */
   statement(s);
}
else if(condition_3) {
   /* execute this if condition_1 & condition_2 are
    * not met and condition_3 is met
    */
   statement(s);
}
.
.
.
else {
   /* if none of the condition is true
    * then these statements gets executed
    */
   statement(s);
}

注:这里需要注意的最重要一点是,在“if-else-if”语句中,一旦一个“else if”后面的条件满足了,这个条件版块里面的语句集就会被执行,其余“else if”条件版块里面的语句就都会被忽略。如果所有的else if后面的条件都不满足,则执行最后面的“other”版块中的语句。

if-else-if实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class IfElseIfExample {
 
   public static void main(String args[]){
    int num=1234;
    if(num <100 && num>=1) {
      System.out.println("Its a two digit number");
    }
    else if(num <1000 && num>=100) {
      System.out.println("Its a three digit number");
    }
    else if(num <10000 && num>=1000) {
      System.out.println("Its a four digit number");
    }
    else if(num <100000 && num>=10000) {
      System.out.println("Its a five digit number");         
    }
    else {
      System.out.println("number is not between 1 & 99999");           
    }
   }
}

输出:

1
Its a four digit number

Java中的if-else语句——通过示例学习Java编程(7)的更多相关文章

  1. Java中的continue语句——通过示例学习Java编程(12)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=23 continue语句主要是用在循环代码块中.当 ...

  2. Java中的switch语句——通过示例学习Java编程(8)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=19 当我们在代码逻辑中有多个选项,而且需要为每个选 ...

  3. 【转】Java中try catch finally语句中含有return语句的执行情况(总结版)

    Java中try catch finally语句中含有return语句的执行情况(总结版) 有一点可以肯定,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有r ...

  4. java中的Switch case语句

    java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...

  5. Java中的大小写字母相互转换(不利用Java自带的方法)

    Java中的大小写字母相互转换(不利用Java自带的方法) 1.设计源码 /** * * @title:UpperAndLower.java * @Package:com.you.utils * @D ...

  6. Java基础学习(五)-- Java中常用的工具类、枚举、Java中的单例模式之详解

    Java中的常用类 1.Math : 位于java.lang包中 (1)Math.PI:返回一个最接近圆周率的 (2)Math.abs(-10):返回一个数的绝对值 (3)Math.cbrt(27): ...

  7. Java中使用最频繁及最通用的Java工具类

    在Java中,工具类定义了一组公共方法,Java中使用最频繁及最通用的Java工具类. 一. org.apache.commons.io.IOUtils closeQuietly:关闭一个IO流.so ...

  8. Java中的do-while循环——通过示例学习Java编程(11)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=22&cid=0 在上一篇教程中,我们讨论了w ...

  9. Java中的for循环——通过示例学习Java编程(9)

      作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=21 循环用于反复执行同一组语句,直到满足特定条件 ...

随机推荐

  1. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  2. Windows下Tesseract4.0识别与中文手写字体训练

    一 . tesseract 4.0 安装及使用 1. tesseract 4.0 安装 安装包下载地址: http://digi.bib.uni-mannheim.de/tesseract/tesse ...

  3. Codeforces Round #535(div 3) 简要题解

    Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...

  4. [ZJU 2112] Dynamic Rankings

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1901 [算法] 首先 , 考虑没有修改操作 不妨建立可持久化线段树 , 第i棵树维护 ...

  5. 洛谷P1522牛的旅行——floyd

    题目:https://www.luogu.org/problemnew/show/P1522 懒于仔细分情况而直接像题解那样写floyd然后不明白最后一步max的含义了... 分开考虑怎么保证在一个内 ...

  6. 使用svnsync实时备份SVN版本库

    前段时间把SVN版本库从win迁移到了Linux上,没隔几天那台win的磁盘就严重坏道了....这TMD什么运气! 花费了点时间研究了下svn自己的同步工具.写个日志记录下. 注意:svnsync要求 ...

  7. bzoj2117

    动态电分治+二分 肯定要枚举所有点对,那么我们建出点分树降低树高,然后每个点存下点分树中所有子树到这个点的距离,然后二分+lower_bound就行了. #include<bits/stdc++ ...

  8. PHP正则匹配中文汉字注意

    preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}]+$/u', $str) 如上,是匹配字母或者汉字的,一定要在后面加模式修饰符 u , 不然就出错! u (PCRE_UT ...

  9. PHP开发api接口 -- 安全验证 生成签名

    转载博客 ————. http://blog.csdn.net/li741350149/article/details/62887524 REST模式中HTTP请求方法(GET,POST,PUT,DE ...

  10. mina框架之---服务端NioSocketAcceptor的学习

    接上一讲对mina的简单应用和对mina服务端和客户端中几个重要的技术知识点的理解后,今天着重对mina服务端的NioSocketAcceptor 进行学习. 说这个玩意之前,先整体上看一下在mina ...