if / else
Try it yourself →Branching based on conditions.
Syntax
if (condition) { ... } else { ... }Description
The condition is written in parentheses; if true, the first block runs, otherwise the else block runs. Intermediate conditions use else if. Curly braces are not mandatory for a single line.
Example
using System;
int age = 20;
if (age >= 18) {
Console.WriteLine("Adult");
} else {
Console.WriteLine("Child");
}Expected output
Adult
When to use?
Decision making, input control.