3.3. Dart Basics: Conditional Structures

Página 21

3.3. Dart Basics: Conditional Structures

Dart is a modern programming language, developed by Google, that is used to build mobile, web, and desktop applications. It's the main language used in Flutter, Google's popular mobile app development framework. In this section, we'll cover a fundamental aspect of Dart programming: conditional structures.

Conditional Structures

Conditional structures are a fundamental concept in any programming language. They allow the program to make decisions based on certain conditions. In Dart, we have several conditional structures available, including 'if', 'else if', 'else', 'switch' and 'case'.

If, Else If, ​​and Else

The 'if' statement is the most basic conditional structure. It checks whether a condition is true and, if so, executes a block of code. For example:

int x = 10;
if (x > 5) {
  print('x is greater than 5');
}

Here, the program checks whether 'x' is greater than 5. If so, it prints 'x is greater than 5'.

The 'else if' statement is used to check multiple conditions. If the condition in the 'if' statement is false, the program checks the condition in the 'else if' statement. If this condition is true, the program executes the corresponding block of code. For example:

int x = 10;
if (x > 20) {
  print('x is greater than 20');
} else if (x > 10) {
  print('x is greater than 10');
} else if (x > 5) {
  print('x is greater than 5');
}

Here, the program first checks if 'x' is greater than 20. Since 'x' is 10, this condition is false, so the program moves on to the next condition. It checks if 'x' is greater than 10. Again, this condition is false, so the program moves on to the next condition. It checks if 'x' is greater than 5. As this condition is true, the program prints 'x is greater than 5'.

The 'else' statement is used as a kind of "last resort". If all conditions in the 'if' and 'else if' statements are false, the program executes the block of code in the 'else' statement. For example:

int x = 10;
if (x > 20) {
  print('x is greater than 20');
} else if (x > 10) {
  print('x is greater than 10');
} else {
  print('x is 10 or less');
}

Here, all the conditions in the 'if' and 'else if' statements are false, so the program prints 'x is 10 or less'.

Switch and Case

The 'switch' statement is used to select one of many blocks of code to be executed. The 'switch' statement evaluates an expression and compares the value of the expression with the value of each 'case' statement. If there is a match, the associated block of code is executed.

int x = 2;
switch(x){
  case 1:
    print('x is 1');
    break;
  case 2:
    print('x is 2');
    break;
  default:
    print('x is neither 1 nor 2');
}

Here, the program checks whether 'x' is equal to 1. If so, it prints 'x is 1'. If not, check if 'x' is equal to 2. If so, print 'x is 2'. If 'x' is neither 1 nor 2, the program prints 'x is neither 1 nor 2'.

In summary, conditional statements are an essential part of Dart programming. They allow the program to make decisions based on certain conditions, which is critical for creating complex and interactive applications.

Now answer the exercise about the content:

Which of the following statements correctly describes the use of the 'switch' statement in the Dart programming language?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

223.4. Dart Basics: Looping Structures

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text