Multiple Conditions
Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨 — 66개 중 8번째.
C#에서는 논리 연산자를 사용하여 의사 결정 로직에서 여러 조건을 결합할 수 있습니다.
여러 조건이 모두 참인지 확인하기 위해 AND 연산자(&&)를 사용하세요:
int age = 25;
double salary = 50000;
if (age > 18 && salary >= 30000)
{
Console.WriteLine("Eligible for loan");
}적어도 하나의 조건이 참이어야 할 때 OR 연산자 (||)를 사용하세요:
bool hasLicense = true;
bool hasPassport = false;
if (hasLicense || hasPassport)
{
Console.WriteLine("Has valid ID");
}복잡한 조건을 위해 괄호를 사용하여 여러 연산자를 결합하세요:
bool isWeekend = true;
bool isHoliday = false;
int hour = 14;
if ((isWeekend || isHoliday) && hour > 10)
{
Console.WriteLine("Store is open");
}NOT 연산자 (!)를 사용하여 조건을 부정하세요:
bool isRaining = true;
if (!isRaining)
{
Console.WriteLine("No need for umbrella");
}
else
{
Console.WriteLine("Take an umbrella");
}챌린지
중급evaluateApplication이라는 메서드를 생성하세요. 다음을 수행하도록 하세요:
- 지원자의 나이(int), 점수(int), 이전 경험 여부(bool)인 세 개의 매개변수를 받습니다
- 다음의 여러 조건에 따라 문자열을 반환합니다:
- 지원자가 18세 이상이고 점수가 70 초과인 경우 "Accepted"
- 지원자가 위 조건을 충족하고 이전 경험이 있는 경우 "Accepted with Merit"
- 지원자가 18세 이상이고 점수가 50과 70 사이(포함)인 경우 "Provisionally Accepted"
- 그 외 모든 경우 "Rejected"
치트 시트
C#은 if 문에서 여러 조건을 결합하기 위한 논리 연산자를 제공합니다:
AND 연산자 (&&) - 모든 조건이 참이어야 합니다:
if (age > 18 && salary >= 30000)
{
Console.WriteLine("Eligible for loan");
}OR 연산자 (||) - 적어도 하나의 조건이 참이어야 합니다:
if (hasLicense || hasPassport)
{
Console.WriteLine("Has valid ID");
}NOT 연산자 (!) - 조건을 부정합니다:
if (!isRaining)
{
Console.WriteLine("No need for umbrella");
}복잡한 조건 - 조건을 그룹화하기 위해 괄호를 사용합니다:
if ((isWeekend || isHoliday) && hour > 10)
{
Console.WriteLine("Store is open");
}직접 해보기
public class EvaluateApplication
{
// EvaluateApplication 메서드를 구현하세요
public static string evaluateApplication(int age, int score, bool hasPriorExperience)
{
// 여기에 코드를 작성하세요
}
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
논리 및 흐름의 모든 레슨
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