— Dart, Control Flow, Conditional Statements, Looping Statements — 2 min read
Conditional statements allow you to make decisions in your code based on certain conditions. The most common conditional statement in Dart is the if/else statement. Here's an example:
1int x = 5;2
3if (x > 10) {4 print('x is greater than 10');5} else {6 print('x is less than or equal to 10');7}
In this example, we're checking whether x
is greater than 10. If it is, we print "x is greater than 10". Otherwise, we print "x is less than or equal to 10".
Another type of conditional statement in Dart is the switch/case statement. This is useful when you have multiple conditions that you want to check. Here's an example:
1String fruit = 'banana';2
3switch (fruit) {4 case 'apple':5 print('This is an apple');6 break;7 case 'banana':8 print('This is a banana');9 break;10 default:11 print('I don\'t know what this is');12}
In this example, we're checking what type of fruit fruit
is. If it's an apple, we print "This is an apple". If it's a banana, we print "This is a banana". Otherwise, we print "I don't know what this is".
Looping statements allow you to repeat a block of code multiple times. The most common looping statement in Dart is the for loop. Here's an example:
1for (int i = 0; i < 5; i++) {2 print(i);3}
In this example, we're using a for loop to print out the numbers 0 through 4. The loop starts with i
equal to 0, and then repeats as long as i
is less than 5. Each time the loop runs, we print out the value of i
, and then increment i
by 1.
Another type of looping statement in Dart is the while loop. This allows you to repeat a block of code as long as a certain condition is true. Here's an example:
1int i = 0;2
3while (i < 5) {4 print(i);5 i++;6}
In this example, we're using a while loop to print out the numbers 0 through 4. The loop starts with i
equal to 0, and then repeats as long as i
is less than 5. Each time the loop runs, we print out the value of i
, and then increment i
by 1.
Finally, there's also the do/while loop, which is similar to the while loop, except that the code block is executed at least once, even if the condition is false. Here's an example:
1int i = 0;2
3do {4 print(i);5 i++;6} while (i < 5);
In this example, we're using a do/while loop to print out the numbers 0 through 4. The loop starts with i
equal to 0, and then repeats as long as i
is less than 5. Each time the loop runs, we print out the value of i
, and then increment i
by 1. Because we're using a do/while loop, the code block is executed at least once, even though i
starts off as less than 5.
Conditional and looping statements are important building blocks in any programming language, and Dart is no exception. By understanding how to use these statements, you can write more powerful and expressive code. In this article, we've looked at some of the most common conditional and looping statements available in Dart, including if/else, switch/case, forloops, while loops, and do/while loops. By using these statements effectively, you can make your code more efficient and easier to read.