The if-else statement is the most basic of all the control flow statements. It invokes your program to execute a specific section of a code only if a particular test proves to be true.
The following figure displays the work of the IF - Else statement:
Example:
This example gives an idea of how if-else statement works in an easy way:
In the Dart program below, when the program starts, the value of the x variable =10.
In the next line, the "if" statement will work using the greater than condition. After the Dart compiler checks whether this condition is true or not, Dart compiler will directly run the next line of code. However, if the condition is proved not true, Dart compiler will continue to run the code lines under the Else statement.
main() {
int x=10;
if(x>30){
print("Hello, I am If statement running now ......");}
else
{
print("Hello, I am Else statement running now ......");}
}
The run result of this code follows:
* This topic is a part of lesson 2 of Flutter Application Development course. For more information about this course, click here.