Complete C# Tutorial

C# Logical Operator Example – Learn Logical Conditions in Code

In C#, logical operators are used to combine multiple conditions. These operators help you decide whether certain conditions are true or false. You’ll often use them in if statements to check multiple conditions at once.

There are three main logical operators:

  1. && (AND): Returns true if both conditions are true.
  2. || (OR): Returns true if at least one condition is true.
  3. ! (NOT): Reverses the result of a condition. If the condition is true, it returns false, and vice versa.

 

Real-World Scenario: Online Store Discount Check

Let’s say you’re building a shopping cart system for an online store. A customer should get a discount if they meet two conditions:

  1. They need to be a loyal customer (a returning customer).
  2. They should have purchased at least $50 worth of items.

You can use logical operators to check both of these conditions at the same time. If both are true, they get the discount.

 

C# Code Example:

				
					using System;

class DiscountChecker
{
    static void Main()
    {
        // Input: Customer status and purchase amount
        Console.Write("Are you a loyal customer? (true/false): ");
        bool isLoyalCustomer = Convert.ToBoolean(Console.ReadLine());

        Console.Write("Enter your total purchase amount: $");
        double purchaseAmount = Convert.ToDouble(Console.ReadLine());

        // Logical operators used to check the conditions
        if (isLoyalCustomer && purchaseAmount >= 50)
        {
            Console.WriteLine("You qualify for a discount!");
        }
        else
        {
            Console.WriteLine("Sorry, you don't qualify for a discount.");
        }
    }
}
				
			

Explanation of the Code

  1. User Input: The program asks if the customer is a loyal customer (true/false) and their total purchase amount.
  2. Logical AND (&&): We use the && operator to check if both conditions are true. The customer has to be loyal and have spent at least $50.
  3. Decision: If both conditions are true, the customer gets a discount. Otherwise, they don’t.

 

Example Output:

Let’s see how it works when you enter some data:

Case 1: Loyal Customer with a $60 Purchase

				
					Are you a loyal customer? (true/false): true
Enter your total purchase amount: $60
You qualify for a discount!
				
			

Case 2: Not a Loyal Customer with a $60 Purchase

				
					Are you a loyal customer? (true/false): false
Enter your total purchase amount: $60
Sorry, you don't qualify for a discount.
				
			

Case 3: Loyal Customer with a $40 Purchase.

				
					Are you a loyal customer? (true/false): true
Enter your total purchase amount: $40
Sorry, you don't qualify for a discount.
				
			

How the Code Works:

  1. In Case 1: The customer is loyal and their purchase is greater than or equal to $50, so they get the discount.
  2. In Case 2: The customer is not loyal, even though their purchase is over $50, so they don’t get the discount.
  3. In Case 3: The customer is loyal, but their purchase is less than $50, so they don’t get the discount either.

 

Conclusion:

There you have it! C# logical operators are really handy when you need to check multiple conditions at once. Whether you’re checking if a user meets all the requirements for a promotion or testing if multiple conditions are true, logical operators are a must-know tool. Once you get comfortable with C# logical operators, your code will become more powerful and efficient. 💪

Now, you can combine different conditions with AND (&&), OR (||), and NOT (!) to make smart decisions in your programs. You’ll use them all the time in real-world applications, like checking eligibility for discounts, user permissions, or more.

 

Next What?

Alright, you’ve made it through C# logical operators! 🎉 But don’t get too comfortable, because in the next lesson, you’ll learn about increment and decrement operators. These operators let you easily increase or decrease a number by one, which comes in handy for loops and counters. Can’t wait to dive into that with you! 😄

Leave a Comment

Share this Doc

Logical operators

Or copy link