Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära if...else Statement in Dart | Conditional Statements
Introduction to Dart

bookif...else Statement in Dart

This image illustrates the basic structure of a conditional statement in programming:

First, the code runs and a condition is checked. If the condition is true, the if block executes; if false, the else block runs. After that, the program continues with the next part of the code, allowing it to make decisions based on conditions.

if (condition)
{
    // Code block `if` condition is `true`
}
else
{
    // Code block `if` condition is `false`
}

An if can be followed by an optional else block. The else block will execute if the Boolean expression tested by the if block evaluates to false.

main.dart

main.dart

copy
123456789101112
void main() { int age = 17; if (age >= 18) { print("You're an adult."); } else { print("You're not an adult yet."); } }

In the example above, the age < 18, so the if code block hasn't been executed. The else code block executes when the if condition is false. The else syntax is like the if syntax without a condition and parentheses ( ).

You can also write a condition for the if statement to check whether the variable is of type int by using is operator.

main.dart

main.dart

copy
123456789101112
void main() { var num = 7.0; if(num is int { print('Type: int'); } else { print('Type: other type'); } }
question mark

Which statement best describes how the if...else statement works?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain how the `is` operator works in Dart?

Can you give an example of using an `if` statement with the `is` operator?

What happens if there is no `else` block after an `if` statement?

Awesome!

Completion rate improved to 4.55

bookif...else Statement in Dart

Svep för att visa menyn

This image illustrates the basic structure of a conditional statement in programming:

First, the code runs and a condition is checked. If the condition is true, the if block executes; if false, the else block runs. After that, the program continues with the next part of the code, allowing it to make decisions based on conditions.

if (condition)
{
    // Code block `if` condition is `true`
}
else
{
    // Code block `if` condition is `false`
}

An if can be followed by an optional else block. The else block will execute if the Boolean expression tested by the if block evaluates to false.

main.dart

main.dart

copy
123456789101112
void main() { int age = 17; if (age >= 18) { print("You're an adult."); } else { print("You're not an adult yet."); } }

In the example above, the age < 18, so the if code block hasn't been executed. The else code block executes when the if condition is false. The else syntax is like the if syntax without a condition and parentheses ( ).

You can also write a condition for the if statement to check whether the variable is of type int by using is operator.

main.dart

main.dart

copy
123456789101112
void main() { var num = 7.0; if(num is int { print('Type: int'); } else { print('Type: other type'); } }
question mark

Which statement best describes how the if...else statement works?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
some-alt