Lesson 4 of 15

Conditionals

Conditionals

C# uses if, else if, and else for branching:

int score = 85;

if (score >= 90) {
    Console.WriteLine("A");
} else if (score >= 80) {
    Console.WriteLine("B");
} else if (score >= 70) {
    Console.WriteLine("C");
} else {
    Console.WriteLine("F");
}
// Output: B

Ternary Operator

A compact one-liner for simple conditions:

int x = 10;
string result = x > 0 ? "positive" : "non-positive";

Your Task

Write a static method Sign(int n) that returns:

  • "positive" if n > 0
  • "negative" if n < 0
  • "zero" if n == 0
WasmSharp (.NET) loading...
Loading...
Click "Run" to execute your code.