Nested If Statement In Dev C++

In C++, the braces of an if or an else clause can contain another if statement. These are known as nestedif statements. The following NestedIf program shows an example of a nested if statement in use.

Nov 15, 2016  For the Love of Physics - Walter Lewin - May 16, 2011 - Duration: 1:01:26. Lectures by Walter Lewin. They will make you ♥ Physics. Recommended for you. C: Switch Statements Sometimes when creating a C program, you run into a situation in which you want to compare one thing to a number of other things. Let's say, for example, that you took a character from the user and wanted to compare this to a number of characters to perform different actions.

This program starts by asking for the user’s birth year. If the birth year is later than 2000, then the program outputs the string “You were born in the 21st century”.

In mathematically accurate terms, the year 2000 belongs to the 20th century, not the 21st.

If the birth year is not greater than 2000, then the program enters the else clause of the outer if statement. This clause starts by outputting the string “You were born in” before comparing the birth year to 1950.

If the birth year is less than 1950, then the program adds the first “the first half”. If the birth year is not less than 1950, then the else clause of the inner if statement is executed, which tacks on the phrase “the second half”. Finally, the program adds the concluding phrase “of the 20th century” to whatever has been output so far.

In practice, the output of the program appears as follows for three possible values for birth year. First, 2002 produces the following:

For example, 1956 generates the following:

Finally, the birth year of 1932 generates the third possibility:

DevC++

You could use a nested if to avoid the unnecessary comparisons in the NestedBranchDemo program:

This version performs the first comparison just as before. If nOperand1 is greater than nOperand2, this snippet outputs the string “Argument 1 is greater than argument 2”. From here, however, control jumps to the final closed brace, thereby skipping the remaining comparisons.

Nested Switch In C

If nOperand1 is not greater than nOperand2, then the snippet performs a second test to differentiate the case that nOperand1 is less than nOperand2 from the case that they are equal in value.

The figure shows graphically the flow of control for the NestedBranchDemo program for the input of 5 and 10.

Nested If Statement In Dev C In Excel

Performing the test for equality is unnecessary: If nOperand1 is neither greater than nor less than nOperand2, then it must be equal.