Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Nested if Statements

C++ Nested if Statements

C++ Decision C++ Decision Making

In C++,Nested if-else statements are legal, which means you can nest one inside a if or else if if statement inside another if or else if if statement.

A nested if statement is a variant of an if statement, where one if statement can be nested within another if statement.

Nested if statements can help you test multiple conditions more precisely.

Syntax

In C++ Nested if statement syntax:



if( boolean_expression 1)

{

   // Execute when boolean expression 1 is true

   if(boolean_expression 2)

   {

      // Execute when boolean expression 2 is true

   }

}

You can nest else if...else, in the same way as nesting if statements.

if (condition1) {

   // If condition1 is true, execute the code block here

   if (condition2) {

      // If condition2 is also true, execute the code block here

   }

   else {

      // If condition2 is false, execute the code block here

   }

}

else {

   // If condition1 is false, execute the code block here

}

Example

The following example uses if nesting to evaluate different conditions:

Example

#include

using

namespace

std

;

int

main

(

)

{

   

int

x

=

10

;

   

if

(

x

<

20

)

{

cout

<<

"x is less than 20"

<<

endl

;

       

if

(

x

<

15

)

{

cout

<<

"x is less than 15"

<<

endl

;

       

}

   

}

   

return

0

;

}

In the example above, it first checks whether x is less than 20. If the condition holds, the first message in the if statement will be output. Then, the program checks whether x is less than 15. If this condition also holds, the second message in the if statement will be output.

When the code above is compiled and executed, it produces the following result:



x 小于 20

x 小于 15

Another nested if example:

Example 2

#include

using

namespace

std

;

 

int

main

(

)

{

   

// local variable declaration

   

int

a

=

100

;

   

int

b

=

200

;

 

   

// Check boolean condition

   

if

(

a

==

100

)

   

{

       

// If the condition is true, check the condition below

       

if

(

b

==

200

)

       

{

         

// If the condition is true, output the statement below

         

cout

<<

"The value of a is 100, and the value of b is 200"

<<

endl

;

       

}

   

}

   

cout

<<

"The exact value of a is "

<<

a

<<

endl

;

   

cout

<<

"The exact value of b is "

<<

b

<<

endl

;

 

   

return

0

;

}

When the code above is compiled and executed, it produces the following result:



a 的值是 100,且 b 的值是 200

a 的准确值是 100

b 的准确值是 200

The following example demonstrates the use of nested if-else statements:

Example 3

#include

using

namespace

std

;

int

main

(

)

{

   

int

x

=

15

;

   

if

(

x

<

20

)

{

cout

<<

"x is less than 20"

<<

endl

;

       

if

(

x

<

15

)

{

cout

<<

"x is less than 15"

<<

endl

;

       

}

else

{

           

cout

<<

"x is greater than or equal to 15"

<<

endl

;

       

}

   

}

else

{

       

cout

<<

"x is greater than or equal to 20"

<<

endl

;

   

}

   

return

0

;

}

In the example above, it first checks whether x is less than 20; if the condition holds, the first message in the if statement will be output.

Then, the program checks whether x is less than 15. If this condition also holds, the second message in the if statement will be output. If the second condition does not hold, the message in the else statement will be output.

If x is greater than or equal to 20, the first condition of the if statement does not hold, and the message in the else statement will be output.

When the code above is compiled and executed, it produces the following result:



x 小于 20

x 大于等于 15

C++ Decision C++ Decision Making