Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Lesson 6: Control Flow – if, else, switc | C++ Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. C++ Course

Lesson 6 of 45

·C++

Lesson 6: Control Flow – if, else, switch

10 min

Course progress13%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Simple if‑else chains
  • Nested conditions
  • Switch statements with fall‑through
  • When to use each

Control Flow – if, else, switch

Conditional statements let a program choose between alternative execution paths. if evaluates a boolean expression, optionally followed by else if and a final else. The switch statement jumps to a case label based on an integral or enumeration value.


Example: if‑else and switch

int score = 85;
if (score >= 90) {
    std::cout << \"Grade A\" << '\
';
} else if (score >= 80) {
    std::cout << \"Grade B\" << '\
';
} else {
    std::cout << \"Grade C or lower\" << '\
';
}

enum class Color { Red, Green, Blue };
Color c = Color::Green;
switch (c) {
    case Color::Red:   std::cout << \"Red\
\"; break;
    case Color::Green: std::cout << \"Green\
\"; break;
    case Color::Blue:  std::cout << \"Blue\
\"; break;
    default:          std::cout << \"Unknown\
\"; break;
}

Tip: With C++17 you can use if constexpr to conditionally compile branches based on compile‑time constants.


<Alert type="warning">switch does not support float or std::string directly. Use if‑else or a std::map for those cases.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from C++ CourseBrowse activities
Previous lessonLesson 5: OperatorsContinue to lesson 7Lesson 7: Loops – for, while, do‑whileBack to C++ Course curriculum