1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Android Studio, Kotlin, and Basic App Layouts

Branching If/Else/Else If

Start course
Overview
Difficulty
Beginner
Duration
3h 39m
Students
1
Description

This course begins by downloading Android studio - make sure you watch the appropriate video depending on whether you're on Mac or Windows. Then, we'll take a tour off the Android Studio interface and see how apps are put together. You'll learn about text views, buttons, and images to build a user interface for our app, and we'll also write some code to make our apps interactive.

Then we'll move onto a practical project in which we make a temperature converter app, which converts temperatures from Fahrenheit to Celsius. You'll be able to follow, building the app, and then running it on your system.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Hello and welcome back. In the last video, we built our WeatherChanger app but we left it in a scenario where if I click on the button right here, it changes the background image the first time but after that it stays there and nothing happens even as I keep clicking this button, okay? And that's because if you look at the code in our method, right here in this changeWeather method, all it does is it takes the view container that we have and once the button is clicked it changes the resource image right here to the sunny_image_2, that's it. So, when we click the button again it's just doing the same thing over and over again and the image is the sunny_image so nothing happens. 

So, in order to fix this, we have to do two things. We need a way to identify what is the image that is currently being displayed on the screen and also based on the image that's displayed, if it's one we need to switch it to the other one else if it's not that one, then we'll switch it back to the first one, all right? So, that if-else scenario I just mentioned is very common in programming and is known as branching. So, let's explore that in our Kotlin playground. I'll pull up a browser window with our Kotlin playground. Here we go and I've gotten rid of all the other code here except the main function and a couple of comments. In the comments I have if the image name is stormy image, change it to sunny_image. 

Else if the image name is sunny_image change it to stormy_image, this is what we are trying to do. So, first I'm going to declare a variable with the string stormy image. So, I'll say val myImageName = "stormy_image". Okay, and this is how you would write an if else condition or code block in Kotlin. You start with the keyword if then within parentheses you declare the condition or the test that you're looking for and what we are looking for is, is my image name stormy_image? That's the test that we're going to perform here. So, if (myImageName ==, that's two equals, this one equal sign is the assignment operator. You're setting the variable to something and the two equal signs is the equality operator where you're testing one side with the other. So, we want to see if my image name is equal to stormy_ image. Okay, if this is the case, then I want to do something. 

So, I'll start a code block right here open close curly braces. All right. So, if it is stormy_image, I simply want to println("Changing stormy_image to sunny_image"). That's what I want to do. And in the program, in Android Studio itself, we're going to actually change the image but here for this example, we can just have this print to the screen. So, if I run this, check it out, changing stormy_image to sunny_image. That's because my image name is stormy_image. So, this condition that I have here is true, which is why it entered this code block. If this condition was false, let's say it's not stormy_image, if this was sunny_image and if I ran this, nothing happens, right? Because this test proved false and it didn't enter this code block. 

So, how do we capture this scenario where my image name is sunny_image? We have to capture this other scenario where the image name is not stormy_image in another block and we can use what's called else. Okay, so this else will capture all other scenarios where my image name is not stormy_image. So, here I'll println("Changing sunny_image back to stormy_image") for now. All right, now that my image name is sunny_image, if I do run this, check it out, it says changing sunny_image back to stormy_image. There you go. There is an if statement, there's a test. If the test fails, it goes to the else part of my code block and prints this to the screen. So, that's the general logic and this is what's known as branching, and at a very basic level. 

There are other fun additions to this that we can add as well. For example, what if the image name accidentally got changed to something irrelevant? So, there are other possibilities other than just sunny_image or stormy_image and we want to capture that as well. What do we do then? Then we'd want an additional if or what's known as else if where we check for if the image name is sunny_image and we can do that easily by adding else if. So, else if, then within parentheses we will enter in the condition, we'll say {myImageName == "sunny_image") If that's the case, then we want to print this ("changing sunny_image back to stormy_image"). So, now we have an additional test here. 

The first test, the second test, and then we can do an else after that to capture all other scenarios where the image name is not sunny or stormy_image. So, here I'll print something else and say "Incorrect image name, app crashed". Okay, so if, else if, and then else. So, what if I change this to, whoops, and then 'Run', check it out; incorrect image name, app crashed. All right. So, that's how you implement branching in Kotlin using if, else if, else code blocks or conditions and you could have multiple else if conditions as well within the block. And this is also known as controlling the execution flow of your program. 

You're basically executing pieces of code based on if the condition that they're in is true and if it isn't then it won't execute that piece of code and it will move on and you may be thinking, okay, what if you have multiple relative conditions and a few of them are turned out to be true? In that case, only the first condition, the first test that turns to be true, it executes the code in that code block, then it exits the if else code block. It doesn't execute any of the other true conditions. So, the first true condition that's met, that's it. All right. So we're going to leave it at that for our initial look at branching for now and in the next video, we'll use what we have learned here in our WeatherChanger project and make it work in the way that we want. See you there.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics