Advanced if Statements in C++
By: Priya in C++ Tutorials on 2007-09-04
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments