Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære If, Else, and Else If Statements | Conditional Logic in Dart
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart Control Flow

bookIf, 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

main.dart

copy
12345678910111213
void 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

main.dart

copy
12345678910111213
void 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.

question mark

Which of the following statements about if, else if, and else in Dart is correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookIf, Else, and Else If Statements

Sveip for å vise menyen

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

main.dart

copy
12345678910111213
void 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

main.dart

copy
12345678910111213
void 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.

question mark

Which of the following statements about if, else if, and else in Dart is correct?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1
some-alt