Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara 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'); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 4.55

bookelse-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 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'); } }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3
some-alt