Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 演算の順序 | 制御構造
C#の基礎

book演算の順序

メニューを表示するにはスワイプしてください

1つの演算子が複数回使用されている場合、式は左から右に評価されます。

例として、簡単のために true および false リテラルを使用します。たとえば、false || false || true || false のような長い式がある場合、この式は左側から評価されます:

main.cs

main.cs

copy
123456789101112
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine(false || false || true || false); // Output: True } } }

論理演算子の優先順位は次のとおりです:

以下は、複数の異なる演算子を含む、より複雑な例です。コードリーディングの練習として、読み解いて理解してみてください。

main.cs

main.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int x = 50; // We can store results of boolean / logical expressions in boolean variables or constants. bool inRange = (1 <= x) && (x <= 10) || (90 <= x) && (x <= 100); Console.WriteLine($"The value {x} is in the range 1-10 or 90-100: {inRange}"); // Output: False x = 99; inRange = (1 <= x) && (x <= 10) || (90 <= x) && (x <= 100); Console.WriteLine($"The value {x} is in the range 1-10 or 90-100: {inRange}"); // Output: True } } }

上記のコードでは、(1 <= x) && (x <= 10) || (90 <= x) && (x <= 100) という式があり、x が 1~10 または 90~100 の範囲にあるかどうかを判定しています。

演算子の優先順位を考慮すると、式は次のように評価されます。

question-icon

演算子の実行順序として正しいものはどれですか?

-> ->

クリックまたはドラッグ`n`ドロップして空欄を埋めてください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  4

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  4
some-alt