capypad
0 day streak
csharp / beginner
Snippet

Conditional Logic with If-Else

The 'if' statement allows your program to make decisions. If the condition in parentheses is true, the first block of code runs; otherwise, the 'else' block runs.

snippet.csharp
csharp
1
2
3
4
5
6
7
int score = 85;
 
if (score >= 50) {
Console.WriteLine("You passed!");
} else {
Console.WriteLine("Try again.");
}
Breakdown
1
if (score >= 50)
Checks if the variable 'score' is greater than or equal to 50.
2
Console.WriteLine("You passed!");
Prints a success message if the condition is met.
3
else { ... }
Provides an alternative action if the 'if' condition is false.