Conditional logic controls the flow of execution based on boolean states. Go supports standard if/else structures for sequential logic and switch statements for multi-branch, discrete value evaluation or cleaner condition grouping.
Implement complex request classification using if/else and a grading system using switch.
- Function:
ClassifyRequest(age int, hasID bool, balance float64, isVIP bool) string - Use
if,else if,else. Do not use switch. - Evaluate rules top-to-bottom. Return immediately upon match.
- Rules:
- "INVALID": if
age <= 0ORbalance < 0. - "REJECTED": if
age < 18ORhasIDis false. - "VIP_ACCESS": if
isVIPis true ANDbalance >= 10000. - "STANDARD_ACCESS": if
balance >= 1000. - "LIMITED_ACCESS": All other cases.
- "INVALID": if
- Function:
EvaluateGrade(score int) string - Use
switch. Do not use if/else. - Rules:
- "INVALID": if
score < 0ORscore > 100. - "A": 90–100.
- "B": 80–89.
- "C": 70–79.
- "D": 60–69.
- "F": 0–59.
- "INVALID": if
ClassifyRequest:age(int),hasID(bool),balance(float64),isVIP(bool)EvaluateGrade:score(int)
- Both return a
stringmatching exactly one of the allowed return values defined in requirements (e.g. "VIP_ACCESS", "A").
ClassifyRequest(30, true, 15000, true)→"VIP_ACCESS"ClassifyRequest(16, true, 100, false)→"REJECTED"EvaluateGrade(95)→"A"EvaluateGrade(105)→"INVALID"
To run the tests, execute the following command from the root directory:
go test -v ./quests/004.conditionsOr from the quest directory:
go test -vExpected output:
=== RUN TestClassifyRequest
--- PASS: TestClassifyRequest (0.00s)
=== RUN TestEvaluateGrade
--- PASS: TestEvaluateGrade (0.00s)
PASS