zoukankan      html  css  js  c++  java
  • 使用&&和||取代&和|的原因

    如果使用&或者|,则在if判断括号中的第一个条件为0的时候它依旧会继续去判断第二个条件,但是如果第二个条件中的除数为 0 的时候就会让程序出现错误,抛出run-time异常。

    Console.Write("Enter a dividend: ");
    var dividend = Convert.ToInt32(Console.ReadLine());
    
    Console.Write("Enter a divisor: ");
    var divisor = Convert.ToInt32(Console.ReadLine());
    
    // If the divisor is 0, the second clause in the following condition
    // causes a run-time error. The && operator short circuits when the
    // first expression is false. That is, it does not evaluate the
    // second expression. The & operator evaluates both, and causes
    // a run-time error when divisor is 0.
    if ((divisor != 0) && (dividend / divisor > 0))
    {
        Console.WriteLine("Quotient: {0}", dividend / divisor);
    }
    else
    {
        Console.WriteLine("Attempted division by 0 ends up here.");
    }
    
  • 相关阅读:
    JDBC事务处理
    JDBC之LOB数据类型
    使用JDBC驱动程序处理元数据
    JDBC之PreparedStatement
    JDBC主要API学习总结
    JDBC简介
    ForkJoinPool 分支/合并框架
    线程池
    线程八锁
    jQuery中的事件冒泡
  • 原文地址:https://www.cnblogs.com/ezhar/p/13468412.html
Copyright © 2011-2022 走看看