Paramclasses Academy - Blog

Tutorials Details

←
Post 6 of 40
→

Dart Operators, Conditions and Loops with Practical Examples | Param Classes

Dart Operators, Conditions and Loops with Practical Examples | Param Classes

November 2, 2026

❖ Dart Operators, Conditions and Loops with Practical Examples

✦ Welcome to Param Classes!

When you start learning Dart programming for Flutter app development, three important concepts you must understand are Operators, Conditions, and Loops.

Operators help you perform calculations and compare values. Conditions help your program make decisions. Loops allow you to repeat a task without writing the same code again and again.

For example, imagine you are creating a student management application. You need operators to calculate marks, conditions to check whether a student has passed or failed, and loops to display multiple student records.

In this practical tutorial by Param Classes, we will learn these concepts step by step with beginner-friendly Dart code examples.

✦ 1. What Are Operators in Dart?

Operators are special symbols that perform operations on values and variables.

For example:

void main() {
  int a = 10;
  int b = 5;

  print(a + b);
}

Output:

15

Here, the + operator adds two numbers.

Dart provides different types of operators.

✦ 2. Arithmetic Operators in Dart

Arithmetic operators are used to perform mathematical calculations.

Operator Meaning Example
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2.0
~/ Integer division 10 ~/ 3 = 3
% Modulus (remainder) 10 % 3 = 1

Practical Example:

void main() {
  int a = 20;
  int b = 10;

  print("Addition: ${a + b}");
  print("Subtraction: ${a - b}");
  print("Multiplication: ${a * b}");
  print("Division: ${a / b}");
  print("Integer Division: ${a ~/ b}");
  print("Remainder: ${a % b}");
}

Output:

Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2.0
Integer Division: 2
Remainder: 0

The modulus operator % is especially useful when checking whether a number is even or odd.

✦ 3. Relational Operators in Dart

Relational operators compare two values and return a Boolean result: true or false.

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example:

void main() {
  int age = 20;

  print(age == 20);
  print(age != 18);
  print(age > 18);
  print(age < 25);
  print(age >= 20);
  print(age <= 30);
}

Output:

true
true
true
true
true
true

These operators are commonly used in login systems, registration forms, marksheets, and other applications.

✦ 4. Logical Operators in Dart

Logical operators combine or reverse conditions.

Dart provides three commonly used logical operators:

Operator Meaning
&& AND
|| OR
! NOT

Example: AND Operator

Both conditions must be true for the result to be true.

void main() {
  int age = 22;
  bool hasId = true;

  if (age >= 18 && hasId == true) {
    print("Entry Allowed");
  } else {
    print("Entry Denied");
  }
}

Output:

Entry Allowed

Example: OR Operator

At least one condition must be true.

void main() {
  bool hasEmail = false;
  bool hasMobile = true;

  if (hasEmail || hasMobile) {
    print("Registration Allowed");
  } else {
    print("Enter Email or Mobile");
  }
}

Output:

Registration Allowed

Example: NOT Operator

The NOT operator reverses a Boolean value.

void main() {
  bool isLoggedIn = false;

  print(!isLoggedIn);
}

Output:

true

✦ 5. Assignment Operators in Dart

Assignment operators assign values to variables or update their existing values.

Operator Example Meaning
= a = 10 Assign value
+= a += 5 Add and assign
-= a -= 5 Subtract and assign
*= a *= 2 Multiply and assign
/= a /= 2 Divide and assign
~/= a ~/= 2 Integer divide and assign

Example:

void main() {
  int amount = 100;

  amount += 50;
  print(amount);

  amount -= 20;
  print(amount);

  amount *= 2;
  print(amount);
}

Output:

150
130
260

✦ 6. What Are Conditions in Dart?

Conditions allow a program to make decisions based on whether an expression is true or false.

For example, a student receives a pass result if their marks are 33 or higher.

Dart supports if, if-else, else if, and switch statements.

❖ A. if Statement

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

void main() {
  int age = 20;

  if (age >= 18) {
    print("You are eligible to vote");
  }
}

Output:

You are eligible to vote

❖ B. if-else Statement

The if-else statement executes one block when the condition is true and another block when it is false.

void main() {
  int marks = 75;

  if (marks >= 33) {
    print("Student Passed");
  } else {
    print("Student Failed");
  }
}

Output:

Student Passed

❖ C. else-if Statement

Use else if when you need to check multiple conditions.

Example: Student Grade System

