YouTip LogoYouTip

Cpp Nested If

# C++ Nested if Statement [![Image 3: C++ Decisions](#) C++ Decisions](#) In C++, **nested** if-else statements are legal, which means you can use another **if** or **else if** statement inside an **if** or **else if** statement. A nested if statement is a variant of an if statement where one if statement can be nested inside another if statement. Nested if statements can help you test multiple conditions more precisely. ## Syntax The syntax for a **nested if** statement in C++: if( boolean_expression 1){ // Executes when the boolean expression 1 is true if(boolean_expression 2) { // Executes when the boolean expression 2 is true }} You can nest **else if...else** in the same way as you nest _if_ statements. if (condition1) { // Executes if condition1 is true if (condition2) { // Executes if condition2 is also true } else { // Executes if condition2 is false }}else { // Executes if condition1 is false} ## Example The following example demonstrates nested if statements for different condition checks: ## Example #include using namespace std; int main(){ int x =10; if(x <20){ cout<<"x Less than 20"<< endl; if(x <15){ cout<<"x Less than 15"<< endl; } } return 0; } In the example above, it first checks if x is less than 20. If the condition is true, the first message inside the if statement is output. Then, the program checks if x is less than 15. If this condition is also true, the second message inside the if statement is output. When the above code is compiled and executed, it produces the following result: x Less than 20 x Less than 15 Another
← Python3 Sum ArrayPython3 Area Of A Circle β†’