Conditional Statements
Conditional statements let a program choose which operations to perform. A decision problem is often easiest to solve by identifying mutually exclusive cases and checking them in a deliberate order. We classify a score and interpret a one-character command, demonstrating both if/else chains for ranges and switch for discrete values. The example has a small input contract so boundary behavior is easy to verify.
Prerequisites: Data Types and Operators.
Build the idea
1. Evaluate a Boolean condition
if(condition) executes its body when the condition is true. Optional else executes when it is false. Braces group several statements and avoid the common mistake of guarding only one line. A compound condition such as score<0 || score>100 rejects either invalid side of a range. To mean “between 0 and 100,” use score>=0 && score<=100, not a chained mathematical comparison.
2. Order overlapping range checks
The score categories are HIGH for 90..100, PASS for 50..89 and RETRY for 0..49. Check invalid values first, then score>=90, then score>=50, then the remaining valid values. The else-if chain chooses only one branch. Separate independent if statements would print both HIGH and PASS for 95 unless their conditions were adjusted.
3. Cover boundaries explicitly
For score 90, the first valid threshold applies; 89 falls through to PASS. Scores 50 and 49 lie on opposite sides of the passing boundary. Invalid values do not become ordinary failures because the validity check precedes category selection. Writing these examples before coding makes missing or overlapping intervals visible.
4. Use switch for exact discrete choices
switch(command) compares an integral or character expression against case constants. Here 'A' means ADD, 'R' means RESET and any other character means UNKNOWN. break exits the switch after a handled case. Without it, execution falls through into following case bodies unless intentionally controlled. switch does not directly express arbitrary score ranges or string comparisons in C++17.
5. Trace the sample
For score 72, invalid and high checks are false but score>=50 is true, so PASS is printed. Command A selects case 'A', prints ADD and breaks. Each output line describes one independent decision. Conditions do not consume new input; they inspect the values already read into variables.
C++17 implementation
This is a complete program. Compile with C++17 and enter the input described below.
// Key idea: 5. Trace the sample For score 72, invalid and high checks are false but score>=50 is true,
// so PASS is printed.
#include<iostream>
int main(){
int score;
char command;
std::cin>>score>>command;
if(score<0||score>100){
std::cout<<"INVALID\n";
}
else if(score>=90){
std::cout<<"HIGH\n";
}
else if(score>=50){
std::cout<<"PASS\n";
}
else{
std::cout<<"RETRY\n";
}
switch(command){
case 'A':
std::cout<<"ADD\n";
break;
case 'R':
std::cout<<"RESET\n";
break;
default:
std::cout<<"UNKNOWN\n";
break;
}
}
Complexity analysis
O(1) time and space: there is a fixed number of comparisons and at most two output lines. More cases in a different problem would require a separate analysis rather than relying on this fixed example.
Example input and output
Read an int score and one non-whitespace character command. Classify score as INVALID, HIGH, PASS or RETRY, then interpret A as ADD, R as RESET and anything else as UNKNOWN.
Input
72 A
Output
PASS ADD
72 belongs to 50..89 and A selects the ADD case.
Practical applications
Use if/else for decisions based on intervals, compound conditions or arbitrary Boolean expressions. Use switch for a manageable set of exact integral/character options. Tables or maps may organize large data-driven choices later, but clear branch logic is preferable for a small fixed classification.
Check your understanding
Why check score>=90 before score>=50?
Put it into practice
Test scores −1,0,49,50,89,90,100,101 with commands A,R,X.
Show a hint
Check each category boundary and the switch default separately.