void main() {
  int marks = 85;

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

Output:

Grade A

The program checks the conditions from top to bottom. When it finds a true condition, it executes that block.

❖ D. switch Statement

A switch statement is useful when you want to compare one value against multiple possible cases.

Example: Day Selection

void main() {
  int day = 3;

  switch (day) {
    case 1:
      print("Monday");
      break;

    case 2:
      print("Tuesday");
      break;

    case 3:
      print("Wednesday");
      break;

    case 4:
      print("Thursday");
      break;

    default:
      print("Invalid Day");
  }
}

Output:

Wednesday

The break statement ends the selected case. The default block runs when no case matches.

✦ 7. What Are Loops in Dart?

A loop is used to execute a block of code repeatedly.

Suppose you want to print numbers from 1 to 100. Writing 100 separate print statements would take too much time.

Instead, you can use a loop.

Dart provides several looping options, including:

  • for loop

  • while loop

  • do-while loop

  • for-in loop

✦ 8. for Loop in Dart

A for loop is useful when you know how many times you want to repeat an operation.

Example: Print Numbers from 1 to 5

void main() {
  for (int i = 1; i <= 5; i++) {
    print(i);
  }
}

Output:

1
2
3
4
5

Explanation:

  • int i = 1 initializes the counter.

  • i <= 5 checks the loop condition.

  • i++ increases the counter by one.

  • print(i) displays the current number.

✦ 9. while Loop in Dart

A while loop repeats a block of code as long as its condition remains true.

Example:

void main() {
  int i = 1;

  while (i <= 5) {
    print(i);
    i++;
  }
}

Output:

1
2
3
4
5

Here, the loop first checks whether i is less than or equal to 5. If true, it prints the number and increases the value.

Important: Always update the loop variable when necessary. Otherwise, you may accidentally create an infinite loop.

✦ 10. do-while Loop in Dart

A do-while loop executes its code block at least once because the condition is checked after the block runs.

Example:

void main() {
  int i = 1;

  do {
    print(i);
    i++;
  } while (i <= 5);
}

Output:

1
2
3
4
5

Even if the condition is initially false, the do block runs once.

✦ 11. for-in Loop in Dart

The for-in loop is useful when you want to access each item in a collection, such as a List.

Example: Display Student Names

void main() {
  List<String> students = [
    "Rahul",
    "Amit",
    "Priya",
    "Neha"
  ];

  for (String name in students) {
    print(name);
  }
}

Output:

Rahul
Amit
Priya
Neha

The loop visits each item in the List and stores the current item in the name variable.

✦ 12. break and continue Statements

Dart also provides break and continue to control loop execution.

A. break Statement

The break statement stops the loop immediately.

void main() {
  for (int i = 1; i <= 10; i++) {
    if (i == 5) {
      break;
    }

    print(i);
  }
}

Output:

1
2
3
4

B. continue Statement

The continue statement skips the current iteration and moves to the next one.

void main() {
  for (int i = 1; i <= 5; i++) {
    if (i == 3) {
      continue;
    }

    print(i);
  }
}

Output:

1
2
4
5

Here, the number 3 is skipped.

✦ 13. Practical Project: Simple Dart Calculator

Now let's combine operators and conditions to create a simple calculator.

void main() {
  double num1 = 20;
  double num2 = 10;

  String operator = "+";

  if (operator == "+") {
    print("Result: ${num1 + num2}");
  } else if (operator == "-") {
    print("Result: ${num1 - num2}");
  } else if (operator == "*") {
    print("Result: ${num1 * num2}");
  } else if (operator == "/") {
    if (num2 != 0) {
      print("Result: ${num1 / num2}");
    } else {
      print("Cannot divide by zero");
    }
  } else {
    print("Invalid Operator");
  }
}

Output:

Result: 30.0

How does this program work?

  1. Two numbers are stored in variables.

  2. The selected operator is stored in a String.

  3. The if-else conditions check which operator was selected.

  4. The appropriate mathematical calculation is performed.

  5. The result is displayed using print().

This is a simple example of how programming logic works in real applications.

✦ 14. Practice Questions for Beginners

Try these exercises yourself using DartPad.

  1. Write a Dart program to check whether a number is even or odd.

  2. Write a program to find the largest of two numbers.

  3. Create a program to check whether a student has passed or failed.

  4. Print numbers from 1 to 20 using a for loop.

  5. Print the multiplication table of 5.

  6. Calculate the sum of numbers from 1 to 10.

  7. Display all names stored in a List using a for-in loop.

  8. Use a switch statement to display a month name based on its number.

Practising these questions will improve your understanding of Dart programming logic.

✦ Conclusion

In this tutorial, we learned about arithmetic, relational, logical, and assignment operators. We also explored conditional statements such as if, if-else, else-if, and switch.

Next, we practised for, while, do-while, and for-in loops. Finally, we created a simple calculator using operators and conditions.

These concepts are important for every Flutter developer because they help you build application logic, validate user input, calculate values, and manage repeated operations.

At Param Classes, our goal is to help beginners learn programming through practical examples and step-by-step tutorials. Keep practising Dart, because a strong programming foundation will make your Flutter learning journey easier.

Learn Today, Grow Tomorrow — Param Classes!

←
Post 6 of 40
→