By: Priya Emailed: 1673 times Printed: 2153 times

 By: rohit kumar - how this program is work By: Kirti - Hi..thx for the hadoop in By: Spijker - I have altered the code a By: ali mohammed - why we use the java in ne By: ali mohammed - why we use the java in ne By: mizhelle - when I exported the data By: raul - no output as well, i'm ge By: Rajesh - thanx very much... By: Suindu De - Suppose we are executing

It is worth noting that any statement can be used in an if or else clause, even another if or else statement. Thus, you might see complex if statements in the following form:

```if (expression1)
{
if (expression2)
statement1;
else
{
if (expression3)
statement2;
else
statement3;
}
}
else
statement4;```

This cumbersome if statement says, "If expression1 is true and expression2 is true, execute statement1. If expression1 is true but expression2 is not true, then if expression3 is true execute statement2. If expression1 is true but expression2 and expression3 are false, execute statement3. Finally, if expression1 is not true, execute statement4." As you can see, complex if statements can be confusing!

Listing below gives an example of such a complex if statement.

A complex, nested if statement.

```1:  // a complex nested
2:  // if statement
3:  #include <iostream.h>
4:  int main()
5:  {
6:      // Ask for two numbers
7:      // Assign the numbers to bigNumber and littleNumber
8:      // If bigNumber is bigger than littleNumber,
9:      // see if they are evenly divisible
10:     // If they are, see if they are the same number
11:
12:     int firstNumber, secondNumber;
13:     cout << "Enter two numbers.\nFirst: ";
14:     cin >> firstNumber;
15:     cout << "\nSecond: ";
16:     cin >> secondNumber;
17:     cout << "\n\n";
18:
19:     if (firstNumber >= secondNumber)
20:     {
21:       if ( (firstNumber % secondNumber) == 0) // evenly divisible?
22:       {
23:            if (firstNumber == secondNumber)
24:                 cout << "They are the same!\n";
25:            else
26:                 cout << "They are evenly divisible!\n";
27:       }
28:       else
29:            cout << "They are not evenly divisible!\n";
30:     }
31:     else
32:       cout << "Hey! The second one is larger!\n";
33:        return 0;
34: }

Output: Enter two numbers.
First: 10

Second: 2

They are evenly divisible!
```

Analysis: Two numbers are prompted for one at a time, and then compared. The first if statement, on line 19, checks to ensure that the first number is greater than or equal to the second. If not, the else clause on line 31 is executed.
If the first if is true, the block of code beginning on line 20 is executed, and the second if statement is tested, on line 21. This checks to see whether the first number modulo the second number yields no remainder. If so, the numbers are either evenly divisible or equal. The if statement on line 23 checks for equality and displays the appropriate message either way.

If the if statement on line 21 fails, the else statement on line 28 is executed.

Be the first one to add a comment

Your email(required, will not be shown to the public):

More Tutorials by Priya
 Getting started with ASP EJB Entity Beans Error Handling - Stderr and Exit - sample program in C Pointers and Arrays in C Data Types and Sizes in C virtual inheritance example in C++ ctime() sample program in C++ TextBox sample program in J2ME Advantages of Java Beans list() contents of a Directory - sample program in Java File example program in Java continue and break statements in C++ Use of 'throw' in Java Passing parameters to a function by value in C++ Demonstrating global and local variables in C++

More Tutorials in C++
 Two-Dimensional Array Manipulation in C++ Calculate average using Two-Dimensional Array in C++ Compute the square root of the sum of the squares of an array in C++ Matrix using nested for loops in C++ Sorting an array of Strings in C++ Calculating total based on the given quantity and price in C++ Compiling and Linking Multiple Source Files in C++ Enumerations in C++ Program to add two numbers in C++ Comments in C++ while loop in C++ for loop in C++ Programming errors a compiler will detect in C++ if in C++ Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
 C++ Recursion function explained using Fibonacci series Sorting an array of Strings in C++ Constructors and Destructors in C++ Dot (.) vs Arrow (->) to access data members in C++ Where to Put Class Declarations and Method Definitions in C++ Using cout.width() in C++ virtual inheritance example in C++ Public versus Private members in C++ while (1) Loops in C++ Calculating total based on the given quantity and price in C++ Demonstration of Prefix and Postfix operators in C++ The Birth, history and need for C++ const Member Functions in C++ Looping with the keyword goto in C++ while Loops in C++
Most Emailed Articles (in C++)
 Looping with the keyword goto in C++ Default arguments in C++ Compiling and Linking Multiple Source Files in C++ Difference between Procedural, Structured, and Object-Oriented Programming Tutorial on Classes and Members in C++ for Loops in C++ Using switch Statements in C++ Operator Precedence in C++ How to handle Exceptions in C++ strcpy() and strncpy() sample program in C++ assert() example program in C++ Using peek() and putback() with cin in C++ Function overloading in C++ File in C++ - Writing text to a file in C++ Pass by Reference vs Pass Value in C++ functions