Dart Conditional Stattements

| Page Views: 67

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.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post