if / else
In this page:
if / else
An if statement runs a block only when its condition is true; an optional else runs when it's false. else if chains let you test multiple conditions in order, stopping at the first match. Braces are optional for single statements but recommended for clarity.
Note: Chain conditions with else if instead of nesting separate if blocks.
Example: if / else
using System;
class Program
{
static void Main(string[] args)
{
int score = 72;
if (score >= 90)
Console.WriteLine("A");
else if (score >= 70)
Console.WriteLine("B");
else
Console.WriteLine("C");
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: