• 技术文章 >PHP >PHP数组

    php中if…elseif…else判断条件

    小妮浅浅小妮浅浅2021-01-27 16:31:58原创5832

    我们可以选择简单的语句判断,也可以对多个条件进行同时的筛选。在php中除了switch可以判断两个以上的条件外,if…elseif…else语句也同样可以实现。认为switch语句难度大的,不妨来学习一些这个语句。下面我们就php中if…elseif…else的概念、语法带来讲解,最后分享成绩的实例判断。

    1.概念

    if…elseif…else语句根据两个以上的条件执行不同的代码。

    2.语法

    1

    2

    3

    4

    5

    6

    7

    if (条件) {

      条件为 true 时执行的代码;

    } elseif (condition) {

      条件为 true 时执行的代码;

    } else {

      条件为 false 时执行的代码;

    }

    3.实例

    判断分数对应的等级。

    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

    26

    27

    28

    29

    30

    31

    32

    33

    /* 编写一个程序,按照下列分数段指定相应的等级,例如:

     * 90~100         A级        

     * 80~89          B级        

     * 70~79          C级        

     * 60~69          D级        

     * 0~59           E级        

     * 其他                              不合法

     * 使用if…else if…else语句实现判断95分对应的等级。

     */

      

    public class Classify{

    public static void main(String[] args) {

    int score=95;

    if(score>=90&&score<=100) {

    System.out.println("A级");

    }

    else if(score>=80&&score<=89) {

    System.out.println("B级");

    }

    else if(score>=70&&score<=79) {

    System.out.println("C级");

    }

    else if(score>=60&&score<=69) {

    System.out.println("D级");

    }

    else if(score>=0&&score<=59) {

    System.out.println("E级");

    }

    else {

    System.out.println("不合法");

    }

    }

    }

    以上就是php中if…elseif…else判断条件的方法,相信大家对于这种if类型的条件语句,使用起来还是比较顺手的。学会后可以多使用,加强对语句的实际运用。更多php学习指路:php数组

    专题推荐:php if…elseif…else
    上一篇:php switch语句的使用 下一篇:php有几种流程控制语句

    相关文章推荐

    • php中sort函数是什么• php中rsort函数是什么• php asort函数的使用• php定义常量数组有几种方式• php中条件语句的使用整理• php中ksort()函数是什么• php switch语句的使用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网