Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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'); } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 4.55

bookelse-if Statement in Dart

Pyyhkäise näyttääksesi valikon

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'); } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3
some-alt