C#
 Computer >> コンピューター >  >> プログラミング >> C#

|の違いは何ですかおよび|| C#の演算子?


|| 論理ORと呼ばれます 演算子と| ビットごとの論理ORと呼ばれます しかし、それらの基本的な違いは、実行方法にあります。 ||の構文および|次と同じ-

  • bool_exp1 || bool_exp2
  • bool_exp1 | bool_exp2
  • 1と2の構文は互いに似ていますが、実行方法がまったく異なります。
  • 最初のステートメントでは、最初にbool_exp1が実行され、次にこの式の結果によって他のステートメントの実行が決定されます。
  • trueの場合、ORはtrueになるため、otherステートメントを実行しても意味がありません。
  • bool_exp2ステートメントは、bool_exp1が実行時にfalseを返した場合にのみ実行されます。
  • 最初の式の結果に基づいて回路(ステートメント)を短絡するため、短絡演算子とも呼ばれます。
  • 今|の場合物事は異なります。コンパイラは両方のステートメントを実行します。つまり、1つのステートメントの結果に関係なく、両方のステートメントが実行されます。
  • ORの結果は「false」と評価された結果に対してのみ有効であり、両方のステートメントがfalseの場合に可能であるため、一方がtrueの場合、もう一方のステートメントを実行しても意味がないため、これは非効率的な方法です。

論理OR

using System;
namespace DemoApplication{
   public class Program{
      static void Main(string[] args){
         if(Condition1() || Condition2()){
            Console.WriteLine("Logical OR If Condition Executed");
         }
         Console.ReadLine();
      }
      static bool Condition1(){
         Console.WriteLine("Condition 1 executed");
         return true;
      }
      static bool Condition2(){
         Console.WriteLine("Condition 2 executed");
         return true;
      }
   }
}

出力

Condition 1 executed
Logical OR If Condition Executed

ビットごとの論理OR

using System;
namespace DemoApplication{
   public class Program{
      static void Main(string[] args){
         if(Condition1() | Condition2()){
            Console.WriteLine("Logical OR If Condition Executed");
         }
         Console.ReadLine();
      }
      static bool Condition1(){
         Console.WriteLine("Condition 1 executed");
         return true;
      }
      static bool Condition2(){
         Console.WriteLine("Condition 2 executed");
         return true;
      }
   }
}

出力

Condition 1 executed
Condition 2 executed
Logical OR If Condition Executed

  1. Pythonの!=演算子と<>演算子の違いは何ですか?

    Python 2.xでは、!=演算子と<>演算子の両方を使用して、2つのオペランドが等しくないかどうかを確認できます。どちらも、オペランドが等しくない場合はtrueを返し、等しくない場合はfalseを返します。 Python 3.xでは、<>演算子は非推奨になりました。

  2. Pythonの=演算子と==演算子の違いは何ですか?

    Pythonでは、シンボルは割り当てとして定義されます オペレーター。左側に1つの変数、右側に式が必要です。右側の式の値は、左側の変数に割り当てられます。変数の式と名前は互換性がありません。 >>> a=10 >>> b=20 >>> c=a+b >>> a,b,c (10, 20, 30) >>> a+b=c SyntaxError: can't assign to operator ==記号は比較演算子であり、等しいと呼ばれます オペレーター。いずれかの側のオペランドが等しい場合はtrueを