Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// if/else, switch statement, and switch expression examples int temperature = 22; // Classic if/else if (temperature < 0) Console.WriteLine("Freezing"); else if (temperature < 15) Console.WriteLine("Cold"); else if (temperature < 25) Console.WriteLine("Comfortable"); else Console.WriteLine("Hot"); // Traditional switch statement DayOfWeek day = DayOfWeek.Wednesday; switch (day) { case DayOfWeek.Saturday: case DayOfWeek.Sunday: Console.WriteLine("Weekend"); break; default: Console.WriteLine("Weekday"); break; } // Switch expression (C# 8+) — returns a value string description = temperature switch { < 0 => "Freezing", >= 0 and < 15 => "Cold", >= 15 and < 25 => "Comfortable", _ => "Hot" }; Console.WriteLine(description); // Property pattern in switch expression var point = (X: 0, Y: 5); string quadrant = point switch { (0, 0) => "Origin", ( > 0, > 0) => "Q1", ( < 0, > 0) => "Q2", ( < 0, < 0) => "Q3", ( > 0, < 0) => "Q4", _ => "On axis" }; Console.WriteLine(quadrant); // "On axis"
Result
Open