You can make choices in C++ by using conditional statements like if, else-if, else and switch:
Syntax for if, else-if, else:
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
Syntax for Switch
#include <iostream>
using namespace std;
int main() {
int choice;
cout << "Choose an option (1-3): ";
cin >> choice;
switch (choice) {
case 1:
cout << "You chose option 1." << endl;
break;
case 2:
cout << "You chose option 2." << endl;
break;
case 3:
cout << "You chose option 3." << endl;
break;
default:
cout << "Invalid choice! Please choose a number between 1 and 3." << endl;
}
return 0;
}