
Understanding Nested If-Else Statements
Explore the concept of nested if-else statements in programming, including examples and best practices for handling mutually exclusive cases. Learn how to structure if-if and else-else-if statements effectively. Discover the versatility of using nested if-else blocks for different scenarios, from handling discrete cases to sequences of numerical values.
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Here is a model for an if if statement with multiple mutually exclusive alternatives: if(condition1) { } else if(condition2) { } else { }
Any number of else if It can be a useful programming practice to end with a simple else This final else the if ifs and else if Sometimes there is no separate statement or block of statements that needs to be executed in the default case. else ifs can be used. else. else is the default case if none of else ifs hold true.
If so, then the else immediately by a semicolon or by a pair of braces containing nothing. This may seem redundant, but it can serve as a helpful reminder that the default case, after having checked all previous cases, is to do nothing. else can be followed
This structure can be used for a mutually exclusive set of discrete cases as illustrated by the following: if(x == 1) { // do something } else if(x == 2) { // do something else } else { // do yet something else if x is // neither 1 nor 2 }
Notice that it is also useful for handling sequences of numerical values, either ascending or descending: if(x <= 1.0) { // do something } else if(x <= 2.0) { // executed for values of x > 1.0 and <= to 2.0 } else if(x <= 3.0) { // executed for values of x > 2.0 and <= to 3.0 } else { // executed for values of x > 3.0 }
In general there are no restrictions on where if if statements can appear in program code. They can occur within the pair of braces delimiting any valid block. In particular, an if if statement and its else else if of code that belongs to another if if, else else if When this occurs it is known as a nested if if. else or else if statements can appear within a block else, or else if statement.
The nesting refers to the pattern of pairs of matched braces occurring within other pairs of matched braces. For ease of reading, it is recommended that each pair be indented one tab from the pair that contains it. The model when nested only one level deep would look like this:
if(condition1) { if(condition2) { statement1 is executed if condition1 is true and condition2 is true. statement2 is executed if condition1 is true and condition2 is not true. statement3 is executed if condition1 is not true and condition3 is true. statement4 is executed if condition1 is not true and condition3 is not true. statement1; } else { statement2; } } else { if(condition3) { statement3; } else { statement4; } }