Controlling the flow of your code is important. Programmers want to control the logic of their code for many reasons; one of the main reasons is that the user of the software should have many options open to them.
You may not know the conditions beforehand, in which way your programming logic should move, though.
You can only guess, so as a developer, you should open as many avenues for the user as possible. There are several techniques you can adopt to control the flow of the code. For example, the if-else logic is popular.
In this tutorial, you will learn about the Dart if else if Statement and its application with practical examples.
Dart if else if Statement
In Dart, if..else..if statement allows us to add an alternative set of test conditions in if..else statement using else-if and single else statements for if condition.
In such way if..else..if statement is used to select one among several blocks of code to be executed.
Let’s look at a simple example of controlling the flow of the code. After that, we will delve deep into the logical consequences of this approach.
An if can be followed by else if the Boolean statement tested by the if block comes out as false.
if(it is true){
The program executes
}
Else {
This block will not execute then
}
Let see a working Example on a sample If and Else block and nested if..else example.
void main()
{
var a = 10;
var b = 10;
print("NintyZero - Dart Example if else if Statement");
if(a > b){
print("a is greater than b");
}
else if(a == b){
print("a and b are equal");
}
else {
print("b is greater than a");
}
}
Conclusion:
In this tutorial we learnt how to implement conditional blocks in Dart using If.Else blocks and also tried a nested way to do it.