else-if Statement in Dart
The else…if
statement is useful to test multiple conditions. Following is the syntax of the same.
- An
if
construct can contain any number ofelse if
blocks 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'); } }
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you show me the code that needs to be fixed?
What is wrong with the current code?
Can you give me a hint for fixing the code?
Awesome!
Completion rate improved to 4.55
else-if Statement in Dart
Desliza para mostrar el menú
The else…if
statement is useful to test multiple conditions. Following is the syntax of the same.
- An
if
construct can contain any number ofelse if
blocks 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'); } }
¡Gracias por tus comentarios!