Conditionals
if, else if, and else work the
same as in Java or C++.
Example: a time-of-day greeting
using System;
class Program {
static void Main() {
int hour = 14;
string greeting;
if (hour < 12) {
greeting = "Good morning";
} else if (hour < 18) {
greeting = "Good afternoon";
} else {
greeting = "Good evening";
}
Console.WriteLine(greeting);
}
}
Good afternoon
By now, if you've seen this same example in the Java or C++ sections of this tutorial, the pattern should feel entirely familiar — that repetition is deliberate: seeing the identical logic expressed in three closely related languages is one of the fastest ways to internalize which parts are "genuinely how branching works" versus "just this language's particular syntax."
using System;
class Program {
static void Main() {
int hour = 14;
string greeting;
if (hour < 12) {
greeting = "Good morning";
} else if (hour < 18) {
greeting = "Good afternoon";
} else {
greeting = "Good evening";
}
Console.WriteLine(greeting);
}
}
Good afternoon