C# Keywords Tutorial Part 85: true

C# Keywords Tutorial Part 85: true

Popular programming language C# is frequently used to create many kinds of software applications. Support for Boolean values, which may either be true or false, is one of C#’s fundamental features. The “true” keyword is a core component used to denote the Boolean value true in the C# language. The “true” keyword in C# will be examined and discussed in detail in this blog article.

What is the “true” keyword in C#?

The Boolean literal “true” is a keyword in C# that stands for the value true. It serves as a sign that a certain assumption or assertion is accurate. One of two Boolean literals in C#, the other being “false,” which stands for the value false, is the term “true.”

Using the “true” keyword in C#

The “true” keyword can be used in various contexts in C#, depending on the type of statement being evaluated. Here are some common examples of how the “true” keyword can be used in C#:

  1. If Statements

In an “if” statement, the “true” keyword is used to evaluate a condition. If the condition is true, the code within the “if” block is executed. Otherwise, the code within the “else” block is executed. Here’s an example:

int a = 5;

if (a == 5)
{
    Console.WriteLine("a is equal to 5");
}
else
{
    Console.WriteLine("a is not equal to 5");
}

In this example, the “if” statement checks whether the value of “a” is equal to 5. Since the value of “a” is indeed 5, the code within the “if” block is executed and the message “a is equal to 5” is displayed.

  1. While Loops

In a “while” loop, the “true” keyword is often used to create an infinite loop. An infinite loop is a loop that runs indefinitely until a “break” statement is encountered. Here’s an example:

int i = 0;

while (true)
{
    Console.WriteLine(i);
    i++;
}

In this example, the “while” loop runs indefinitely because the condition “true” is always true. The loop continues to execute until a “break” statement is encountered. This type of loop is often used in games and other real-time applications where continuous processing is required.

  1. Boolean Expressions

In a Boolean expression, the “true” keyword is used to represent the value true. Here’s an example:

bool isSunny = true;
bool isRaining = false;

if (isSunny && !isRaining)
{
    Console.WriteLine("It's a beautiful day!");
}
else
{
    Console.WriteLine("It's not a beautiful day.");
}

In this example, the “if” statement checks whether it’s sunny and not raining. Since both conditions are true, the message “It’s a beautiful day!” is displayed.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *