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

bookelse-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 of else 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

main.dart

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

main.dart

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

main.dart

copy
12345678910111213141516
void 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'); } }
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

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

bookelse-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 of else 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

main.dart

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

main.dart

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

main.dart

copy
12345678910111213141516
void 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'); } }
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
some-alt