When you code an if statement within another if statement as in the following?
Home / c-programming / C – If..else, Nested If..else and else..if Statement with example Show
In the last tutorial we learned how to use if statement in C. In this guide, we will learn how to use if else, nested if else and else if statements in a C Program. Syntax of if else statement: if(condition) { // Statements inside body of if } else { //Statements inside body of else } Flow diagram of if else statementExample of if else statementIn this program user is asked to enter the age and based on the input, the if..else statement checks whether the entered age is greater than or equal to 18. If this condition meet then display message “You are eligible for voting”, however if the condition doesn’t meet then display a different message “You are not eligible for voting”. #include Output: Enter your age:14 You are not eligible for voting Note: If there is only one statement is present in the “if” or “else” body then you do not need to use the braces (parenthesis). For example the above program can be rewritten like this: #include C Nested If..else statementWhen an if else statement is present inside the body of another “if” or “else” then this is called nested if else. if(condition) { //Nested if else inside the body of "if" if(condition2) { //Statements inside the body of nested "if" } else { //Statements inside the body of nested "else" } } else { //Statements inside the body of "else" } Example of nested if..else#include Output: Input the value of var1:12 Input the value of var2:21 var1 is not equal to var2 var2 is greater than var1 C – else..if statementThe else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement. Syntax of else..if statement: if (condition1) { //These statements would execute if the condition1 is true } else if(condition2) { //These statements would execute if the condition2 is true } else if (condition3) { //These statements would execute if the condition3 is true } . . else { //These statements would execute if all the conditions return false. } Example of else..if statementLets take the same example that we have seen above while discussing nested if..else. We will rewrite the same program using else..if statements. #include Output: Input the value of var1:12 Input the value of var2:21 var1 is not equal to var2
Important Points: When you code an if statement within another if statement The statements?A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.
Can you put an if statement inside an if statement?You can place or nest an If statement inside another If statement. When you nest If statements, you can check for a condition only when another condition is found to be true.
What is the code inside an if statement called?That code is called the body of the if statement.
When an ifWhen an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
|