Programming in Kotlin: Fundamentals

Aug 9 2022 · Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 1: Use Data Types & Operations

08. Challenge: Practice If Expressions & Boolean Logic

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 07. Branch with If Expressions & Scopes Next episode: 09. 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.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Booleans and if statements and expressions are really powerful. Which is why I've got an awesome challenge for you to practice them. In the startup project, you'll find your challenge. You have to create and validate user registration data, which consists of an email, a username, and a password. There's certain set of rules described in the project to verify if the data is valid or not. You have to use an if expression to define a message for the user. Depending on each of the rules, the data has to follow. Once you assign an appropriate message, you have to print if any of the rules have been broken or if the data is valid and the registration has been successful, that's it. You'll use the knowledge you've covered so far in this path to solve this challenge. So take your time and re-watch episodes for concepts If you have to. Now pause this video and solve the challenges, then once you're done play the video to compare your solution with mine. Good luck. First off, you have to create three values to hold the data like so. These describe the registration data. Then create a message constant and assign it to the following expression. The is empty function gives the returns true if the character sequence is empty. These cover the first two rules that properties can be empty and the username has to be at least six characters long. Next, let's cover the third rule. The password needs to be at least 10 characters and this will ensure that. Now for the fourth rule, enter the following. These are all the fill in cases ending with the email formats being wrong. Finally, add the successful case as the else block. Now add all the messages for each of the conditions. If all the, if and else if cases fail and the code reaches the else block it means that all the values follow the four rules for valid data and that our user can be registered. Run the project to see if that's the case with your data. We don't have any message printed out. And that is because I forgot to add a print statement. I'll add that now, like so. Then run the project once again. Cool, the success message is printed out. To make sure one of the negative cases works change the passwords to be just password. And see if you get a password error message by running the project. The case works as you can see an error message is printed out.