Dart Conditional Stattements

| Page Views: 16

Dart Conditional Statements Tutorial and Examples.

Let's look at various conditional statements in dart programming language.

1. Simple If Statement

Here's a simple if statement in Dart:

main() {
  num count = 15;

  if (count % 5 == 0) {
    print('${count} is divisble by 5');
  }
}

Result:

15 is divisble by 5

2. If-Else Statements

Here's an if - else statement example in Dart:

main() {
  num count = 15;

  if (count % 2 == 0) {
    print('${count} is an even number.');
  } else {
    print('${count} is an odd number');
  }
}

Result:

15 is an odd number

3. Ternary Operators

Here's a ternary operator example in dart:

main() {
  var isMarried = true;
  var answer = isMarried ? 'Yes' : 'No';
  print('Is James Married? : $answer');
}

Result:

Is James Married? : Yes

4. Switch Statement

Dart also has switch statements:

Here's an example:

main() {
  var travel = "INTER-PLANETARY";

  switch(travel) {
    case 'PLANETARY':
      print('ATTAINABLE - EASY');
      break;
    case 'INTER-PLANETARY':
      print('ATTAINABLE - FAIR');
      break;
    case 'INTER-STELLAR':
      print('ATTAINABLE - DIFFICULT');
      break;
    case 'INTER-GALACTIC':
      print('MAYBE - ALMOST IMPOSSIBLE');
      break;
    case 'INTER-UNIVERSE':
      print('NEVER - BEYOND HUMANITY');
      break;      
    default:
      print('NONE');
  }

}

Result:

ATTAINABLE - FAIR

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post