Dart Programming – If Else Statement (if , if..else, Nested if, if-else-if)
Decision-making statements allow programmers to determine which statements should be executed under different conditions. There are four primary types of decision-making statements:
- if Statement
- if-else Statement
- else-if Ladder
- Nested if Statement
if Statement
This type of statement simply checks the condition and if it is true then the statements within it are executed but if it is not then the statements are simply ignored in the code.
Syntax:
if ( condition ){
// body of if
}
Illustration with Image:

Example:
void main()
{
int gfg = 10;
// Condition is true
if (gfg > 3) {
// This will be printed
print("Condition is true");
}
}
Output:
Condition is true
if…else Statement
This type of statement simply checks the condition and if it is true, the statements within are executed but if not then else statements are executed.
Syntax:
if ( condition ){
// body of if
}
else {
// body of else
}
Illustration with Image:

Example:
void main()
{
int gfg = 10;
// Condition is false
if (gfg > 30) {
// This will not be printed
print("Condition is true");
}
else {
// This will be printed
print("Condition id false");
}
}
Output:
Condition is false
else…if Ladder
This type of statement simply checks the condition and if it is true the statements within it are executed but if it is not then other if conditions are checked, if they are true then they are executed, and if not then the other if conditions are checked. This process is continued until the ladder is completed.
Syntax:
if ( condition1 ){
// body of if
}
else if ( condition2 ){
// body of if
}
.
.
.
else {
// statement
}
Illustration with Image:

Example:
void main()
{
int gfg = 10;
if (gfg < 9) {
print("Condition 1 is true");
gfg++;
}
else if (gfg < 10) {
print("Condition 2 is true");
}
else if (gfg >= 10) {
print("Condition 3 is true");
}
else if (++gfg > 11) {
print("Condition 4 is true");
}
else {
print("All the conditions are false");
}
}
Output:
Condition 3 is true
Nested if Statement
This type of statement checks the condition and if it is true then the if statement inside it checks its condition and if it is true then the statements are executed otherwise else statement is executed.
Syntax:
if ( condition1 ){
if ( condition2 ){
// Body of if
}
else {
// Body of else
}
}
Illustration with Image:

Example:
void main()
{
int gfg = 10;
if (gfg > 9) {
gfg++;
if (gfg < 10) {
print("Condition 2 is true");
}
else {
print("All the conditions are false");
}
}
}
Output:
All the conditions are false
Try code of if-else with Operator:
void main() {
int x = 5, y = 7;
// (++x > y--) → x=6, y=7 (false), so (++x < ++y) is skipped
if ((++x > y--) && (++x < ++y)) {
print("Condition true");
} else {
// Executes since first condition is false
print("Condition false");
}
// Final values after evaluation
// 6 (incremented once)
print(x);
// 6 (decremented once)
print(y);
}
Output:
Condition false
6
6
Conclusion
In programming, decision-making statements are crucial because they help dictate how a program runs based on certain conditions. There are four main types of these statements:
- if Statement: This runs a piece of code only when a specific condition is true.
- if-else Statement: This allows you to choose between two paths: it executes one block of code if the condition is true and another if it’s false.
- else-if Ladder: This is useful for checking multiple conditions one after the other, helping to streamline choices.
- Nested if Statement: This is where you put an if statement inside another if statement, which is great for handling more complex conditions.
These decision-making structures are essential for creating logical flows in programs, ensuring they execute efficiently and respond appropriately to different situations.