else-if Statement in Dart
The else…if statement is useful to test multiple conditions. Following is the syntax of the same.
- An
ifconstruct can contain any number ofelse ifblocks of code; - It is not necessary to use the code block
else.
if (first_expression)
{
}
else if (second_expression)
{
}
else
{
}
The else if statement works like if: it has a condition in () and a code block in {}. Once a condition is true and its block runs, the rest of the conditions are skipped.
main.dart
12345678910111213141516void main() { int num = 2; if(num > 0) { print("is positive"); } else if(num < 0) { print("is negative"); } else { print("is zero"); } }
The program defines a variable num with the value 2. It checks if num is greater than 0; if so, it prints "is positive". If num is less than 0, it prints "is negative". Otherwise, when num equals 0, it prints "is zero".
Task
Fix the code so that the program works correctly.
main.dart
12345678910111213141516void main() { var score = '2.0'; if(score is int) { print('Type: int'); } ___ (___) { print('Type: double'); } else { print('Type: other type'); } }
else if (score is double) - correct condition
main.dart
12345678910111213141516void main() { var score = '2.0'; if(score is int) { print('Type: int'); } else if (score is double) { print('Type: double'); } else { print('Type: other type'); } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 4.55
else-if Statement in Dart
Scorri per mostrare il menu
The else…if statement is useful to test multiple conditions. Following is the syntax of the same.
- An
ifconstruct can contain any number ofelse ifblocks of code; - It is not necessary to use the code block
else.
if (first_expression)
{
}
else if (second_expression)
{
}
else
{
}
The else if statement works like if: it has a condition in () and a code block in {}. Once a condition is true and its block runs, the rest of the conditions are skipped.
main.dart
12345678910111213141516void main() { int num = 2; if(num > 0) { print("is positive"); } else if(num < 0) { print("is negative"); } else { print("is zero"); } }
The program defines a variable num with the value 2. It checks if num is greater than 0; if so, it prints "is positive". If num is less than 0, it prints "is negative". Otherwise, when num equals 0, it prints "is zero".
Task
Fix the code so that the program works correctly.
main.dart
12345678910111213141516void main() { var score = '2.0'; if(score is int) { print('Type: int'); } ___ (___) { print('Type: double'); } else { print('Type: other type'); } }
else if (score is double) - correct condition
main.dart
12345678910111213141516void main() { var score = '2.0'; if(score is int) { print('Type: int'); } else if (score is double) { print('Type: double'); } else { print('Type: other type'); } }
Grazie per i tuoi commenti!