Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Dart Basics: Conditional Structures

Capítulo 21

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The switch statement in Dart evaluates an expression and compares its value with each case label. When a match is found, the corresponding code block is executed. This allows selecting one of many code blocks to execute, based on the evaluated expression.

Next chapter

Dart Basics: Looping Structures

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.