---
jupytext:
formats: md:myst
text_representation:
extension: .md
format_name: myst
kernelspec:
display_name: Python 3
language: python
name: python3
---Conditions and Operators: Conditions and Switch - Case¶
Adapted from: “Learn Modern C++” by cpptutor: Learn Modern C++: Conditions and Operators
Program that Demonstrates Conditions and Switch Case¶
// 03-calc.cpp : simple calculator with four functions
#include <iostream>
using namespace std;
int main() {
int r{}, x{}, y{};
char op{};
cout << "Please enter a calculation (number op number, op is one of +-*/):\n";
cin >> x >> op >> y;
switch (op) {
case '+':
r = x + y;
break;
case '-':
r = x - y;
break;
case '*':
r = x * y;
break;
case '/':
if (y) {
r = x / y;
}
else {
cerr << "Error: divide by zero.\n";
}
break;
default:
cerr << "Error: invalid op.\n";
break;
}
cout << "Result: " << r << '\n';
}Explanation of the Above Code¶
This C++ program implements a simple calculator that performs basic arithmetic operations (+, -, *, /) based on user input. Here’s a breakdown of the code:
Code Explanation:¶
Header Inclusion:
#include <iostream>The
<iostream>header is included to enable input (cin) and output (coutandcerr) operations.
Namespace:
using namespace std;This allows the program to use standard library features (like
cin,cout, andcerr) without prefixing them withstd::.
Main Function:
int main() {The
mainfunction is the entry point of the program.
Variable Declarations:
int r{}, x{}, y{}; char op{};r: Stores the result of the calculation.xandy: Store the two numbers entered by the user.op: Stores the operator entered by the user.
User Input:
cout << "Please enter a calculation (number op number, op is one of +-*/):\n"; cin >> x >> op >> y;Prompts the user to enter a calculation in the format
number operator number(e.g.,5 + 3).Reads the input values into
x,op, andy.
Switch Statement:
switch (op) {Evaluates the operator (
op) and performs the corresponding arithmetic operation.Case ‘+’:
case '+': r = x + y; break;Adds
xandyand stores the result inr.
Case ‘-’:
case '-': r = x - y; break;Subtracts
yfromxand stores the result inr.
Case ‘*’:
case '*': r = x * y; break;Multiplies
xandyand stores the result inr.
Case ‘/’:
case '/': if (y) { r = x / y; } else { cerr << "Error: divide by zero.\n"; } break;Divides
xbyyand stores the result inr.Checks if
yis zero to avoid division by zero. Ifyis zero, an error message is printed tocerr.
Default Case:
default: cerr << "Error: invalid op.\n"; break;Handles invalid operators by printing an error message to
cerr.
Output the Result:
cout << "Result: " << r << '\n';Prints the result of the calculation to the console.
Key Features:¶
Error Handling:
Division by zero is explicitly checked, and an error message is displayed if it occurs.
Invalid operators are handled with a default case in the
switchstatement.
User Interaction:
The program is interactive, prompting the user for input and displaying the result.
Example Input/Output:¶
Valid Input:
Input: 5 + 3 Output: Result: 8Division by Zero:
Input: 5 / 0 Output: Error: divide by zero.Invalid Operator:
Input: 5 % 3 Output: Error: invalid op.
Compile and Run Code¶
Use Python to Change to Working Directory¶
import os
root_dir = os.getcwd()code_dir = root_dir + "/" + "Cpp_Code/03_Conditions_and_Operators"os.chdir(code_dir)Use Docker to Compile the Code in a C++23 Environment¶
!docker run --rm -v $(pwd):/app cpp23-clang18:latest clang++-18 -std=c++23 -stdlib=libc++ /app/03-calc.cpp -o /app/03-calcUse Docker to Run Executable in a C++23 Environment¶
!echo "5 + 5" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-calcPlease enter a calculation (number op number, op is one of +-*/):
Result: 10
!echo "5 * 5" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-calcPlease enter a calculation (number op number, op is one of +-*/):
Result: 25
!echo "25 / 5" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-calcPlease enter a calculation (number op number, op is one of +-*/):
Result: 5
!echo "15 - 5" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-calcPlease enter a calculation (number op number, op is one of +-*/):
Result: 10
!echo "15 / 0" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-calcPlease enter a calculation (number op number, op is one of +-*/):
Result: 0
Error: divide by zero.
!echo "15 V 5" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-calcPlease enter a calculation (number op number, op is one of +-*/):
Result: 0
Error: invalid op.