Use the if statement to specify a block of C# code to be executed if a condition is True.
Syntax
if (condition)
{
// block of code to be executed if the condition is True
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
Let's have a code below
using System;
class Program
{
static void Main()
{
int number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}
Explanation:
Condition: The condition inside the if statement is evaluated. If it returns true, the code block inside the if is executed.
Else if: This is optional and can be used to check multiple conditions. If the if condition is false, the else if condition is checked.
Else: If all previous conditions are false, the code block inside the else is executed.
You can also nest if-else statements within each other to check multiple conditions:
using System;
class Program
{
static void Main()
{
int number = 10;
int anotherNumber = 5;
if (number > 0)
{
if (anotherNumber > 0)
{
Console.WriteLine("Both numbers are positive.");
}
else
{
Console.WriteLine("The first number is positive, but the second is not.");
}
}
else
{
Console.WriteLine("The first number is not positive.");
}
}
}
Use the if statement to specify a block of C# code to be executed if a condition is True.
Syntax
if (condition) { // block of code to be executed if the condition is True }
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
Let's have a code below
using System; class Program { static void Main() { int number = 10; if (number > 0) { Console.WriteLine("The number is positive."); } else if (number < 0) { Console.WriteLine("The number is negative."); } else { Console.WriteLine("The number is zero."); } } }
Explanation:
Condition: The condition inside the if statement is evaluated. If it returns true, the code block inside the if is executed.
Else if: This is optional and can be used to check multiple conditions. If the if condition is false, the else if condition is checked.
Else: If all previous conditions are false, the code block inside the else is executed.
You can also nest if-else statements within each other to check multiple conditions:
using System; class Program { static void Main() { int number = 10; int anotherNumber = 5; if (number > 0) { if (anotherNumber > 0) { Console.WriteLine("Both numbers are positive."); } else { Console.WriteLine("The first number is positive, but the second is not."); } } else { Console.WriteLine("The first number is not positive."); } } }
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved