Else if ladder is a type of conditionals in the dart. Just like if and if-else. We use else if ladder when we want to check multiple conditions.
Syntax
if (boolean_expression) {
//Statements to be executed if the condition is true
} else if (boolean_expression) {
//Statements to be executed if the condition is true
} else if (boolean_expression) {
//Statements to be executed if the condition is true
} else {
//Statements to be executed if all the conditions are false
}
If the Boolean expression evaluates to be true, then the block of code inside the if statement will be executed. Otherwise, the compiler will check else-ifās condition.
Once any else-ifās condition is true, then all other else-if conditions will be skipped. So every else-if condition must be unique.
If all the else-if conditions evaluate to be false, then the elseās code block will be executed.
Here again, the else is optional. But if it occurs then it must be after all the else-if blocks.
Sample Code
void main() {
int number = 2;
if (number == 5) {
print("Number is 5");
} else if (number == 1) {
print("Number is 1");
} else if (number == 2) {
print("Number is 2");
} else {
print("None of the number is matched");
}
}
Output
Number is 2
Thatās all for else if ladder statement guys. Feel free to share me if I miss something. Iāll love to learn it from you. Till Then Keep Loving, Keep Coding. Iāll surely catch you up in the next article.
Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Donāt feel hesitate that you canāt do that or you donāt know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.
For More Information Please Visit Following Links
Jai Hind, Vande Mataram š®š³
Wanna get in touch with me? Here are links. Iāll love to become your friend. š
- My Site
- My Blogs
- Medium
- Github
- Stack overflow or just mail me at jd.tillu@gmail.com