Conditional Logic

Conditional Logic

Conditional logic is a fundamental aspect of programming that allows you to control the flow of your code based on certain conditions. In C#, you can use if statements, switch statements, and ternary operators to implement conditional logic.

– If Statements

An if statement allows you to execute a block of code if a certain condition is true. Here’s the basic syntax of an if statement in C#:

if (condition)

{

    // code to execute if condition is true

}

Here’s an example of using an if statement in C#:

int num = 10;

if (num > 0)

{

    Console.WriteLine(“num is positive”);

}

In this example, the code inside the if block will only execute if the condition (num > 0) is true.

You can also use else and else if clauses to create more complex conditional logic. Here’s an example:

int num = 10;

if (num > 0)

{

    Console.WriteLine(“num is positive”);

}

else if (num < 0)

{

    Console.WriteLine(“num is negative”);

}

else

{

    Console.WriteLine(“num is zero”);

}

In this example, if the first condition (num > 0) is false, the second condition (num < 0) will be checked. If that condition is also false, the else block will execute.

– Switch Statements

A switch statement is used to evaluate a variable or expression against a list of possible values and execute a corresponding block of code. Here’s the basic syntax of a switch statement in C#:

switch (variable)

{

    case value1:

        // code to execute if variable == value1

        break;

    case value2:

        // code to execute if variable == value2

        break;

    default:

        // code to execute if variable doesn’t match any of the cases

        break;

}

Here’s an example of using a switch statement in C#:

int num = 2;

switch (num)

{

    case 1:

        Console.WriteLine(“num is 1”);

        break;

    case 2:

        Console.WriteLine(“num is 2”);

        break;

    case 3:

        Console.WriteLine(“num is 3”);

        break;

    default:

        Console.WriteLine(“num is not 1, 2, or 3”);

        break;

}

In this example, the code inside the case 2 block will execute because the value of “num” is 2.

– Ternary Operators

A ternary operator is a shorthand way of writing an if-else statement in a single line of code. Here’s the basic syntax of a ternary operator in C#:

variable = (condition) ? value1 : value2;

Here’s an example of using a ternary operator in C#:

int num = 10;

string message = (num > 0) ? “num is positive” : “num is not positive”;

Console.WriteLine(message);

In this example, the value of “message” will be “num is positive” because the condition (num > 0) is true.

Conditional logic is a powerful tool that allows you to create complex programs that can adapt to different situations. By using if statements, switch statements, and ternary operators, you can write code that can make decisions based on specific conditions and respond accordingly.

In many cases, conditional logic will decide which action to perform based on user input or external conditions.

All conditional logic starts with a condition: a simple expression that can be evaluated to true or false. Your code can then make a decision to execute different logic depending on the outcome of the condition. To build a condition, you can use any combination of literal values or variables along with logical operators. Below lists the basic logical operators.

OperatorDescription
==Equals to.
!=Not Equals to.
<Less than.
>Greater than.
<=Less than or equal to.
>=Greater than or equal to.
&&If the logical (and) operator is used then both expression must be true to evaluate the condition.
||If the logical (or) operator is used then one expression must be true to evaluate the condition.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Variable Operations
The if Statement

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?