Conditional Structures and Loops in Dart Programming Language


Conditional Structures and Loops in Dart Programming Language

The Dart programming language has rapidly increased its popularity, especially in mobile application development with Flutter. Dart’s powerful and easy-to-learn structure makes it attractive for both beginners and experienced developers. Conditional structures and loops, which are the cornerstones of programming languages, allow us to control the flow of applications as desired in Dart. In this article, we cover in detail conditional structures and loops in the Dart programming language, with examples.

Conditional Structures in Dart

In the Dart programming language, conditional structures allow the code to operate differently depending on different conditions. The most frequently used conditional structure is the if, else if, and else blocks. Also, you can conveniently check multiple conditions using the switch statement.

Using if-else


int sayi = 10;
if (sayi > 0) {
  print('Sayı pozitif');
} else if (sayi < 0) {
  print('Sayı negatif');
} else {
  print('Sayı sıfır');
}

In the example above, whether the number is positive, negative or zero is checked with a classic if-else structure in Dart.

Using switch-case


String gun = 'Pazartesi';
switch (gun) {
  case 'Pazartesi':
    print('Haftanın ilk günü');
    break;
  case 'Cuma':
    print('Haftanın son iş günü');
    break;
  default:
    print('Haftaiçi veya Haftasonu');
}

With switch-case, you can quickly execute different blocks depending on the state of a particular variable. Conditional structures in the Dart programming language form the basis of the decision-making mechanism.

Loops in Dart

With loops in the Dart programming language, you can write your code more efficiently and readably by repeating certain operations. Loop structures such as for, while, and do-while are used to iterate over collections and repeat operations under certain conditions.

For Loop


for (int i = 0; i < 5; i++) {
  print('Sayaç: $i');
}

In this example, the numbers from 0 to 4 are displayed on the screen with Dart. The logic of the loop and the use of the counter can be seen in the classic for loop.

while and do-while Loops


int sayac = 0;
while (sayac < 3) {
  print('while: $sayac');
  sayac++;
}

dint sayac2 = 0;
do {
  print('do-while: $sayac2');
  sayac2++;
} while (sayac2 < 3);

In the while loop, the code block runs repeatedly as long as the condition is met. The do-while loop first executes the code block and then checks the condition. The Dart programming language offers the ability to write flexible and clean code with these loop structures.

Conclusion

The Dart programming language enables you to develop powerful, dynamic, and readable applications with conditional structures and loops. Whether you are using Flutter or developing command-line applications with Dart; you can easily manage code flow with structures like if-else and switch-case, and repetitive operations with loops. By examining the code examples, you can easily use them in your own projects and benefit from these advantages of Dart.