C # if, if ... else, if ... else if and Nested if Instrukcja (z przykładami)

W tym artykule nauczymy się, jak używać instrukcji if, if… else, if… else if w języku C #, aby kontrolować przepływ wykonywania naszego programu.

Testowanie warunku jest nieuniknione w programowaniu. Będziemy często napotykają sytuacje, w których musimy warunkach testowych (czy jest truelub false) do sterowania przepływem programu. Na te warunki mogą mieć wpływ dane wejściowe użytkownika, czynnik czasu, bieżące środowisko, w którym program jest uruchomiony itp.

W tym artykule nauczymy się testować warunki przy użyciu instrukcji if w języku C #.

Instrukcja C # if (if-then)

Instrukcja C # if-then wykona blok kodu, jeśli podany warunek jest prawdziwy. Składnia instrukcji if-then w C # jest następująca:

 if (wyrażenie-boolowskie) (// instrukcje wykonywane, jeśli wyrażenie-boolowskie jest prawdziwe) 
  • Wyrażenie boolowskie zwróci wartość true lub false.
  • Jeśli wyrażenie logiczne zwróci true, instrukcje wewnątrz treści if (wewnątrz (… )) zostaną wykonane.
  • Jeśli wyrażenie logiczne zwróci false, instrukcje wewnątrz treści if zostaną zignorowane.

Na przykład,

 if (liczba <5) (liczba + = 5;) 

W tym przykładzie instrukcja

 liczba + = 5;

zostanie wykonany tylko wtedy, gdy wartość liczba jest mniejsza niż 5.

Pamiętasz operator + =?

Jak działa stwierdzenie?

Przykład 1: instrukcja C # if

 using System; namespace Conditional ( class IfStatement ( public static void Main(string() args) ( int number = 2; if (number < 5) ( Console.WriteLine("(0) is less than 5", number); ) Console.WriteLine("This statement is always executed."); ) ) ) 

Kiedy uruchomimy program, wynik będzie:

 2 jest mniejsze niż 5 Ta instrukcja jest zawsze wykonywana.

Wartość liczba jest inicjalizowana na 2. Zatem wyrażenie number < 5jest obliczane na true. W związku z tym wykonywany jest kod wewnątrz bloku if. Kod po instrukcji if będzie zawsze wykonywany niezależnie od wyrażenia.

Teraz zmień wartość liczby na większą niż 5, powiedzmy 10. Po uruchomieniu programu wynik będzie wyglądał następująco:

 Ta instrukcja jest zawsze wykonywana.

Wyrażenie number < 5zwróci false, stąd kod wewnątrz, jeśli blok nie zostanie wykonany.

Instrukcja C # if… else (if-then-else)

Instrukcja if w C # może mieć opcjonalną instrukcję else. Blok kodu wewnątrz instrukcji else zostanie wykonany, jeśli wartość wyrażenia ma wartość false.

Składnia instrukcji if… else w C # jest następująca:

 if (wyrażenie-boolowskie) (// instrukcje wykonywane, jeśli wyrażenie-boolowskie jest prawdziwe) else (// instrukcje wykonywane, jeśli wyrażenie-logiczne ma wartość fałsz) 

Na przykład,

 if (liczba <5) (liczba + = 5;) else (liczba - = 5;) 

W tym przykładzie instrukcja

 liczba + = 5;

zostanie wykonany tylko wtedy, gdy wartość liczba jest mniejsza niż 5.

Twierdzenie

 liczba - = 5;

zostanie wykonany, jeśli wartość liczby jest większa lub równa 5.

A jeśli… else Statement działa?

Przykład 2: C # if… else Instrukcja

 using System; namespace Conditional ( class IfElseStatement ( public static void Main(string() args) ( int number = 12; if (number < 5) ( Console.WriteLine("(0) is less than 5", number); ) else ( Console.WriteLine("(0) is greater than or equal to 5", number); ) Console.WriteLine("This statement is always executed."); ) ) ) 

Kiedy uruchomimy program, wynik będzie:

 12 jest większe lub równe 5 Ta instrukcja jest zawsze wykonywana.

Here, the value of number is initialized to 12. So the expression number < 5 is evaluated to false. Hence, the code inside the else block are executed. The code after the if… else statement will always be executed irrespective to the expression.

Now, change the value of number to something less than 5, say 2. When we run the program the output will be:

 2 is less than 5 This statement is always executed.

The expression number < 5 will return true, hence the code inside if block will be executed.

Ternary operator in C# provides a shortcut for C# if… else statement.

C# if… else if (if-then-else if) Statement

When we have only one condition to test, if-then and if-then-else statement works fine. But what if we have a multiple condition to test and execute one of the many block of code.

For such case, we can use if… else if statement in C#. The syntax for if… else if statement is:

 if (boolean-expression-1) ( // statements executed if boolean-expression-1 is true ) else if (boolean-expression-2) ( // statements executed if boolean-expression-2 is true ) else if (boolean-expression-3) ( // statements executed if boolean-expression-3 is true )… else ( // statements executed if all above expressions are false ) 

The if… else if statement is executed from the top to bottom. As soon as a test expression is true, the code inside of that if ( or else if ) block is executed. Then the control jumps out of the if… else if block.

If none of the expression is true, the code inside the else block is executed.

Alternatively, we can use switch statement in such condition.

Example 3: C# if… else if Statement

 using System; namespace Conditional ( class IfElseIfStatement ( public static void Main(string() args) ( int number = 12; if (number 5) ( Console.WriteLine("(0) is greater than 5", number); ) else ( Console.WriteLine("(0) is equal to 5"); ) ) ) ) 

When we run the program, the output will be:

 12 is greater than 5

The value of number is initialized to 12. The first test expression number < 5 is false, so the control will move to the else if block. The test expression number> 5 is true hence the block of code inside else if will be executed.

Similarly, we can change the value of number to alter the flow of execution.

Nested if… else Statement

An if… else statement can exist within another if… else statement. Such statements are called nested if… else statement.

The general structure of nested if… else statement is:

 if (wyrażenie-logiczne) (if (wyrażenie-zagnieżdżone-1) (// kod do wykonania) else (// kod do wykonania)) else (if (wyrażenie-zagnieżdżone-2) (// kod do wykonania ) else (// kod do wykonania)) 

Zagnieżdżone instrukcje if są zwykle używane, gdy musimy przetestować jeden warunek, po którym następuje inny. W zagnieżdżonej instrukcji if, jeśli zewnętrzna instrukcja if zwraca wartość true, wchodzi do treści, aby sprawdzić wewnętrzną instrukcję if.

Przykład 4: zagnieżdżona instrukcja if… else

Poniższy program oblicza największą liczbę spośród 3 liczb przy użyciu zagnieżdżonej instrukcji if… else.

 using System; namespace Conditional ( class Nested ( public static void Main(string() args) ( int first = 7, second = -23, third = 13; if (first> second) ( if (firstNumber> third) ( Console.WriteLine("(0) is the largest", first); ) else ( Console.WriteLine("(0) is the largest", third); ) ) else ( if (second> third) ( Console.WriteLine("(0) is the largest", second); ) else ( Console.WriteLine("(0) is the largest", third); ) ) ) ) ) 

Kiedy uruchomimy program, wynik będzie:

 13 jest największa

Interesujące artykuły...