Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen If-Else-Kette | Kontrollstrukturen
Quizzes & Challenges
Quizzes
Challenges
/
C# Grundlagen

bookIf-Else-Kette

We can add additional conditions using the else if keyword. The additional conditions are evaluated in case the previous conditions are not met.

if (expression) 
{
    // code if first condition is met
}
else if (expression)
{
    // code if second condition is met
} else 
{
    // code if no condition is met
}


Let's consider an example with if else chain:

main.cs

main.cs

copy
1234567891011121314151617181920212223242526
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value_1 = 9; int value_2 = 7; if(value_1 < value_2) { Console.WriteLine("Value 1 is smaller than Value 2"); } else if(value_1 > value_2) { Console.WriteLine("Value 1 is bigger than Value 2"); } else if(value_1 == value_2) { Console.WriteLine("Value 1 is equal to Value 2"); } } } }

In the above program we chained conditions using if-else if. This is called conditional chaining. The first condition value_1 < value_2 is evaluated. Since it is false, the program skips to the next condition value_1 > value_2 which is true and hence it executes its code block and stops executing the chain further.

The main feature of conditional chaining is that it stops executing the chain as soon as a condition is met.

Consider the following code:

main.cs

main.cs

copy
12345678910111213141516171819202122232425
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value = 10; if(value > 5) { Console.WriteLine("Value is bigger than 5"); } else if(value > 7) { Console.WriteLine("Value is bigger than 7"); } else if(value > 9) { Console.WriteLine("Value is bigger than 9"); } } } }

Even though all three conditions are true, it stops executing on the first condition since it's a chain.

Now let's try writing it using simple if keywords without chaining:

main.cs

main.cs

copy
123456789101112131415161718192021222324252627
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value = 10; if(value > 5) { Console.WriteLine("Value is bigger than 5"); } if(value > 7) { Console.WriteLine("Value is bigger than 7"); } if(value > 9) { Console.WriteLine("Value is bigger than 9"); } } } }

In the above case, each condition is evaluated individually and not treated as a part of any chain hence all three statements are outputted.

We can also add the else keyword at the end of the if-else chain which will execute if no condition is matched:

main.cs

main.cs

copy
1234567891011121314151617181920212223242526
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value_1 = 9; int value_2 = 7; if(value_1 < value_2) { Console.WriteLine("Value 1 is smaller than Value 2"); } else if(value_1 > value_2) { Console.WriteLine("Value 1 is bigger than Value 2"); } else { Console.WriteLine("Value 1 is equal to Value 2"); } } } }
question mark

Which lines will be included in the output of the following program?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 9

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain the difference between if-else chaining and separate if statements?

Can you give more examples of conditional chaining in JavaScript?

What happens if none of the conditions in the if-else chain are true?

bookIf-Else-Kette

Swipe um das Menü anzuzeigen

We can add additional conditions using the else if keyword. The additional conditions are evaluated in case the previous conditions are not met.

if (expression) 
{
    // code if first condition is met
}
else if (expression)
{
    // code if second condition is met
} else 
{
    // code if no condition is met
}


Let's consider an example with if else chain:

main.cs

main.cs

copy
1234567891011121314151617181920212223242526
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value_1 = 9; int value_2 = 7; if(value_1 < value_2) { Console.WriteLine("Value 1 is smaller than Value 2"); } else if(value_1 > value_2) { Console.WriteLine("Value 1 is bigger than Value 2"); } else if(value_1 == value_2) { Console.WriteLine("Value 1 is equal to Value 2"); } } } }

In the above program we chained conditions using if-else if. This is called conditional chaining. The first condition value_1 < value_2 is evaluated. Since it is false, the program skips to the next condition value_1 > value_2 which is true and hence it executes its code block and stops executing the chain further.

The main feature of conditional chaining is that it stops executing the chain as soon as a condition is met.

Consider the following code:

main.cs

main.cs

copy
12345678910111213141516171819202122232425
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value = 10; if(value > 5) { Console.WriteLine("Value is bigger than 5"); } else if(value > 7) { Console.WriteLine("Value is bigger than 7"); } else if(value > 9) { Console.WriteLine("Value is bigger than 9"); } } } }

Even though all three conditions are true, it stops executing on the first condition since it's a chain.

Now let's try writing it using simple if keywords without chaining:

main.cs

main.cs

copy
123456789101112131415161718192021222324252627
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value = 10; if(value > 5) { Console.WriteLine("Value is bigger than 5"); } if(value > 7) { Console.WriteLine("Value is bigger than 7"); } if(value > 9) { Console.WriteLine("Value is bigger than 9"); } } } }

In the above case, each condition is evaluated individually and not treated as a part of any chain hence all three statements are outputted.

We can also add the else keyword at the end of the if-else chain which will execute if no condition is matched:

main.cs

main.cs

copy
1234567891011121314151617181920212223242526
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int value_1 = 9; int value_2 = 7; if(value_1 < value_2) { Console.WriteLine("Value 1 is smaller than Value 2"); } else if(value_1 > value_2) { Console.WriteLine("Value 1 is bigger than Value 2"); } else { Console.WriteLine("Value 1 is equal to Value 2"); } } } }
question mark

Which lines will be included in the output of the following program?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 9
some-alt