Menu
Coddy logo textTech

Multiple Conditions

Part of the Logic & Flow section of Coddy's C# journey — lesson 8 of 66.

C# allows you to combine multiple conditions in your decision-making logic using logical operators.

Use the AND operator (&&) to check if multiple conditions are all true:

int age = 25;
double salary = 50000;

if (age > 18 && salary >= 30000)
{
    Console.WriteLine("Eligible for loan");
}

Use the OR operator (||) when at least one condition needs to be true:

bool hasLicense = true;
bool hasPassport = false;

if (hasLicense || hasPassport)
{
    Console.WriteLine("Has valid ID");
}

Combine multiple operators with parentheses for complex conditions:

bool isWeekend = true;
bool isHoliday = false;
int hour = 14;

if ((isWeekend || isHoliday) && hour > 10)
{
    Console.WriteLine("Store is open");
}

Use the NOT operator (!) to negate a condition:

bool isRaining = true;

if (!isRaining)
{
    Console.WriteLine("No need for umbrella");
}
else
{
    Console.WriteLine("Take an umbrella");
}
challenge icon

Challenge

Medium

Create a method called evaluateApplication that:

  1. Takes three parameters: an applicant's age (int), their score (int), and whether they have prior experience (bool)
  2. Returns a string based on these multiple conditions:
    • "Accepted" if the applicant is at least 18 years old AND has a score above 70
    • "Accepted with Merit" if the applicant meets the above conditions AND has prior experience
    • "Provisionally Accepted" if the applicant is at least 18 years old AND has a score between 50 and 70 inclusive
    • "Rejected" in all other cases

Cheat sheet

C# provides logical operators to combine multiple conditions in if statements:

AND operator (&&) - all conditions must be true:

if (age > 18 && salary >= 30000)
{
    Console.WriteLine("Eligible for loan");
}

OR operator (||) - at least one condition must be true:

if (hasLicense || hasPassport)
{
    Console.WriteLine("Has valid ID");
}

NOT operator (!) - negates a condition:

if (!isRaining)
{
    Console.WriteLine("No need for umbrella");
}

Complex conditions - use parentheses to group conditions:

if ((isWeekend || isHoliday) && hour > 10)
{
    Console.WriteLine("Store is open");
}

Try it yourself

public class EvaluateApplication
{
    // Implement the EvaluateApplication method
    public static string evaluateApplication(int age, int score, bool hasPriorExperience)
    {
        // Write your code here
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow