English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# bietet viele Entscheidungsanweisungen, die den Programmablauf eines C#-Programms basierend auf bestimmten logischen Bedingungen helfen können. Hier werden Sie die if, else if, else und die eingebetteten if else-Befehle kennenlernen, um den Ablauf basierend auf Bedingungen zu steuern.
C# enthält die folgenden Arten von if-Befehlen:
if-Befehl
else-if-Befehl
else statement
Der if-Befehl enthält eine Boolean-Bedingung, gefolgt von einem einzigen oder mehreren Zeilen umfassenden Codeblock, der ausgeführt werden soll. Wenn die Boolean-Bedingung bei der Laufzeit auf true bewertet wird, wird der Codeblock ausgeführt, andernfalls nicht.
if(condition) { //Codeblock, der bei wahrer Bedingung des if-Befehls ausgeführt wird }
int i = 10, j = 20; if (i < j) { Console.WriteLine("i ist kleiner als j"); } if (i > j) { Console.WriteLine("i ist größer als j"); }
i ist kleiner als j
Im obigen Beispiel ist die Boolean-Bedingung im ersten if-Befehl i < j wahr, daher führt der C#-Compiler den folgenden Codeblock aus. Die Bedingung im zweiten if-Befehl i > j wird auf false bewertet, daher führt der Compiler den Codeblock nicht aus.
Der bedingte Ausdruck muss einen Boolean-Wert zurückgeben, andernfalls gibt der C#-Compiler bei der Kompilierung einen Fehler aus.
int i = 10, j = 20; if (i + 1) { Console.WriteLine("i is less than j"); } if (i + j) { Console.WriteLine("i is greater than j"); }
You can call a function that returns a boolean value in the if statement.
static void Main(string[] args) { int i = 10, j = 20; if (isGreater(i, j)) { Console.WriteLine("i is less than j"); } if (isGreater(j, i)) { Console.WriteLine("j is greater than i"); } } static bool isGreater(int i, int j) { return i > j; }
Multiple else if statements can be used after the if statement. It will only be executed when the calculation result of the if condition is false. Therefore, one of the if or else if statements can be executed, but not both at the same time.
if(condition1) { //If condition1code block to be executed when evaluated as true } else if(condition2) { // code block executed when // condition1is calculated as flase // condition2is calculated as true } else if(condition3) { // code block executed when // condition1is calculated as flase // condition2is calculated as false // condition3is calculated as true }
The following example demonstrates the else if statement.
int i = 10, j = 20; if (i == j) { Console.WriteLine("i ist gleich j"); } else if (i > j) { Console.WriteLine("i ist größer als j"); } else if (i < j) { Console.WriteLine("i ist kleiner als j"); }
i ist kleiner als j
The else statement can only appear after an if or else if statement and can only appear in an if-The else statement is used once in the else statement. The else statement cannot contain any conditions and will be executed when all the calculation results of the preceding if and else if conditions are false.
int i = 20, j = 20; if (i > j) { Console.WriteLine("i is greater than j"); } else if (i < j) { Console.WriteLine("i is less than j"); } else { Console.WriteLine("i equals j"); }
i equals j
C supports another if-if in the else statement-else statement. This is called nested if-else statement. Nested if statements make the code more readable.
if(condition1) { if(condition2) { // code block executed when // condition1and condition2is calculated as true } else if(condition3) { if(condition4) { // code block executed when // only condition1,condition3and condition4The calculation result is true } else if(condition5) { // code block executed when // only condition1,condition3and condition5The calculation result is true } else { // code block executed when // condition1and condition3is evaluated as true // condition4and condition5The calculation result is false } } }
Das folgende Beispiel zeigt eingebettete if-else-Anweisungen.
int i = 10, j = 20; if (i != j) { if (i < j) { Console.WriteLine("i ist kleiner als j"); } else if (i > j) { Console.WriteLine("i ist größer als j"); } } else { Console.WriteLine("i ist gleich j"); }
i ist kleiner als j
Verwenden Sie den Drei-Wege-Operator ? : anstelle einfacher if-else-Anweisungen.