Dart Lesson 7 – Conditionals | Dataplexa

Conditional Statements in Dart

In this lesson, you will learn about conditional statements in Dart. Conditional statements allow a program to make decisions based on data.

They are used everywhere in real applications — checking user eligibility, validating payments, controlling access, and handling business rules.


What Are Conditional Statements?

Conditional statements execute different blocks of code depending on whether a condition is true or false.

In Dart, conditions are written using boolean expressions.


The if Statement

The if statement executes code only when a condition is true.

Example: Check if a customer is eligible for a discount.

int customerAge = 65;

if (customerAge >= 60) {
  print("Senior citizen discount applied");
}

If the customer’s age is 60 or above, the discount message is printed.


The if-else Statement

The if-else statement provides an alternative block of code when the condition is false.

Example: Validate whether a student has passed an exam.

int score = 42;

if (score >= 50) {
  print("Pass");
} else {
  print("Fail");
}

This logic is commonly used in grading systems.


The else if Ladder

When multiple conditions must be checked, else if is used.

Example: Assign grades based on marks.

int marks = 82;

if (marks >= 90) {
  print("Grade A");
} else if (marks >= 75) {
  print("Grade B");
} else if (marks >= 60) {
  print("Grade C");
} else {
  print("Grade D");
}

Only one condition is executed, starting from the top.


Using Logical Operators in Conditions

Conditions can include logical operators like && (AND) and || (OR).

Example: Check if a user can access premium content.

bool isLoggedIn = true;
bool isPremiumUser = false;

if (isLoggedIn && isPremiumUser) {
  print("Access granted");
} else {
  print("Upgrade required");
}

Both conditions must be true for access to be granted.


Nested if Statements

An if statement can be placed inside another if.

Example: Bank withdrawal validation.

double balance = 1200.0;
double withdrawalAmount = 500.0;

if (withdrawalAmount <= balance) {
  if (withdrawalAmount <= 1000) {
    print("Withdrawal approved");
  }
}

This ensures both balance and limit conditions are satisfied.


Ternary Operator

The ternary operator is a shorthand for simple if-else logic.

Syntax:

condition ? valueIfTrue : valueIfFalse
int items = 3;

String status = items > 0 ? "In Stock" : "Out of Stock";
print(status);

This is commonly used in UI labels and status messages.


📝 Practice Exercises


Exercise 1

Check if a user is eligible to vote based on age (18 or above).

Exercise 2

Print different shipping charges based on order amount.

Exercise 3

Use a ternary operator to check if a number is even or odd.


✅ Practice Answers


Answer 1

int age = 20;

if (age >= 18) {
  print("Eligible to vote");
} else {
  print("Not eligible");
}

Answer 2

double orderAmount = 750.0;

if (orderAmount >= 500) {
  print("Free shipping");
} else {
  print("Shipping charge applies");
}

Answer 3

int number = 9;

String result = number % 2 == 0 ? "Even" : "Odd";
print(result);

What’s Next?

In the next lesson, you will learn about loops in Dart.

Loops allow you to repeat conditional logic efficiently, which is essential for processing lists, data streams, and user input.