Chapter 3
HomeLoops let you repeat work. Conditionals let you choose what work to do. Together they form the entire control flow toolkit you need to solve almost any competitive programming problem.
The if statement runs a block only when a condition is true.
Optionally, else runs a different block when the condition is false.
Given one integer, print "even" if it's even, otherwise "odd".
Sample input: 7 Expected output: odd
#include <bits/stdc++.h>
using namespace std;
int main() {
int x; cin >> x;
if (x % 2 == 0) {
cout << "even\n";
} else {
cout << "odd\n";
}
return 0;
}
use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().lock().read_to_string(&mut input).unwrap();
let x: i32 = input.trim().parse().unwrap();
if x % 2 == 0 {
println!("even");
} else {
println!("odd");
}
}
📐 Anatomy of a conditional
if (condition) {
// runs when condition is true
} else {
// runs when condition is false
}
if (condition1) {
// first check
} else if (condition2) {
// second check
} else {
// fallback
}
You can chain multiple conditions with else if. The first true
branch wins — once one condition matches, the rest are skipped.
Given a score from 0–100, print A (≥90), B (≥80), C (≥70), D (≥60), or F (<60).
Sample input: 85 Expected output: B
#include <bits/stdc++.h>
using namespace std;
int main() {
int score; cin >> score;
char grade;
if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else if (score >= 60) grade = 'D';
else grade = 'F';
cout << grade << "\n";
return 0;
}
use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().lock().read_to_string(&mut input).unwrap();
let score: i32 = input.trim().parse().unwrap();
let grade = if score >= 90 { 'A' }
else if score >= 80 { 'B' }
else if score >= 70 { 'C' }
else if score >= 60 { 'D' }
else { 'F' };
println!("{}", grade);
}
In Rust, if is an expression — it returns a value.
💡 Order matters
Check the most specific condition first. If you wrote if (score >= 60)
first, a score of 95 would match that branch and print "D" — wrong! Always order
from most restrictive to least restrictive.
Combine multiple conditions with logical operators. These let you ask "is this AND that true?" or "is this OR that true?"
| Operator | C++ | Rust | Meaning |
|---|---|---|---|
| AND | && | && | Both true |
| OR | || | || | At least one true |
| NOT | ! | ! | Inverts true/false |
Given N, print "FizzBuzz" if divisible by both 3 and 5, otherwise the number itself.
Sample input: 15 Expected output: FizzBuzz
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
if (n % 3 == 0 && n % 5 == 0) {
cout << "FizzBuzz\n";
} else {
cout << n << "\n";
}
return 0;
}
use std::io::{self, Read};
fn main() {
let mut input = String::new();
io::stdin().lock().read_to_string(&mut input).unwrap();
let n: i32 = input.trim().parse().unwrap();
if n % 3 == 0 && n % 5 == 0 {
println!("FizzBuzz");
} else {
println!("{}", n);
}
}
💡 Short-circuit evaluation
With &&, if the left side is false, the right side is never evaluated.
With ||, if the left side is true, the right side is skipped.
This matters when the second condition would crash — like checking i < n && arr[i] > 0.
A compact way to write a simple if/else that returns a value. Use it sparingly — it's great for short conditions, but nested ternaries are hard to read.
int x; cin >> x; string result = (x % 2 == 0) ? "even" : "odd"; cout << result << "\n";
let x: i32 = input.trim().parse().unwrap();
let result = if x % 2 == 0 { "even" } else { "odd" };
println!("{}", result);
Rust doesn't have a ternary operator — if is already an expression.
⚠️ Don't nest ternaries
a ? b ? c : d : e is confusing even to experienced programmers.
Use if/else chains instead.
if / else if / else chains run the first true branch and skip the rest.
&& (and), || (or), ! (not) to combine conditions.
cond ? a : b (C++) or if expression (Rust) is for simple cases only.