黑马程序员技术交流社区

标题: 逻辑运算符笔记,睡前分享一下 [打印本页]

作者: 温暖的小白    时间: 2015-3-10 22:23
标题: 逻辑运算符笔记,睡前分享一下
逻辑运算符:
    与(&):两边只要有一个是false,结果就为false,只有两边都为true,结果才为true;
        true & true=true;
        true& false=false;
        false& true=false;
        false& false=false;
        
    或(|):两边只要有一个是true,结果就为true,只有两边都是false,结果才为false;
        true| true=true;
        true| false=true;
        false| true=true;
        false| false=false;
|
    异或(^):就是和|有点不一样,相同为假,不同为真;
        true^ true=false;
        true ^ false=true;
        false^ true=true;
        false^ false=false;
    &&:and(短路),false && true=false;
        示例:
            inta=4;
            a>3& a<6;//结果为true & true =true;
            a>3&& a<6;//第二个表达式也运算,结果为true;
            inta=2;            
            a>3&& a<6;// 结果为false,第二个表达式不运算(不管第二个表达式的结     果是true还是false,结果都为false);
    ||:or(短路),true && false =true;
        示例:
            inta=4;
            a>3| a<6;//结果为true | true =true;
            a>3|| a<6;//第二个表达式不运算,结果为true;
            a>3|| a<2;//第二个表达式不运算,结果为true;
            a<3|| a<6;//第二个表达式运算,结果为true;
    总结:
        &和&&的特点:
            &:无论左边是true是false,右边都运算;
            &&:当左边为false是,右边不运算(当左边为true时,右边运算)。
        |和||的特点:
            |:无论左边是true是false,右边都运算;
            ||:当左边为true是,右边不运算(当左边为false时,右边运算)。

作者: 521123270    时间: 2015-3-12 19:25
:handshake




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2