conditional cout in c++
Conditional cout in C++
#include <iostream>
using namespace std;
int main() {
int x = 10;
if (x > 5) {
cout << "x is greater than 5" << endl;
} else {
cout << "x is not greater than 5" << endl;
}
return 0;
}
Explanation
- Include the iostream library: This line includes the input-output stream library, which allows us to perform input and output operations in C++.
- Using namespace std: This line allows us to use entities from the standard C++ library, such as
cout
andendl
, without specifying the namespace. - Define the main function: This is the entry point of the program where the execution begins.
- Initialize variable x: We declare and initialize the variable
x
with the value 10. - Conditional statement (if-else): We use an if-else statement to check if
x
is greater than 5. If the condition is true, "x is greater than 5" is printed usingcout
. Otherwise, "x is not greater than 5" is printed.
This program demonstrates the usage of conditional cout
statements in C++, where the output is based on the evaluation of a condition.