If, Else, and Else If Statements
Conditional branching allows you to control the flow of your Dart programs by making decisions based on conditions. The most common tools for this are the if, else if, and else statements. These statements let you check if certain conditions are true or false, and execute different blocks of code accordingly. A practical example is validating user input: you might want to check if a user's age falls within an acceptable range before allowing them to proceed. This approach ensures your program responds appropriately to different inputs and situations.
main.dart
12345678910111213void main() { int age = 17; if (age < 0) { print("Invalid age: age cannot be negative."); } else if (age < 18) { print("Sorry, you are too young to access this feature."); } else if (age > 120) { print("Invalid age: please enter a realistic age."); } else { print("Welcome! Your age is valid."); } }
In the code above, you see how branching logic works with if, else if, and else. The program checks the value of age in several steps. First, it tests if age is less than zero. If so, it prints a message about invalid input. If not, it moves to the next condition: is age less than 18? If this is true, it informs the user they are too young. The next branch checks if the age is unrealistically high. If none of these conditions match, the final else block runs, confirming the age is valid. Only one branch is executed, and the checks are performed in order from top to bottom.
main.dart
12345678910111213void main() { String role = "user"; if (role == "admin") { print("Welcome, admin! You have full access."); } else { if (role == "user") { print("Welcome, user! You have limited access."); } else { print("Hello, guest! Please sign up or log in."); } } }
When you use nested if-else statements, as in the user role example, it is important to keep your code readable. Avoid deep nesting whenever possible, as it can make your logic harder to follow and maintain. Consider grouping related conditions or using else if chains to simplify complex checks. Always use clear variable names and consistent indentation so that each branch of your logic is easy to understand at a glance.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Can you give me an example of how to use if-else statements in Dart?
What are some best practices for writing readable branching logic?
How do I avoid deep nesting in my conditional statements?
Чудово!
Completion показник покращився до 10
If, Else, and Else If Statements
Свайпніть щоб показати меню
Conditional branching allows you to control the flow of your Dart programs by making decisions based on conditions. The most common tools for this are the if, else if, and else statements. These statements let you check if certain conditions are true or false, and execute different blocks of code accordingly. A practical example is validating user input: you might want to check if a user's age falls within an acceptable range before allowing them to proceed. This approach ensures your program responds appropriately to different inputs and situations.
main.dart
12345678910111213void main() { int age = 17; if (age < 0) { print("Invalid age: age cannot be negative."); } else if (age < 18) { print("Sorry, you are too young to access this feature."); } else if (age > 120) { print("Invalid age: please enter a realistic age."); } else { print("Welcome! Your age is valid."); } }
In the code above, you see how branching logic works with if, else if, and else. The program checks the value of age in several steps. First, it tests if age is less than zero. If so, it prints a message about invalid input. If not, it moves to the next condition: is age less than 18? If this is true, it informs the user they are too young. The next branch checks if the age is unrealistically high. If none of these conditions match, the final else block runs, confirming the age is valid. Only one branch is executed, and the checks are performed in order from top to bottom.
main.dart
12345678910111213void main() { String role = "user"; if (role == "admin") { print("Welcome, admin! You have full access."); } else { if (role == "user") { print("Welcome, user! You have limited access."); } else { print("Hello, guest! Please sign up or log in."); } } }
When you use nested if-else statements, as in the user role example, it is important to keep your code readable. Avoid deep nesting whenever possible, as it can make your logic harder to follow and maintain. Consider grouping related conditions or using else if chains to simplify complex checks. Always use clear variable names and consistent indentation so that each branch of your logic is easy to understand at a glance.
Дякуємо за ваш відгук!