Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 4 / 68

Conditionals (if statements)

In this lesson you will learn how to write if statements in C# so your program can branch based on a condition. This is for anyone searching for "C# if statement syntax".

if / else if / else handles conditional branching. The syntax is similar to JavaScript and Java. It helps to picture it as translating an everyday decision such as "if it is raining, take an umbrella" into code.

The sample code prints one of three messages depending on the value of score, using if / else if / else. Comparisons use operators such as >, <, and ==, and stacking more else if clauses naturally expresses three or more branches.

A common stumbling block for beginners is losing track of which {} pairs with which condition. As the number of conditions grows this gets hard to follow, so keeping your indentation consistent matters.

In real-world development, people coming from other languages settle in quickly here, because the braces and comparison operators are almost identical across the C family.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)