< Previous | Contents | Next >
An if statement can cause a program to execute a statement or block of statements, including other if statements. When you write one if statement
inside another, it’s called nesting. In the following code, the if statement that begins if (score >= 1000) is nested inside the if statement that begins if (score > 500).
if (score >= 500)
{
cout << "You scored 500 or more. Nice.\n\n";
if (score >= 1000)
{
cout << "You scored 1000 or more. Impressive!\n";
}
}
Because score is greater than 500, the program enters the statement block and displays the message, “You scored 500 or more. Nice.” Then, in the inner if statement, the program compares score to 1000. Because score is greater than or equal to 1000, the program displays the message, “You scored 1000 or more. Impressive!”
Hin t
You can nest as many levels as you want. However, if you nest code too deeply, it gets hard to read. In general, you should try to limit your nesting to a few levels at most.