LogoDart Beginner Tutorials

Pet Status Report Conditional Logic and Output Formatting

We will use `if/else` statements and print with string interpolation to build a simple status display showing the pets hunger and happiness levels.

Welcome to Part 3! In the previous parts, you learned how to create a basic Dart program and take user input to name your digital pet. Now, let's add some more sophisticated features to monitor your pet's well-being. We'll learn about conditional logic (if/else statements) and enhance our output using string interpolation.

Step 1: Adding Happiness – Another Pet Attribute#

Just like hunger, our pet needs happiness! Let's add another int variable to track this vital stat. Add the following line below petHunger in your main function:

int petHappiness = 7; // Initially, the pet is fairly happy (scale of 0-10)

We'll use the same 0-10 scale as hunger.

Step 2: Introducing if/else – Checking Pet's Mood#

Let's use if/else statements to check the pet's hunger and happiness levels and provide feedback. Add the following code after you've set petHunger and petHappiness:

if (petHunger > 7) {
  print('$petName is very hungry!');
} else if (petHunger > 3) {
  print('$petName is a little hungry.');
} else {
  print('$petName is not hungry.');
}

if (petHappiness < 3) {
  print('$petName is sad.');
} else if (petHappiness < 7) {
  print('$petName is okay.');
} else {
  print('$petName is happy!');
}

This code uses if/else if/else to check different ranges of hunger and happiness and prints different messages accordingly.

Step 3: Enhancing Output with String Interpolation#

Let's make our output more informative and visually appealing using string interpolation. Replace the existing print statements with the following:

print('${petName}\'s Hunger: $petHunger');
print('${petName}\'s Happiness: $petHappiness');

Notice the use of \`` to escape the apostrophe in petName` and how the values are seamlessly integrated into the strings.

Step 4: Putting it All Together#

Here's the complete main function with all the changes:

import 'dart:io';

void main() {
  // ... (Your code for getting petName remains the same) ...

  int petHunger = 5;
  int petHappiness = 7;

  print('${petName}\'s Hunger: $petHunger');
  print('${petName}\'s Happiness: $petHappiness');

  if (petHunger > 7) {
    print('$petName is very hungry!');
  } else if (petHunger > 3) {
    print('$petName is a little hungry.');
  } else {
    print('$petName is not hungry.');
  }

  if (petHappiness < 3) {
    print('$petName is sad.');
  } else if (petHappiness < 7) {
    print('$petName is okay.');
  } else {
    print('$petName is happy!');
  }
}

Run your code! You should now see a more detailed report on your pet's condition.

What's Next?#

In the next part, "Playing with Your Pet: Functions, Asynchronous Operations, and Null Safety," we'll make our pet simulator more dynamic by introducing functions, simulating delays using async/await, and handling potential null values with nullable types. Get ready for some fun interactive elements!