🔀 Decision Making in C++
Control flow statements allow your program to branch dynamically based on variable values or conditional evaluation.
💡 Syntax Structure: if-else vs switch-case
💻 Code Example: Grading & Calculator Engine
⊞C++ Source Code (main.cpp)
#include <iostream>
int main() {
char option;
std::cout << "Choose Operation [A: Addition, S: Subtraction, M: Multiplication]: ";
std::cin >> option;
double num1 = 50.0, num2 = 10.0;
switch (toupper(option)) {
case 'A':
std::cout << "Result: " << (num1 + num2) << std::endl;
break;
case 'S':
std::cout << "Result: " << (num1 - num2) << std::endl;
break;
case 'M':
std::cout << "Result: " << (num1 * num2) << std::endl;
break;
default:
std::cout << "Invalid Option Selected!" << std::endl;
break;
}
return 0;
}
⊞Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
⊞Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)
—□✕
Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)
(c) Microsoft Corporation. All rights reserved.
C:\Users\Student\CPP_Project>#include <iostream>
C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp
C:\Users\Student\CPP_Project> main.exe
Hello World from C++ on Windows 11 Pro!
[Process exited with code 0 in 0.001 seconds]
> 💡 Developer Joke: Why did the C++ developer get lost in the city?
> Because they couldn't find the default: case! 😂