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
MediumCreate a method called evaluateApplication that:
- Takes three parameters: an applicant's age (int), their score (int), and whether they have prior experience (bool)
- 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
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety