Previous episode: 06. Set Conditional Values
Next episode: 08. Conclusion
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Okay, we've done a lot of work covering logical operators. It's time to put your knowledge to the test. In this episode, we have two challenges. We'll start with the first one. First, I want you to define two variables. One should be a constant called my age and you should set it to your age. The other should be a string variable called message that you'll print out to the console. Now, if my age is greater or equal than 13 or less than equal than 18, you should set the message to you are a teenager. Otherwise set the message to not a teenager. Then print out the message. Pause the video now and try it out. (upbeat music) How'd your challenge go? Let's review it now. First, we'll start by creating some variables. First, define your age in a message. Here we defined my age, as well as a message. Remember, if we don't set a value when we define a variable, we must provide the type. Otherwise, dart will set the type to be dynamic. Let's define our logic. We add the expression, checking both ages. Then we set the message. Now let's print the message. Let's run on DartPad and you'll see that I am definitely not a teenager. Okay, that was the first challenge. The second challenge is to write this code so that it sets the code using a ternary operator. That is, write it out in one line. Pause the video now and give it a shot. (upbeat music) How'd that one go? Let's do this now. Let's create a new message. Next, we add our expression. Now we set the result in the case that the expression turns out to be true. Now let's add a string for the false condition. Let's print it out. And that's it. If we run, we get the same result, except this time we've written it on one line. Awesome job.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.