gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
how do you write anested if statement in c++
- User
how do you write anested if statement in c++
- Assistant
In C++, a nested if statement is an if statement that is enclosed within another if statement. Here's the general syntax for writing a nested if statement in C++:
cppif (condition1) { // code to be executed if condition1 is true if (condition2) { // code to be executed if condition1 and condition2 are true } else { // code to be executed if condition1 is true but condition2 is false } } else { // code to be executed if condition1 is false }Here's an example to demonstrate the usage of the nested if statement:
cpp#include <iostream> int main() { int num = 10; if (num > 0) { std::cout << "Number is positive." << std::endl; if (num % 2 == 0) { std::cout << "Number is even." << std::endl; } else { std::cout << "Number is odd." << std::endl; } } else { std::cout << "Number is non-positive." << std::endl; } return 0; }