c++ multiple if conditions
#include <iostream>
using namespace std;
int main() {
int x = 10;
if (x == 5) {
cout << "x is equal to 5" << endl;
} else if (x == 10) {
cout << "x is equal to 10" << endl;
} else {
cout << "x is neither 5 nor 10" << endl;
}
return 0;
}
Explanation:
- The program starts by including the necessary header file
iostream
for input and output operations. using namespace std;
allows us to use elements from thestd
namespace without prependingstd::
.int main()
is the entry point of the program.int x = 10;
declares an integer variablex
and initializes it with the value10
.- The
if-else if-else
construct checks the value ofx
: if (x == 5)
checks ifx
is equal to5
.- If true, it prints "x is equal to 5".
else if (x == 10)
checks ifx
is equal to10
.- If true, it prints "x is equal to 10".
- If none of the conditions above are true, the
else
block executes and prints "x is neither 5 nor 10". return 0;
signifies successful completion of themain()
function with a return status of0
.