Android & Kotlin Development Masterclass – Full Course

freeCodeCamp.org · Beginner ·🔧 Backend Engineering ·1y ago

Key Takeaways

This comprehensive course covers Android and Kotlin development, including hands-on practice with building multiple real-world applications, such as an Uber clone, using various tools and technologies like Firestore, SQLite, MVVM, Retrofit, Navigation Component, and Jetpack Compose.

Full Transcript

This massive Cotlin and Android course takes you from the fundamentals to advanced concepts with hands-on practice. Throughout this course for beginners, Alexandro Christian will teach you to build multiple realworld applications, including an impressive Uber clone that demonstrates professional-grade development techniques. Master everything from Cotlin syntax and Android architecture to UI design, database integration and API implementation as you create portfoliow worthy projects. So do you want to become an Android developer? Then you've come to the right course. Becoming an Android developer takes time and effort and you need to learn a lot of things and learning them separate is very hard. This is why I created this course. With this course, I put together all the knowledge needed to become a confident Android developer. My name is Alex and I started coding when I was 18. For who is this course? This course is for anybody who wants to become an Android developer. We learn everything from zero by downloading the software we need to writing our first line of cotling code to building our very first app. No programming experience is needed. Cotlin is perfect for coding because it's very intuitive and concise. So let's get started. But before we get started, let me first show you what apps are going to build in this course. In this course, you'll learn how to build Android apps using Cotlin. And after finishing this course, you could apply for jobs and you can also create your own apps, any apps you want and publish them to Play Store to generate revenue. And first we're going to start by installing intelligj ID to type our cotling code. Then we're going to move to more specific things related to cotling like the main function. We're going to look at arrays, how we can remove elements in arrays, how we can uh add elements to arrays. Then we're going to move to the navigation graph which is a new way of u navigating to fragments. We'll see what fragments are in the course. Then we're going to uh move to the MVVM which is a new way of uh storing data locally on the phone or more generic name on the device and um this way of storing data is using something called live data. We're going to see what all of this is later in the course. So we're going to learn about AVM pattern. Then we're going to create a note app using the MVVM and you'll be able to add elements to it to remove elements to swipe elements. You'll have animations to this app. Then I'm going to create a drawing app and you could you could draw on the on the screen. You'll have a color picker to choose a color to draw on that uh screen. You can also choose an image and you can draw on that image. You can save the image. And next we're going to move to weather app. We're going to build a weather app using retrofit. We're going to get data from the internet and we're going to display in our app. Next, we're going to build a quiz application in which we're going to uh ask for uh uh what country does this flag belong. So, I'm going to create this and uh then I'm going to build a chat app. This is a beautiful app. We're going to build it from zero. It's going to have au authentification. It's going to have an option to choose an image for the profile image. You could select that image from the phone. You could log in. You could send messages. You can create another account and u send another message. And the previous message going to be displayed. So this is the chat app that we're going to build. Next, we're going to build an another clone. And this is the most complex app that you're going to build because it's going to have many features like uh you're going to search for a route. It's going to display the route in real time. It the you could send a request for the Uber app for the Uber car to accept the request. it will show um a line with the distance where you are and where you want to go and and uh it will have a nice pulsing animation as you can see here and uh this is going to be built this takes this chapter has 13 hours if I think next we're going to learn about um jetpack compose which is a new way of creating um uh UI and we cover the basics but if there are requests because in um a student when I first published this course it was only about Catlin but a student uh asked me if I want to add new content to create real life real projects. So, I added Android. I added a complete new section about Android and I built all of those apps. And uh if you don't believe me, if you don't if you're not if you're still not convinced about this course and what you're going to build, let me show you some reviews that I get. So, some four some four some fivestar reviews that I get from my uh students as you can see here. And uh this is this is all. I'm going to end this video here and I'll see you in the next video and enroll in this course. Hey guys, welcome to this course. In this video, I'm going to explain you who I am and also I'm going to show you something which is going to help you in your learning journey. So my name is Salio Zubinka. I am a software developer who learned to code in 2018 and um I have a course here on Udemia. I have actually more courses not just one course. I have a course about Python. I have two courses about Java. I have this big course about Kotlin and Android. And u this is u basically who I am. But beyond those things, beyond coding, coding and debugging algorithms, my passion extends to the realms of of mathematics and physics because there is something profoundly captivating about the elegant logic and timeless truths that permeate those disciplines. And I'm not saying that I am an expert on math and physic. The truth is that I am um I am still u learning to to to I'm learning math and physics and one day I hope that I'll be able to to read the Roger Penrose book the road reality and to be able to understand that book because that's a very very good book about all the knowledge all the the physic physics that uh that we know so far. So this is who I am and those are my interests. Let me show you the book that I want to to to read. So let me show you here. I have it here. The road to reality. So this is the book. He has more than 1,000 pages and is very hard is by Nobel Prize winner Roger Penrose which is one of the most accomplished scientists of our time and as I said it's basically uh from the very attempts by the Greeks to grapple with the complexity of our known world to the latest application of infinity in figures the road reality carefully explores the move movement of the smallest atomic particles and reaches into the vastness of intergalactic space so it's uh something which I am very very excited about but uh currently I'm not able to to understand uh the information in this book because my uh my math skills are not that good but if uh and if you if you are a physicist or if you studied physics maybe you can recommend me some more simpler books that I can rent to to prepare myself for this uh for this book. Now another thing that uh I want to to tell you is u is that uh you will as you go through through this course you'll may have you'll probably have problems sometimes because you'll not understand some concepts and in order to give you the motivation to to try again I'm going to explain to you so very briefly some very briefly something which is very very very interesting which is called neuroplasticity and neuroplasticity is basically the the ability of the brain to rewire itself to in order to rewire to rewire itself uh in response to the extrinsic inputs. So if uh because uh our let me show you actually the book which I also recommend to read this book by Norman Deuts. is a New York time bestseller and uh uh this book basically explains how the brain changes if you if you stimulate your brain to learn something new and uh uh there is no there is this this thing let me actually read you what what is on the cover of the book. So he says uh one of the reviews says that finally the power of positive thinking has credibility scientific credibility and he says the case presented in this book will change the human culture the way of learning of people and history. So it's a book basically which shows that you you're not uh your brain is not fixed. So if you don't know math, let's say that you're a person who wasn't very good at math like myself and uh if you keep practicing, if you keep uh if you keep trying your brain because this the studies which this psychiatrist did show basically that your brain will change will will it will adapt to to make you understand. So and all why I'm saying you all of this I'm saying all of this because in uh in going through this course you'll have maybe moments when you don't understand some concepts that is not a problem just watch again and uh surely you will understand for example when I learned Java because Java was my first programming language I have I have I had a very hard time with collections but because I knew that the brain is going going to change it's going to rewire itself so that I will understand that finally I keep uh I kept trying so I uh I watched this the section on collections again again and again until I understand until I understood that uh that chapter so this is what I wanted to tell to you so welcome to this course and don't give up because you have now scientific u scientific proofs that your brain is changing you know, you can learn new things by stimulating the brain. So, I'm going to end this video here and see you in the next video. All right, now it's time to talk about the curriculum and what this course covers. And we start by learning the cotlin programming language because this is now the official language that is supported by Google. So, we learn about things like variables, operators, operations. We learn about control flow. So we learn about if statements when statement we learn about loops. We learn about for loops for each loops. We learn about the while and while loops we learn about functions arrays. We learn about object-oriented programming because Katlin is an object- oriented programming language. We learn about classes, inheritance. We learn about uh interfaces, abstract classes, delegation and many more things. Then we move to collections and we learn about sets. We learn about maps. We learn about arrays. We learn how we can um call specific functions on collections. We learn about linear search. You learn about binary search. Then you move to generics. And here we learn about type erasure. We learn about coariance and countervariance. Then you move to lambda functions. Then we learn about core routines which are very important. Learn about SQL. And then we start our journey into Android. And we learn about XML first. Then we build with that knowledge. We'll build a convert to inches app. Then we build another small app which is going to be called change color app using the knowledge that we have from the previous chapter. Then we'll learn about more XML. We learn about activities. We learn about how you can navigate between activities. How you can send data between activities. And with that knowledge we create a quiz application. Then you learn about recycle view. We learn about how you can display a list of elements with a recycle view. They're going to learn about the linear layout, relative layout, nested scroll view, constraint layout and going to see what is the difference between all of those and when to use one or when to use another. We learn about with that knowledge you create a drawing app. Then we learn about navigation drawer. So we learn about fragments. We learn how we can send data between fragments, how you can navigate between fragments. Then we learn about the fire store database. We learn how we can store data in the cloud and we move then to navigation component and we learn how we can navigate between fragments in a new modern way not uh like in in the past. Then learn about databases and we build a notab. We learn about MVVM model view model and we learn about a library called room. Then learn about JSON retrofit and we build with that knowledge a weather app. We get data from the internet and we display in our app. Then we build a chat application. Then we learn briefly about jetpack components. And finally with a 12hour uh chapter you build a new con. So you learn about maps and many many more things here. All right. So my advice for you is like the way the way this course is designed is that you you build your knowledge step by step. So you don't it's important it's very important to not miss any steps. But if you already have some experience with Android, you can skip some uh chapters. But if you are a beginner, you my advice for you is to take uh the course step by step because the course is built in such a way that in when you build something, you'll need the previous knowledge that we learned in the previous chapter. So you don't learn about how to let's say that you build a note app and you display the elements in that app. You learn first about recycle view and then we build up the note app. So this is uh important to do. All right, I'm going to end this video and see you in the course. Hey guys, in this video I'm going to show you how you can join the Discord group that I created for you because one of the students, one of my students suggested me to create a group. And in this group, you're going to see post related to this course. So you're going to see uh let's say that we have a question and maybe I can't answer that question immediately. maybe somebody else who already figured out a problem that you have. It may uh may help you by answering your question. And I also encourage you to post testimonials. So uh post about what projects did you build after you finish this course or what apps did you build. All right. Now to join this Discord group, you simply go under the resource of this video. So we're going to see something like resource. Click on resource and here you're going to see discord group link. You're going to click on that link and that link is going to open this window. And here you need to define a name. So how other people in the chat are going to call you? I'm going to type Alex and u I'm going to check this. I have red agreements and click on continue. And that is going to open the discord group. All right. I need to check here but I'm not going to do that. So you just check that and that is going to take you to the group. All right, I'm going to end this video here and see you in the next video. Welcome. Now it's time to see how you can get the most out of this course because I received some student feedback and also I had many conversations with my students and they point out some things which I want to make uh them clear in this course because it is going to improve your overall experience with this course and you'll understand the course better. And the first thing that you need to understand is that you don't need to skip lessons even though you may have some experience with some topics and you'll be prone to skip to the next uh chapter. Uh I u suggest you my suggestion for you is to take the course step by step because the next step always depends on the previous step. So you need to understand the previous step and then you need to move to the next step. And uh in the case of uh XML, let's say that uh you may be saying that you don't need to uh learn XML because now you have J JPEG compose and you move directly to the JPAN compose section. My advice to you is to first understand all the components of the XML. So what is XML? Why you need to use XML? Why you need to use this component on that component or what is this doing? what is that doing and after that you'll move to jetpack and compose that is going to give you a better understanding of what jetpack compos is doing better why jetpack compose is better and also don't skip the xml section because or sections because uh XML is still has a lot of support online and there are is also widely used in the develop developing apps so it's not something that will to disappear overnight So in this way you should learn the XML first and then move to Jetpack Compose. All right. Now the next thing that I want to point out is that sometimes I speak too fast or my pronunciation is not that good because uh this was my first course. This course that you are watching right now is the first course that I ever recorded and my English was wasn't that good. Even now I'm still practicing and um uh my suggestion to you is to to activate the captions because I added subtitles to the course and to activate the captions you simply go here you click on this icon which says captions and now you have eight languages for which there are subtitles. So those are the languages which now have subtitles. So activate the subtitle and you'll understand what I'm saying. If as I said I'm speaking too fast or I mispronounce some words and you you can't figure out especially if English is English is not your first language. Another thing that I want to say is please leave a review to this course. Leave me your opinion about what you think about this course. What do you think can be improved? What do you think needs more explanation? Because I can use that feedback to make the course better for everyone. So very important, leave me a review, leave me your opinions, tell me what you think. Even though maybe it will not be not all the reviews will be five stars. Whatever you think this course deserve, that that should be your uh your uh your answer, your opinion. So you should u you leave me your uh your review, leave me an opinion, tell me about this course, what do you you like, what you don't like, what can be improved, and so on. So, this is what I want to point out. I'm going to end this video here and see you in the next video. Now, it's time to download the Nestalian ID. And throughout this course, I'm going to use an ID called Intellig ID, which is an ID made by a company called Jet Brains. Now, you may be wondering what is an ID? An ID is stands for an integrated development environment. And it's basically the tool that you're going to use to write a code in. And intellig is like a text editor, but a very sophisticated one because it has a lot of features like highlighting our code, showing realtime errors and suggestions. You can run and compile your code just by a few clicks. In other words, it's making the process of writing code easier and better. Therefore, let's download Intellig ID. So, I'm going to open my browser and in the search bar, we type IntelligJ ID. Press enter and we click on this download link. And on the downloading page, you're going to see two options. Ultimate, which is paid. So you need to pay in order to download it. And community, which is free. And uh the difference is that the ultimate has more features and community has less features. But community is not for our purposes. Also pay attention on the left hand side. When I'm currently recording this video, the latest version is 2021.1.2, which was released on the 1st June of 2022. And if you look at the system requirements here, if you click on that, you'll see that this IntelligJ ID only works for a 64-bit version of Windows. So if you have a 32-bit version of Windows, this will not work for you. Now, if you have MEOX or Linux, uh you should download the particular version for your system. And if you don't know how, just uh tell me in the comments and I will make two separate video and show you how to do. Now I'm going to download the community version for Windows. So click to download the community. Now our downloading is starting on the right hand side on the top. And if you have a different browser, you will see this in an infred place. So once the downloading is finished, just open this file. I'm going to close the browser now because I don't need the browser. And the installation will start immediately. Now it's asking us if you allow this app to make changes to our device. Click yes. Here we need to click on next. This is the for the location where intellig ID is going to be installed. You can keep the default location or you can change it if you want. I'm going to keep the default location. So I'm going to click on next. And here I'm going to check this box to have a desktop shortcut. I'm going to click on next. Now I'm going to click on install. And Intel ID is going to be installed on my computer. As you can see on the left hand side, IntelJ already appeared. Now let's wait until the installation finishes. When the installation finishes, just check this run IntelJ Community Edition and click on finish. Now Intellig IntellJ is going to open. Now it's asking us if you want to import IntellJ settings. So if you have the previous version of Intel ID, you can import the settings here. But I'm going to click not import settings and click on okay. Now intellig ID is going to open and uh this is the welcome window of IntelligJ IDM and here on the left hand side click on customize go here where it says color team so you can change this team because the default team is this dark team I'm going to change it to light because the intellig light team is better for tutorials but you can keep the dark team if you want and in a separate video I'm going to show you how to add more teams not just the ones provided by intellig ID Okay. Now click back on projects. Click on this plus button where it says new project because we need to configure the JDK now. And now here we have JDK and we have in red in angle brackets we have no JD no SDK. And now here we need to add the JDK. And JDK stands for Java Development Kit. SDK stands for software development kit. So software development kit is the Java development kit. No matter how you call it, this thing contains the tools that IntelligJ needs in order to be able to compile and run our programs because without them, IntellJ it's like an empty text editor. So it needs those tools in order to be able to work with our particular programming language which is cotlin. Now to download the JDK and we use Java development kit because Kotlin and Java are interoperable and that means that we can use the existing tools and library which Java already has in our Cotlin project. So we click here on this no SDK and click here where it says download JDK. Now on this window we select version 17 not version 18 because this is the latest version 18. We choose version 17 because this is the version which has the long-term support and that means that it will receive bug fixes and updates for several release to come without you needing to switch to new new version when that new version is released. For the vendor we choose Amazon corator and uh I'm going to let the default location. So I'm going to click download to download the JDK. Now the JDK is going to be downloaded. Now the JDK has been installed and is uh installed on our Intel J ID and now we can create our projects but I'm not going to create a project. So I'm not going to click on create. I'm going to click on cancel and in the next video I'm going to create our first project and we're also going to talk about a little bit the JDK and the JVM. So see you in the next video. Now it's time to create our first project. And there is a traditional programming that our first program should be to output the text hello world to the console. And this is what we're going to do in this video. Therefore, I'm going to open IntelligJ ID. You should open your IntelligJ ID. On the left hand side, make sure to have project selected. Then click on this plus button to create a new project. And here we have a few options. The first is the name. And here we need to name our project. We need to give a name to our project. And the name of the project should start with a capital letter. And if it has multiple words, every word should start with a capital letter. So I'm going to call this u and this way of naming is called the Pascal case. So I'm going to call this u this project hello world. And also you should not have y spaces in your name. Next we have the location. And this is where the project is going to be created. You can change this location if you want, but I'm going to keep the default location. Next we have language and here you can here we need to select the language which this project is going to be created. So um we select Catalin. If you Java is selected just select C. For the B system we select IntelliJ. And uh next we have the JDK the Java development kit. And uh if for whatever reason it says here in angle brackets no ADK. Just click on that and go down here of down of totion. It should say detected as J JDKs and you will see the JDK down there. You're going to see Amazon Corto version 17. Just click on that and it's going to be added here. Next, we have this add sample code. And what this is going to do, as the name implies, it's going to generate some code for us. But I don't want to have that code generated uh now because I want to type that code myself and um I want to explain that code and in the next video we're going to check that to generate the code for us. So I'm going to click on create now to create the project. And here we have the tips. You can keep the tips activated because it's a useful uh information. But I'm going to check this. Actually, I'm going to just close it. Here we have those windows which says there is a there is a plug plug-in update installed. I'm going to close this. So what we have now on the left hand side is called the project pane. And here we have some folders which intellig ID generated for us. But now I want to I want to show you how to check for intelligj updates because in this way we'll have the latest features throughout this course. And to check for intelligj updates, you go to help and go down here where it says check for updates. And here you can check for intelligj updates. And you'll see a window down here. And if there is an update, it's going to say that that there is an update and you can install that that update. But now it's saying that that you already have the latest version of Intellig ID and plugins installed because we have the latest updates. But it's good to check for this because in this way we'll have the latest updates throughout this course. Next check if you want to check for Katlin update. So for the programming language Catalin you go to tools Katlin and go here it says configure cutling plug-in updates and if there is a new update you'll see it here and you click on install and that updates is going to be installed on IntellJ. So there is no update because we have the latest updates. I'm going to click on okay. Now let's focus on the left hand side. And if you don't see if you see something like this and you don't see this in this expanded form, just click on this greater sign sign in front of SRC. SRC stands for source. This is where our source code is going to go. Then click on this greater sign in front of main and make sure to have the folder cutlin selected. On this folder cutlin, this is where we're going to add our code. But first we need to create a file and the file is the thing where we're going to put our code. So we right click on this go to new and here we select on the right hand side we select cotlink class file. Now we need to give a name to our file and I'm going to call this file main. You'll see the reason for this later. So I'm going to call this file main. The name also should be in Pascal case. So it should start with a capital letter and if it has multiple words every words should start with a capital letter. And for the from this options that we have here, we select file because we're going to create as I said the file. Now press enter. Now the file was created for us. As you can see in the folder cutlin now we have main.k have the file and also it open the file for us. Let's uh click there. Now if you don't have the file opened and if and if you have something like this just double click on the file and the file is going to be opened for you. Now here now I'm going to type some code. Now you need to type exactly what I typed. So type fun. So while I am typing as you can see intelligj is trying to help us and is giving us some suggestions. So it's saying do you mean fun that uh and if you want to fill in that for you just press enter because I want to to to type that fun keyword fun and it also added the space. So fun space. Then we type main and next we need to put parentheses and as you can see display button now appeared. I'm going to explain immediately why and uh we put the left parenthesis and when we put the left parentheses the right parenthesis is going to be added automatically. So we put the left parenthesis and the right parentheses was added automatically. Then we get outside of the parentheses. We press space. Now I'm going to add curly braces and again we put the left curly brace and the right curly brace going to be added automatically because Intelligj is helping us. Now we're inside the curly braces and now we press enter and it u added the curly brace on the third line and also indented this line for us. Now let's talk about what we have here. Now what we have here is is what is called the main function and the main function is the entry point of our program. So every program that you create it it first needs this entry point in order to be able to compile your code. But let's take uh each part of this main function individually to talk about. So first we have this fun and fun is the keyword for creating a function and uh you can use fun keyword to create your own functions but we're going to see that in uh in future videos. Next we have main and main is the name of this function. Functions can have different names but main is a special one as I said because main is the entry point of our program. Maybe every program that you create needs needs a main function in order to be able to run your code. And in fact, if I change the name of this function to fun main, let's say a the play button immediately disappear because now uh we don't have an entry point and our program can't run. So this is a special function and functions can have different names as I said and as you'll see when you're going to create your own functions that you can create uh you can give different names to your functions. Now next we have parenthesis. We and we have parenthesis because um functions can take parameters and can do some work with that with those parameters. But our function doesn't take any parameter. But we still need to provide the parenthesis because they are part of the syntax. And we can think of the syntax like the grammar rules that you need to to to respect in a specific uh language in order to be to be to to be understood to be understood and uh in the same way this is the syntax in the programming. It's like the grammar rules in which the compiler as you you will see what is the compiler later. The compiler needs uh enforces you to respect those syntactical rules which is uh to have parenthesis for a function in order to be able to understand your code because if I delete the parenthesis here now we have an underline which says uh expecting parenthesis. So it's um it's it it you need to respect the the syntactical rules in order for your code to be to be understood. Next we have curly braces and we have left curly brace and right curly brace and this is called the body of the function. So this is where our code goes. So this is also called the code block because it's the block of code. This is where our logic our uh our this is where our logic our lines of code go. They are we define them inside the the the curly braces. So this represents the body of the function and let's add some code and I'm going to type print line here. So because when we need to output something in the console so we type here print line. So we type print and while I'm tamping you should see a suggestion and we have the suggestion print line and we select the first one to fill in for us and now we go inside the parenthesis of the print line and we put double quotation marks and inside the double quotation marks we type hello world. Now we need to we need to run this code and in order to run this code to compile this code we can do it in u two ways. First you can click on this play button that we have here or you can right click and uh you can go here where it says runman.k and the code is going to be compiled and uh let's do it from here first. So I'm going to click on display button to run our code and we're going to see the console opening down and we're going to see the text hello world hello world outputed. So the console opened and we have hello world. We have the text that we type inside this uh thing that we have here. This is also function and we have this code outputed now in the console. The console opened. Now I'm going to I'm going to close the console to explain what this print line is. This print line that we have here is a function also but is a function which takes some u which takes input and the input is this text and this function its job is to take whatever you pass as the input and to output that in the console. So this is what this print line is doing and this is a function that we didn't create. We didn't type free fun print line and uh then we call it. This is a function which is u built into cont. This is a function which is already built by the cotlin programmers. So we just uh call that function inside our code and we can use you need you don't need to create this function. So this is the this is what this print line is doing. So it's taking whatever you pass inside the parentheses and it's outputting that in the console. So this our discussion uh about u the first program which is to output some text in the console hello world and u now I'm going to show you that uh and now I'm going to show you an illustration to understand what is um what is the JDK the JVM and uh how Katlin and Java achieves this interoperability because Katlin and Java are fully interoperable and that means that you can You can have a Java project to which you can add cotlin code and you can compile that code and everything to work fine. And also I'm going to explain what the what the word compile means and for that I'm going to bring a illustration here. So this is the illustration and uh let's talk about what we have here. First we have cotlin source code and cotlin source code is the code that we wrote is this code that is um meant to be human readable. So this code the code that we as humans can understand we can write this code we can read this code but the computer doesn't understand this high level high level code that we wrote. It needs something it need it needs machine code it needs binary code in order to be able to understand. And this is where the JDK comes into action. So our cotlin source code again this code which is meant to be human readable then it's when when we uh press on that play button when that and when you press on that play button on this button and the code is compiled what uh happens under the hood is that that cotlin source code is is uh taken by the Java development kit the JDK and through the cotling compiler. So the cotling compiler acts like a translator. It takes that high level human readable and writable code. It converts that code then into what is called Java byte code. And this is not the final step as you can see in this illustration. This this is just the intermediary step. This is not converted into machine code. Then this Java bite code it's uh taken by the JVM. So it's it's sent to the JVM and the JVM then takes that Java by code and convert that J that Java by code into into something which a machine which a specific machine can understand. But let's let's first explain why why cotlin why the JDK takes this cotlin source code and then it converts this cotlin source code into java bite code and this is is doing this because different processors and different operating systems need different instructions in order to understand your code. So if uh we are not uh we will not have this java by code. So if we had we will compile our code on a specific platform that code can run only on that specific platform but because our cotling source code is taken by the JDK and through the cotling compiler and it's converted into Java byte code and the Java byte code then it's it's uh it it's send it to the JVM and JVM stands for Java virtual machine and the JVM then becomes different platform forms and different uh operating uh uh different platform and different processors need different instructions. The JVM is the one which uh is doing the hard work what of figure out figuring out what kind of instruction that specific platform needs. So it converts that Java by code into machine code let's say which a Windows platform can understand. Then the same thing happens to Linux. The J the JVM receives the same Java byte code and then it converts that Java B code into machine code which a Linux platform can understand and then is doing the same thing um to a Mac OS. So the Mac OS is the JVM is going to is going to receive the same Java by code and it's going to convert that Java by code into machine code which Mac OS can understand. So this is how uh Cotlin and Java can achieve this thing called write once and run everywhere. This is because the the work of converting that uh machine code which which a specific platform can understand has shifted from the programmer to the platform which has the which implements that JVM. So we just wrote our code we run our code through the JDK. the JDK converted that code in Java byte code and then the platform which has the JVM installed um it's doing the work of converting that Java by code that we wrote into something which that platform that specific platform can understand. So and this is why Java and Katlin are fully interoperable. So you can add you can add Java code to an existing Kotlin program and vice versa you can add you can add Java you can add the cotling code to an existing uh Java program and that is u that is possible because uh the JDK is going to is going to convert through the compiler all that code into the same Java byte code and then that code is going to be run on the JVM and the JVM is going to convert that into machine code and that code is going to run on a specific platform. So this is how Java and Cotlin and this is why Java and Cotlin are fully interoperable because they u they are they are ultimately converted into the same Java by code and you can also achieve this right once run everywhere because that code then runs on the JVM Java virtual machine which is doing the hard work of figuring out what kind of instructions uh specific platform needs in order to send that uh code that we wrote and uh in this way that hardworking has shifted from us to the people who implement the JVM. So this how uh the JDK and the JVM works under the hood. And if you find this confusing, don't be this. This is just a relaxed discussion about u how the JDK what is the JDK and what is the cotling compiler because I said our cotton I said previously that our code is compiled. When I said that this actually what was happening our code was taken by the JDK and through the JDK was converted to Java byte code. then it was um sent to the JVM of which is on my windows and then that is converted into machine code and then we see in the output uh hello world. So this our discussion about uh the first program how to create the first program hello world and this is the additional discussion about how the JDK how the coding compiler and how the JVM works. So see you in the next video. All right. So in the previous video we've created our first program and uh nevertheless it was a simple program but you should celebrate that because you created your first program and um if you want to make your program to do something useful you need to store data and to store data you need to use variables and this is what we're going to learn in this video. Therefore, let's open intellig ID. It should open your ID. But if you already have uh that project from the previous video opened, you can use that. But I'm going to create a new project. On the left hand side, make sure to select projects. Click on new project to create a new project. On the left hand side here, make sure that Cotlin is selected. Here we need to give a name to our project like we did in the previous video. And this time I'm going to call it variables. It should start with a capital letter. For the project template console application is selected. For the build system, select IntelliJ. And also make sure that the project JDK is selected here. And after that click on next and click on finish. Now our project is going to be created. And if if you see a little uh window popping up here saying that there is a new cutlin plug-in update or there is a new intelligj ID update, click to update uh that because in this way you'll have the latest updates throughout this course. And if you want to manually check for those updates, go to tools for specifically for cut updates. Go to Catalin. Go to configure Catalina plug-in updates. And here you're going to see that there is a new version of uh of Catalin to install. But I'm not going to install that here. But you can install that. And uh to check for IntelJ, you go to for Intel J updates go to help and go down here. It says check for updates. Now our project is ready to go. And uh as you can see here, it generated some code for us. And this is similar to what we've did in the previous video where we created our we create our code u ourselves. But we have some additional code. And uh I'm going to delete this additional code because it will make things confusing. So I'm going to delete this part. Sorry, I'm going to delete this part. And also uh you have may may have noticed that uh here in uh inside this parenthesis now we have this args array and a string. We can delete this two because uh it will make thing things confusing and we're going to talk about uh what are rays and all of that in the next videos. Now I'm going to All right. Now ready. Now let's uh run this code to see to see what is happening. It's going to output uh that text like uh it did in the previous video. It should open the console in a short moment. And indeed it opened the console and it outputed the text hello world like it did in the previous video. So I'm going to close the console now. Also let's expand this src to see the file that it was created automatically for us uh this time. And um let's change this hello world text to print uh another text. Let's say let's print uh type your name there. In my case is Alex. So I'm going to run this quad and it will output uh my name in the console. All right. So it um outputed hello Alex like uh we typed here. But um it's uh what we have here is pretty pretty inflexible. It will a better way to be to to do that. It will be to have something to store this name and then refer that uh that thing here to call that thing here. In this way to be a bit more more dynam dynamic and uh to do that we need to use uh variables and variables are um are like some uh containers which can hold data values and um let's see how to create a variable. Okay, let's uh put the the the code below above our print uh len statement and um if you don't know what a statement is, a statement is a complete uh instruction of execution. Now to create a variable, we just type the keyword var which stands for variable. All right. Next, we need to give a name to our var variable to be able to refer it later to call it later. And I'm going to and the naming convention around the naming variable is called is called camel case. And that means that the first uh letter in the name if it has multiple words. So start with a with a lowerase letter. So I'm going to type user and the next word name with a capital letter. All right. Next we need to provide the t type of the variable and that means what kind of data this variable is going to hold is going to store. To do that we need to put colon and next we need to say what type of data is going to store what what data is going to store and uh we want to store text and um to store text we need to type here string and string means that this variable is going to store a sequ a sequence of characters or text. Now we need to assign a value to this variable. And to do that we put we put here equals space and uh because we explicitly said here that we're going to put text or string we put a double pair of quotation marks and and we type Alex. All right. So now we create our variable. We give it a name, a type and uh we we've also assign a value to this variable. Next, we need to refer this variable down here. Um, and to to to refer that that variable, I'm going to delete this text first. And to refer the variable, we put dollar sign. And after the dollar sign, we start typing the name of the variable user. And as I'm typing, the IntellJ is giving us some suggestions and it says, "Do you mean this username variable?" And it also has this V here for variable. So I'm going to press enter to fill in that for me. And if I uh run this code, you'll see the exact uh thing uh happening here. It outputed the text hello Alex. But this time we didn't type the text directly here. We we we store that text in a variable and we call that variable in in our print ln. I'm going to add a space here to make things more clear. Now what we can do is to change the value of this variable by um adding uh below of our variable typing the name of the variable again user name equals and now we can assign a new value to this variable and uh let's put let's put John here right let's run this code to see what happens now now this time when this uh this instruction instruction is going to be called. It's going to print the the new value of the variable which is John. So as you can see down here, it outputed the text hello John. So we can change the value of the variable after we assign a value to it. But what we can't do is to is to type here username equals five. And um if you hover over here it says integer literally does not conform to the expected type string. And in other words it's saying that we said explicit explicitly up here that is going to store only text. And uh down here we try to put um a whole number an integer and that is not going to work because cotlin is uh language which is uh which is called statically typed and that means that it it will check the types of the variable at compile time not at runtime. And that means it that it you cannot run your code if you assign a value to to a variable which has a different type that than the than the type that you specify specified when you decor the variable. This is why you get this warning here. And if I delete this now let's delete this. All right. Okay. The next type of variable that uh also actually not the the next type the var the next way of declaring a variable is using the keyword val. So if you declare if you declare a variable as val means that we cannot change its value after we assign a value to it. So if you try if you try to assign a new value to our variable like we did here up here, it will not work. So let's actually let's actually just change here. Let's put val. Okay. And and after I did that you it immediately underline in red username and it says val cannot be reassigned. And that is because if you declare if you declare a variable as val you cannot change its value later. It will uh you can put a a value to that variable only once and that's all. You cannot change the v the value of the v variable later. So let's change this back to var to have the ability to change the value. Now let's look at another type of variable. So I'm going to type var again. I'm going to call it age. I'm going to put colon to specify a type here. And now we're going to use another type which is int. And int stands for integer or for whole numbers. Now we need to assign a value to it. I'm going to call I'm going to put um I don't know 10 or I know 20 22. All right. So what we have now is a different type. Up here we have string and that means that we're going to we're going to store sequence of characters. And down here we have integers and that means that we can store um only only numbers only whole numbers. And there are different type of numbers which we're going to look in the next video. And if I try here to change this u to change this u first first let's let's actually let's actually print this uh hello Alex let's type your age is and here you put dollar sign and we call uh the name of the variable we type the name of the variable age. So we press enter to fill in that for us. And if you run this code now it says hello John your age is 22. So it's now it's using the value the values of this variable in our print ln. And we can change for uh the variable also the its value because it's declared as a v not a val. So you can put here age equals let's say 25. And if you run this code the change is uh reflected immediately in our code. So now we see hello John your age is 25. What uh we can't do like in uh the previous uh example with the username here we cannot put a string. We cannot put here Alex because we said explicitly here that this is going to is going to hold only integers only whole numbers. So I'm going to delete this. Let's put it again to 25. And the arrow error disappeared. All right. Now um let's look um at what we have here because we have this underlines this uh uh great light out of our types. And every time uh you see something like this uh that means that IntelJ is trying to say something to you. And if you hover over here actually let's hover over over the type. It says explicitly given type. It's redundant here. And that means that um it does not need to we we don't not we don't need to say explicitly here that is going to hold um sequence of characters. It's going to hold string because it knows this it can because it cotlin has something which is called type inference and that means that it can infer the type of the variable by the value we assign to it. In our case we assign some text. So if I delete the type here now it's still working and if I as you can see if I if I put here let's say if I try to put here username sorry username equals to three is still uh treated as a string because it infer it it it infer the type by the value assigned to it. And uh similarly this uh this thing applies to to our uh int type. So if I delete this is still working is still treating is still treating this as a as an int. So if you try to put here age equals to a text I don't know again we have this arrow error in it says type mismatch require int and found string and that means that it's still is still treating this uh variable as an int because as I said it has something which is called type inference and it can infer the type of the variable by the value you assigned to it. All right, this is u our discussion about variables. There are more types of variables and we're going to look at that uh other types in the next videos and uh collectively they are called data types. Okay, you can remove this space if you want. So I'm going to end this video now and see you in the next video. All right. So in the last video we talked about variables and you saw there that we can store different data in our variables and we looked specifically at the string data type or type for storing sequence of characters or text and at the int or integer data type for storing whole numbers. In this video going to look more closely at the integer data type and also going to extend our understanding to three more data types for storing whole numbers and those are the bite, the short and the long. So let's open IntellJ. Make sure that on the left hand side project is selected. Click on new project. Let's call this project integer type in Pascal case. Select cotlin intelligj add sample code and then click on create create a project. All right. So now our project is going to be created. We wait a little bit. All right. So we open the file main.t for us. Now, here we have some code, but I'm going to delete this code because you don't need this code. And those that you see here are called comments. We're going to look at comments in a separate video. So, let's delete this code. And we have our print line there. We can delete this also. And you can keep uh here those are called the parameters. We're going to look at parameters. We're going to talk about functions, but you can delete those if you want to make it consistent with the last video if you want. So it will work the same. It doesn't make a difference. And in the previous video, we had this code val name of type. So colon string and we're going to put equals and we assign a variable double quotation marks Alex. Then we had another variable called val. So also val because don't want to change it value. Let's call it age of type int with uppercase i equals to 23. So this is the the code that we had previously and we add we had a print line down here which uh output the values for those variables. So let's add a print line here. Let's say hello dollar sign name your ages dollar sign age. So now if you run this let's click on this run button here run main.kt KT and we have uh a popup here which says there is a new version of cutling plugin available. I'm going to click to install this but let's see the output first and get hello Alex your age is 23. Let's put an exclamation mark here. Let's run this again. So the console opened and the output hello RX regist. So this is the code that we had previously. And uh as you can see here those types are gray out because they are redundant because the compiler knows what type these variables are by looking the at the value that is assigned to them. So now if I delet if I hover over here on this gray out string it says explicitly given type is redundant here. So we can delete this type here and we can also delete it here because if you hover over here as you can see it's gray out and it says explicit given type is redundant here. So it's redundant. I know already what type this variable is. This is what is saying in other words. So let's delete this. And now if you run our code we get hello Alex your age is 23. So everything works fine. And if you hover over the name of the variable, you're going to see that val name and we have colon string. So it's infer the type to be a string. And here if you hover over over the age variable, you see you see that we have a val age of type int. So it knows what the the type is very the variable by looking at the value that is assigned to it. And if you try to assign here, let's say age equals to to something we can because first this is a a val and second this is an int not a not a string. So if I change this to var now I don't get here that warning but if I hover over here it says type mismatch required int found string. So it's saying do you you made this variable of type int and int. So you want to store whole numbers and uh you try to assign a text to it. So it's that that is not going to happen. I'm not going to allow it. So this is basically what it's saying. Now let's install this plugin because it's good to have the latest updates throughout this course. So let's wait a little bit. You can hide the col the console. Let's click on restart to activate plug-in updates. So click on restart. Our project opened uh again. Let's click on configure because it says that the cutling not configured. Let's click on configure. Actually, let's wait a little bit. Click on ignore and we have a problem now. All right. Now, let's delete the variable name which is used for storing the the text. And we're going to look closely at the integer data type here. So, let's delete this code. So, now uh here should have an underline or it will this is in red now because the variable doesn't exist. So we can't call it here. So let's delete this. And we can keep your ages. Actually I'm going to change it because I'm going to give a different name to this variable. And uh let's call this variable now number. Now let's think about what is this integer data type that we've used here. Uh we know that is a type. So it's a data type for stoing whole numbers. But u how big the whole number can be or how small the the whole number can be. There is a range is there is there is no range. So actually there is a range. So there is a minimum and maximum value which can be stored in integer data type. And to get the minimum maximum value which is can be stored in a integer data type I'm going to type here val integer max value equals to and I'm going to type here int dot max value. Now this is going to give us back the maximum value which can be stored in in integer data type. So if I press shift control P on this as you can see this uh thing that we have here int and max value we're going to see what uh what this in what are what is this in the future videos when I'm going to talk about object oriented programming but for now we need to know that this line of code is going to give us the maximum value which can be stored in an integer then I'm going to type val to get the minimum value minimum also integer min value equals int dot minimum value and that is going to give us the minimum value. And now let's print those in in the console. So let's output those in the console. I'm going to type integer max value colon going to put dollar sign not here. So here dollar sign integer max value. Let's press Ctrl D to duplicate this line of code. I'm going to type integer now. Min value. It's equal to integer min value. So we choose this variable. Now, now if you run this, we get integer maximum value and we have 2 bill147,483,647 and this is the maximum the maximum value which can be stored in an integer and this is the minimum value which can be stored in an integer. Next, let's see what happens if you attempt to put a number which is bigger than the maximum or the minimum value. So, I'm going to bring down our variable number and I'm going to assign the maximum value which can be stored in an int. I'm going to copy this and I'm going to paste it here. Now, we have an underline because this is a val. So, let's change it to var and uh so this is the maximum value which can be stored in an integer. Now if we increase this by one. So if I put 48 at now at the end there you have an underline and it says the integer literally does not conform to this petty type int. And now you may be wondering uh why because this is an integer. So it's a whole number. So why don't uh why saying that the integer does not conform to the expected type int. This is because this is now a bigger number than the maximum number which can be stored in an integer which is this number below here. And now you try to put a bigger number and it can't it can't uh hold this number. It's too big is bigger than the maximum value allowed to be stored in an int. You need to use a different uh data type for storing this number. For storing a number bigger than the maximum value can be stored in an int. And for that I'm going to use in the next videos. As you'll see a long and with a long you can store a bigger number than those maximum miners defined here. The long data type has a has a much bigger uh range as you'll see. Now I'm going to end this video here and see you in the next video. Now it's time to look at the next data types that we can use for storing whole numbers and those are the bite, short and long. Therefore, I'm going to go down here and I'll type val. I'm going to start with the byte. So, I'm going to bite. I'm going to type here bite max value equals to byte dot max value that. But now let's also put the explicit type here. So let's put here bite. Then I'm going to type val byte mean value of type bite equals to by dot min value. Then I'll add two print lines here. So, I'm going to type print line. Let's put it here. Print line. Let's type by max value. Let's put colon dollar sign byte. We're referring the variable max value. Let's press Ctrl D. byte min value equals by let's change out this variable to mean value. Now let's run our app to see what we get in the output. What is the minimum and maximum value which can be stored in a bite? And as you can see we have integer maximum value. So you have the numbers from the previous video and bite max value is 127 and bite min value is minus 138 and as you can see this is are much smaller than the integer uh types. So let's now look at the next data type and that is short. So I'm going to type here val short max value. Let's define the explicit type short equals to let's type short max value and val short min value of type short equals to short min value. Let's add the print line here. Print line short max max max value dollar sign we're referring the variable now and as you can see they are grayed out and intelligj ID when it tries to tell something to you it's sometimes it's uh graying out the the thing that it wants to tell something about and some other in other cases it highlights with a certain color. So this is good to have in mind. Now I'm going to type here short max value Ctrl D short min value and let's change now short mean value to refer the the that variable. As you can see that is gray out because it's not if you hover over it says variable short mean value is never used. So short mean short should be here. Now if you run this we get short max value 32,767 and minus 32768. This is the minimum value as you can see here. Now let's look at the next uh data types that can be used for storing whole numbers and those are the long and let's hide the console. So I'm going to type val long long max value equals to long max value long min value equals to long min value. Let's add the print line here. I'm going to type long mean min value dollar sign long max value should be here let's press ctrl d and long min value. Now again the long mean value var variable now is gray out because it didn't call it. So let's call it here long mean value. All right. Now let's run our code to see what is the minimum maximum value which can be stored in a long. And as you'll see the long is the data type with the biggest range. As you can see the long max value is this number that we have here and it's a very very big number and the long minimum value is this number. So as you can see it's much bigger than the integer data type the long. So in cases that where the the integer data type can can can no longer be used because uh it's exceed the maximum value can use a long but most of the time you'll be working with integers and uh sometimes in specific scenarios you'll use by short but most of the time you'll work with the integer. Now let's create a variable. So let's take let's delete this to show you something. So I'm going to type here val my number and u to declare a long variable you can declare it in but first let me show you let me show you something. So if I put here equals and I put 28 and if you hover over this variable it says that it's a val my number of type int. And now you may be wondering why it inferred it to be an int because this value can easily fit in any of those type that we looked at. And this happens because the default type which is inferred. So if no type is specified explicitly is an intel. So if I don't type I don't specify an explicit type here after I name the variable it will treat this variable as an int. Even though this variable can uh this variable this number can easily fit in into a long into a short into a bite. So if you want this variable to be u a bite or a short or a long you need to explicitly declare here bite let's say and now if you hover over it's a bite and uh if you want to make it a short you type short here explicitly otherwise it will it will infer the default type to be an int and uh also for a long and for for the long there are two ways you define here the type you put colon you define the name of the variable colon long and if you hover over now this it's a long but uh there is another way and it's by adding a l suffix at the end of the the the value. So if I put an L suffix here and it says use L uppercase instead of L lowerase and this is because that can be easily mistaken for one. So you need to put an uppercase L at the end. Then now if I hover over as you can see val my number is of type as you can see there long because we put a L at the end. And now if I type here a number which is bigger than the maximum value which can be stored in an int. As you can see put here this number. Let's delete because it's too big. Now if I look here as you can see this is a long now it show we had that underlying red because we exceeded al the long also this why it was in red. So it automatically infer this to be a long because it's bigger than the maximum values can be stored in an int. So this is good to have in mind in your coding journey. So I'm going to end this video here and see you in the next video. Now it's time to see how you can store floating point numbers and those are numbers which have a fractional part and going to look specifically at the float data type and at the double data type. But before we start our discussion, let's see the amount of space each data type occupies. So let's start with the bite. A bite occupies eight bits. And we say that a bite has a width of eight. And the short can store a larger range of numbers and occupies 16 bits and has a width of 16. An int has a much larger range as you know and occupies 32 bits. The point here is that each delt type has a different size. It's not particularly relevant to you to know those numbers, but you may encounter it as a question for an interview. Now, let's create a new project. So, let's close this project. Click on new project. Let's call it float and double. And let's click on create. Right now project was created. Let's hide the project pane because don't need the project pane. And let's delete this code because we don't need this code. Also the parameters that are defined here. Now to declare a floatingoint variable I'm going to type here val because I want this variable to be immutable. I don't want to change this value. It's going to be called my number. It's going to be in camel case. And going to put equals 2.5. And now if I hover over here, as you can see the type which was inferred, it's a double. But look what happens if I put here for the type float. Let's say have an underline. And it says the floating point does not conform to the type float. So what is saying is that this is considered to be a double and here we we said that the type should be a float but the default when you are dealing with floatingoint numbers the default type which is inferred by the compiler is the double. So in order for this to work you either put here an f it can be an uppercase f on or a lower case f and u this is how you solve it. So let's create also another variable here val my second number equals to 2 zero let's say and uh let's put here the f and for the double we don't have a a letter to add it as a suffix so you can put here d or d because you don't have such a thing now as you can see here. This is gray out. And every time you see something that is gray out or is highlighted, that means that IntelligJ is trying to tell you something. Now, if I uh hover over here, so let me hover over. It says that explicit is redundant here. And it's redundant because we put an F here. So, it knows that it's going to be a float. But again the default type which is going to be inferred if you don't specify uh a type either by using the f as a suffix it will be the double because the double is the default type which is inferred. Let's press ctrl alt alt l to format the code. Now let's print our variables in the console. So let's add the print line here. print line and let's type here my number equals to dollar sign my number. Let's type end inside the quotation marks. And my second number equals dollar sign my second number. And as you can see, my number is a float and uh my second number is a double. as you can see here. So, let's do use my second number. Let's output those in the console to see what we get. So, get my number 2.5 and my second number 2.0. And let me show you something. If I put here only two, but I I I add an F at the end. So, a lower case F or an uppercase F. And if I run this we get my number is equal to 2.0 even though we didn't we don't have here 2.0 because we added here an f as the suffix it will it will show us also the fractional part which is zero because so and then I have my second number which is also 2.0 zero but this is a double. So this way and uh the difference is that floatingoint number has a small smaller precision than a double and depending on what you're trying to achieve. If you want more precision you'll use a double. But if you don't want uh more precision use float depends on the on the on what you're trying to achieve with the with your code. Next, let me show you what happens if I put here only two and if I run the code. Can you guess? Now I get my number 2.0 and my second number equals to two because now we didn't specify u we didn't put the fractional part here. This was not treated as a double. So it only output it to but if I put here double so the explicit type now we have a problem because if you hover over it says the integer list does not conform to the expected I double and this is because uh as I said in the previous video the the default type which is inferred for the whole numbers is an int. So it's saying here that this is an integer and you declare a double. So make this number that you assign to me into a double. So you need to put the fractional part like 2.0 and the error disappears and this also becomes redundant. Now you can delete this. So this is uh good to have in mind. Now I'm going to end this video here and see you in the next video. All right. So far in our videos we only dealt with data types that can store numbers. But there are two different data types for storing a different kind of data and uh what data you may be wonder. We're going to see what data in this video. So let's create a new project. Make sure that on the left hand side project is selected. And here we have our previous project. Let's click on new project. Let's give it a name. Let's call it char. So this is the data type that we're going to use and boolean in Pascal case language cutlin build system intelligj make sure that we have the JDK 17 here and also going to check this add sample code to generate the main function and the code for us. So click on create You can close this. Right. Our project was created. Now let's delete this code because we don't need this code. Let's also delete the parameters defined here. And let's also hide the project pane because we don't need the project pane. So let's create a variable. I'm going to type val because I want this variable to be immutable. And I'm going to look first at the char data type. So I'm going to type here my char value in camel case of type char. It's going to be equal to and now I'm if I put here double quotation marks we have an underline because if you hover over it says type a mismatch require char found string. So in with a chart we cannot use double quotation marks. What you need to do instead because within a chart you can uh only store a single character which can be a number, a letter, a symbol like an exclamation mark or point and you cannot put double quotation marks. You need to put single quotation marks for a chart. So if I put now single quotation marks and I put here let's say the letter D. Now uh the error will disappear because now we have a single character in our single quotation marks. Now let's print these values. So I'm going to add the print line here. Print line. And we can print this in two ways. So I'm going to put the dollar sign. So the placeholder my chart value. Let's run our code to see the output. All right. So we get the letter D outputed in the console because this is the value of this variable. But there is another way to output this in the console and you can remove the double quotation marks. You can remove the dollar sign and you can run this in this way and it will have the same uh output. So we get also the here output in the console. And as you can see the chart is gray out because uh like the previous uh type pre previous data types the type can be infair. So it can sees that this variable is a is a chart. So it no longer needs this explicit type here. So it can delete the explicit type. We can let it like that. Now if you hover over here as you can see my chart value is of type chart. So it infer the type to be a chart. So this is the chart. And uh now you may be wondering when you going to use this. This can be used let's say that you want to store the last key pressed by an user or I don't know something u a letter from a from a name or in that case you can use a char value. Whenever you want to store a single character you can use a char. Now let's look let's let's now look at the next data type and that is a boolean data type. So I'm going to type here val my in camel case my boolean value and a boolean value we're going to put colon boolean a boolean value can only store two two things true or false. So we can put either we can put here either true and let's output this in the console. So let's uh add a print line here. So if I put my boolean my so my boolean value. Now if you run this because this variable now has the value true. We're going to see true in the console outputed. So we get true being output in console because this is the value of this variable that is referenced here in this print line. Again for this uh variable you can also delete the explicit type because it can infer the type by looking at the value that we assigned to it. And you can also put another uh u value here which is false. So now if you run this you get as you can see false and you can put those in a single print line. So let's delete this. Let's put double quotation marks. Let's type my char value equals dollar sign. my chart value and and my boolean value equals dollar sign my boolean value. So now if you run this we get my chart value D and my boolean value equals to false because those are the values for all variables for all variables. Again you can delete the explicit T here because you can infer the type. So if you hover over now as you can see this is now a ble my boolean value th boo boo boo boo boo boo boo boo boo boo boo boo boo boo boo boo boo booboo boo boo boo boo boo boo boo boo boo boo boo boo boo boo boo boo boo boo booboo boolean so it knows what value it has and um now you may be wondering where you going to use the boolean data type and uh the boolean data type is going to be used a lot in your code in your programming and it's going to be used specifically in conjunction with the control flow statements and we're going to look at the control flow statements in the next videos but uh to give you an example there is a control flow a statement called if statement and with an if statement and a boolean value which can be true or false you can control the flow of the program because the flow of the program is by default from top to bottom. So in other words every line of code is read one by one and what you can do with a boolean value or a boolean expression is that you can break this flow of the program by imposing a specific condition. So only if that condition is true uh a particular uh block of code is going to be executed. So you're going to use boolean values a lot in your programming. Actually there is no program that you can I you can build without uh using the boolean values and control flow statements. Now let's do a recap of all the data types that we learn so far. So we learn about bite, short, int and long for storing whole numbers. Then we we learn about the floating point uh data types. We know that was the float and the double. And we also learn about the char and boolean. And uh we also learn about the string data type which is which is a special data type because is it has what is called class support. And the the the other the other data types are also called so the byite short long int float double char boolean are also called are also called primitive data types and this is um because of how they are stored in memory and uh they're not they're not actually uh primitive data types in my opinion because they can uh you can uh So you can use them as classes but we're going to talk about this in in the section about object- orientated programming. For now what you need to understand is that these are the data types that we're going to use and in the next video we're going to see the operations. We're going to look at operators and operation that we can do with those data types. So I'm going to end this video here and see you in the next video. So far in our videos, you only saw how we can store data in our variables, how we can store different data in our variables, and we looked at different data types for storing different data. But if you want to make your program to do something, we need to do some operations. Therefore, I'm going to create a new project. I'm going to call it operators. Select Kotlin, IntelliJ, JDK, add sample code, and then click on create. All right, our project was created. Let's delete this code because we don't need this code. Let's hide the project pane. Let's delete the parameter here arcs. Next, I'm going to declare two variables here. V x is going to be equal to 5 and val y is going to be equal to three. Then I'll type here val result. And in this variable result I'm going to put the sum of the five entries. I'm going to type here x plus i. Then I'm going to print the result. Print line result equals dollar sign result. And we're going to get eight. Now let's run this to see that we get eight in the output. So we get result equals 8. So what uh this uh oper operator is doing is adding a sum it sums the value of x and i which is five entry and those uh and um the the objects which are affected by an operator. In our case x and i are uh the objects affected by an operator are called operands. So here we have two operands x and i and the operation the operator is plus. So the this those are called operands. This is good to have in mind. And uh if you put here directly five and three this those two are also going to be called operands because they are affected by the operator plus. So if you run this we also get eight. But let's put x and i here. Now let's delete this code and let's uh add let's look at the next operation that you can do in a cutlin. So I'm going to add here a print line and I will type here put quotation marks x + i equals I'm going to put dollar sign curly braces x + i. Then I'm going to press Ctrl + D four times and I will change this to minus this to multiplication dividing and an operator which is not so well known as outside the programming context is called the modulus operator and the modus operator is going to give us the reminder of a division. So let's change now here those two here minus asterisk for multiplication dividing and modulus and this thing that we have here in curly braces is called an expression and and an expression is a construct which evaluates to a single value. So if I press uh if I press here shift controlp not on the entire string. So let's try here shift shift control P. As you can see if I select X + I we have this popup which says that is an integer. So that returns an integer and that is because it sums their value and returns value here. So now if you run this we get x + i 8 x - i we get two which is correct you get x * i 15 because 5 * 3 is 15 x / i is 1 and you don't have the the remainder because uh this uh this this expression is an int here is an int. So it will drop the fractional part completely. It will not show the fractional part and x modulus i is two because 5 / 3 is one with a remainder of two. So this is what the modulus operator is doing. Now if you want more precision and you want to see what is x + x / i, you can you can declare one of those variables as either float or double. So I'm going to put a 3.0 zero here to make this a double. Now if I press shift Ctrl P here, let's go here. Shift Ctrl P. Now, as you can see, this is a double because we put here 3.0. Then the entire expression inside the curly brace is going to be a double. So now you run this now get 8.0 2.0 15.0 and we get 1.666 and then we get 2.0. So as you can see now the all of those uh expressions inside the curly braces are of type double because we added here 3.0. So this is a double and the entire expression is converted into a double. So we have more precision now. So and if I put this uh and I put here an F for float as you can see you'll see less precision here. So if you run this as you can see a float now has a smaller precision than a double because now if you press shift control P to see what kind of type is this expression. So we select X or I. So x minus i. As you can see, it's a float now. So this is why you get only get a smaller precision than the double. So let's delete this. Next, let's look at operator precedence. I'm going to type here print line quotation marks. I'm going to type here three + 3 times asterisk 4. Then I'm going to put equals. I'm going to put dollar sign curly braces. I'm going to type 3 + 3 * 4. So now, can you guess the result? Let's run our app to see what we get. we get 3 + 3 * 4 is 15 because first is doing the multiplication which is 12 and then it's adding 12 to to three and we have 15 but you can control the you can control the the precedence of the of the operators by putting parentheses so if I put parenthesis around 3 + 3 * 4 now we'll have a different result sorry if you run is. So let's wait. We get 24 here down because now it's first because we put parenthesis. This has precedence over the multiplication. It will do first what is inside the parentheses. So we have six and then the six is going to be multiplied by four and we get 24. Now sometimes u in your programming you will want to uh add a specific value to a number and uh in order to do to do that there is something uh there is a shorter way to do those things. So let me show you what I am trying to say here. So sometimes you want to add let's say two to a variable. What we can do is we can type here let's declare in again the variable val result and uh let's put equals to x + x + i and let's go down here. Now what I can do here I can type result equals to result + two. So let's say that you want to increment a variable by two. You can you can do it in this way. You can type a result. So the variable and this mathematically is incorrect because result is not equal to result plus two. But this operator equal in programming is called the assignment operator. So it assign what's in on on the right to the variable that is on the left. So we are assigning now the result plus two which is uh we have 8 + 2 which is going to be 10. And uh let's add a print line here. Let's type result equals to let's put dollar sign result. Now let's do the same thing for the next operation. So I'm going to type here result plus result result equals to result plus minus sorry two. So now we are we are subtracting for the result which has the value now I think uh let me think about 10 it will subtract two to the to the from this variable result and it's going to assign this value into the variable result that's defined here. So let's add a print line here. Let's put result equals to dollar sign result. Then I'm going to type result plus equals not this plus. We're going to look at that immediately. Result result result times two print line result equals to dollar sign result and let's copy this for the division and the modulus. So let's put here division and modulus. Now let's run our app to see what we get in the output. So first we get 10 because we are adding to the variable result which has the value x + i which is 8. We're adding here two and we're assigning that that uh the value of that expression in this variable result. Then we output the that new console. So we have 10. Then we have eight because previously now the variable has the value 10 and we subtract two. So we have we have uh eight. Then we we have what we have in here result equals to result * 2. So we are and here is uh 16 because we have eight now and 8 * 2 is 16. So we get 16 here. Then we have result divided by two and we get 8 because 16 / 2 is 8. Then we have the modulus modulus. So the remainder of 8 / 2 is zero. So we get zero here. And now let me show you the interesting thing. As you can see this is underline. And this is underlined because there is a shorter way of writing this because those things are so so often used in programming they they created created a shorter way to write the same thing. So if I click here replace with plus and equals now we have the same thing but we have the we have this abbreviation I written in this shorter way. So let's let's do the same here. replace with replace with the same here and the same here. Now sometimes you'll want uh in your code to increment a certain variable by only by one and you'll want also to decrement a variable only by one. And for that we have a special operator which is called the increment operator and the decrement operator. And to do that I'm going to type here x equals to zero. But let's change that to v. And I will type here print line x++. Then I'm going to put dollar sign curly braces. And here I'll type x. And to increment a variable only by one, we put plus. And this is called postfix incrementation. And the next one, I'm going to add a print line here. Let's press ctrl d. We have also prefix incrementation and there is a difference between them a very important difference which is going to show different results. Okay, this is called postfix incrementation because what this will do when this line of code is going to be executed it will output the value of x in the console and only after the output is done it's going to increment the value by one. So you'll see in the output zero even though here it's incremented but now because it's post fix incrementation the variable have the the value one. Next this is prefix incrementation. What the prefix incrementation is doing is the opposite. It's incrementing first the variable and at the same time it's outputting it value. So we previously we had one because incremented this by one but as I said it will first outputs to zero because it first outputs the value and then it increments the value and um this variable now x has the value one and when it this line of code is going to be executed that is defined here plus plus x. Now this will be incremented by one and it also going to output in the console the value which was incremented which is in this case x and that variable is going to be two. So now if you run this code let's right click runman.kt. We got the expected result but let's put an equals here. equals equals let's run it again. So we have zero and two as expected and uh there is uh the same operator for decrementing a variable by one. So I can type here print line print line x - equals to dollar sign x -. So this will decrement the variable by uh by one. But again this it will be the same uh like uh the previous it will be a postfix incrementation. Let's press Ctrl D and plus plus not plus+ uh minus min - I here but is before the variable. So it's going to be prefix decrementation now. And now you'll see the opposite. You'll see 2 and zero. So let's run this. So we get x - -2 as expected and then we get - - x 0. Let's also explain this to to don't make things confusing. So this variable now has the value two from the previous uh incrementations that happened here. And when it comes down here to execute this line of code now the because here we have postfix decrementation the first the the value of the variable the variable is going to be output in the console which is two. So it's going to be output two and then we decrement it by one. So now the x has the value one when the next line of code is executed. Now it will decrement the value again by one and now it's going to be zero and it will output this value in the console and now the resulting value is going to be zero. So this is what you get here. This why you get here zero and two and here two and zero. All right. Now I'm going to end this video. All right. In order to introduce the next operators that you can use in Kotlin, I first need to introduce a control flow statement. Specifically, I'm going to show you how to use the if then else statement expression. And we're going to see later why is called a statement expression. And uh we need to use a control flow statement because those operators can only work in conjunction with a control flow statement. We can use them only in conjunction with a control flow statement. So I'm going to declare a variable here. It's going to be a val going to be called is active. And I'm going to assign true to it. Right? Then I'm going to press enter. And on the next line, I'm going to type if and wait for the suggestion. We have parenthesis. We can press enter to fill that for us. And inside the parenthesis, we define our condition. And then we put curly braces. So I'm going to put the left curly brace. I'm going to press enter. And the right curly brace is going to be added automatically. So inside those parenthesis, we define our condition which can be either true or false. And if this condition inside the parenthesis is true, the code inside the curly braces, the code inside the this uh the block that is defined by those curly braces is going to be executed. If this condition is false, the code is not going to be executed. So I can type here if is active. And now I'm going to use the equals to operator. So I'm going to put equal equals. So look at the difference. This operator that we have here is a single equals called the assignment operator because it assigns what's on the right to the variable on the left. This equal to operator is it's an operator which checks to see what that if the operand on the left is the same as the operand on the right. So if I'm going to type here it's active equal true. Then I'm going to type I'm going to type s for shortcut for the print line and I'm going to type the condition is true. So now if you run this and we get the output the condition is true because it's active has the value true and here with the double equals we checking to see if this uh what is inside the this variable it's active it's equal to true and it's equal to true because it has the value true and it outputs in the console the condition is true but if I change this to false. So now it's not equal to true. It's active. Has a different value. And if you run this, we get nothing because the condition is false and the code inside the curly braces of the if is not executed. But what we can do in this scenario, we can add an else block. So we can put here else. So if the condition is false, do something else. I'm going to type here SO. Then I'm going to type the condition. So this else part is going to be executed only if the condition inside the if is uh false. So I'm going to type the condition is false. So now if you run this now I get the condition is false as you can see here because the if first checks to see if the if the condition inside the parenthesis of the if is true and it's not true is false and then it executes the block of code of the else because the else execute if the if if the condition inside the if is And this is called the equals to operator. Now let's uh but before we I'm going to show you another operator. Let's hover over here because it says that you can simplify boolean expression. So what we can do is we can delete this equals equals to true and we can simply put when we have a boolean variable we can simply type is active and this is equivalent to checking is active equals equals true because now if you run this the output will be the same. So if you run this we get the condition is false. And if I change this to true and if I run this now we'll get the condition is true. You get the condition is true. Let's put a single. No. Let's delete the yes there. Let's run it again. and we get the condition is true. So is active without equals equals to true or equals equals to false. It's active is doing the same thing as having equals equal true. So you check to see if this is if this variable has the value true. It's an abbreviation of uh of what we had previously. Next, let's introduce the next operator and that is the greater than or equal to operator. And I'm going to create here a variable val my number is going to be in camel case the name. I'm going to assign here five. I will delete this variable. It's active because I'm not going to use this now. And now this is in red because it doesn't exist. And I'm going to type here if my number my number and now I'm going to introduce the next operator is greater than four. So what we are checking now is is the value assigned to my number variable greater than four. Let's run our code to see what we get. Let's actually change the text to um the number is greater greater than four. So let's run this. So what we have in the parenthesis is going to be evaluated to true or false and in this case going to be true and get the number is greater than four. Now if I um put here five so if is my my number greater than five no it's false. So it's going to say the condition is false. So I get the condition is false. It's executing the else part because the if check failed. So this is false. Let's press uh shift control p on this. As you can see by looking at the popup boolean there. This is a boolean expression. So it values to true or false. And um if I put here greater than or equal to five, look what happens now. If I run this code now we get we get the number is greater than four. Why? Because now we have a a different operator and this is the operator greater than or equal to. So our number is not greater than five but it's equal to five. So it evaluates to true and it's executing the code inside the curly braces of the if which is this print line and u when you when you have a single line of code you can omit curly braces. So you can delete curly braces and you can uh execute the code. So let's run this to see that we get the same output. But use this only when you have a single uh a single line of code. when you have multiple lines of code, you'll always use curly braces because if I put here curly brace, so let's press Ctrl Z actually to have what we had previously. Now let's uh delete the else because I want to show you what happens if you don't put curly braces and you have multiple lines of code. So let's say that I want to to have this condition to be to be to false. Let's put now my number is greater than five which is not not true. My number is equal to five and I want both those lines of code to be skipped. So I don't want this code to be those lines of code to be executed. So if I press Ctrl D multiple times here and uh with curly braces now if I run this and the code is inside the code block how it's called this is how is standard code block. So if the the lines of code are inside the curly braces they are part of the if this condition fails the code block the block of code and implicit all the code inside the curly braces is going to be skipped but if I don't use par uh don't use curly braces and now if I run this look what happens we get only the first uh line of code skipped. So this is skipped because when you don't use curly braces only one line of code is part of the if or all the of the if and uh the next lines of code are executed because they are part of the flow of the program. They executed because they are the next line of code. So this is why you need to use curly braces. Let's so let's put Ctrl Z. If you have multiple lines of code again always use curly B. So let's press Ctrl Z to have the previous code. Now what you can also do is that you can chain more ifs here. So you can type here else if and we have a suggestion with parenthesis. Let's put parenthesis. My number my number is greater than or equal to four. Let's put curly braces and press enter. So now we chained another real statement. So let's press Ctrl Alt L to format the code and I'll add a line of code here. Let's type SOT2 and I'll type here the number is greater than four and here should be five. Why I type here for four five? Now let's change this to four. And let's run our code. So we get the number is greater. So is greater than four. So this line of code this uh check here if evaluates to false and then the else if check evaluates to true and the code inside the curly brace of the else if is executed and you can chain more else if LCS here. So you can chain as many LCS as you want here with various conditions. Now I'm going to end this video here and see you in the next video. Right. So now let's continue our discussion about the next operators that you can use in Cotlin. And let's start with the less than operator. And to do that, let's first change the value of the variable my number to 100. And to use the less than operator, you we put here a less than sign, which you probably know from your mod class. Now if you run this code, I should change here the text to make it consistent. Let's type here. So now if you run this now get the output less than 150 and this is true because the value of the variable my number is less than 150. So this condition now evaluates to true. The next operator that we can use is less than or equal to similar to greater than or equal to operator. And to use the less than or equal to operator we put here a less sign and the equal sign. Right? And let's change the value here to 150 to make it clear. Now let's change also the the value of the variable to 150. And let's also change the text here. Now if you run this code you're going to see the output less than or equal to 150 because now the else if part and the condition of the else if statement eval to true because now the the value of my number it's not it's not less than 150 because it's 150 but it's less than or equal to 150. So this condition evaluates to true. All right. The next uh operator that we can use is called the logical not equal operator and that operator is used to check if two operent respectively to if my number let's say or and 150 are not the same. Let's see how we can do that. And to do that to type here an exclamation mark and an equal sign. And this operator here which is like make a mark is going to check to see if those two operands respectively my number and 150 are not the same and only then the condition is going to evalate to true. So let's run this code to see what happens. So now we get the output less than or equal to 150 because we still have the value 150. So it jumps to the LC part. But if I change this to let's say 100. Now look what happens. Now it executes the if. Now this condition evaluates to true because now this operator is checking to see if the two operands if the value of my number the value of my number is not equal to 150 and that is true they're not equal and this is why you get here the output but let's change the output to make it clear let's type here um they are not equal. So if you run this code going to see the output are not equal because that condition now evalu. So this is how you can use the logical not equal operator. Also with the logical not operator we can change the value of a boolean expression from true to false and from false to true. In other words we can negate its value. And to do that let's first declare the code that we had previously. So I'm going to create again the val variable it's active. I'm going to assign a value of true. I'm going to type the if statement again. I'm going to put the same condition equals equals true. Here you can put the left curly brace and press enter or you can add the the right curly brace if you want but it will be added automatically for you. I'm going to type else also the left curly base. Press enter. I'm going to put the text that we had previous. All right. Now if you run this code you're going to see the output uh the user active because this is going to evalate to true. They are equal. They're the same. Let's expand this. So you see the output the user is active because this conditional is to show true and before we look at the logical not operator how we can get the value let's first see how we can write the same expression that we have here in a shorter way by abbreviating. So because you have noticed probably that we have an underline here and if you hover over here it says that the boolean expression can be simplified. So let's click here simplify boolean expression to see what happens. So now we get if it's active and uh then print uh then execute the code inside the curly braces. So this thing now does the exact same thing that we did previously. It's so it checks to see if the value of it's active is equal to true. So it's a shorter way of writing the same thing. And u now you get the output the user active but with the equal not operator you can put the exclamation mark in front of our active expression. And it's now now it's going to negate it value. And now it's going to change this value from true to false. So now if you run this code now you're going to see the output the user is not active because now this uh logical not operator switch is switching it value to false. So now this is going to be this will evalate to false and it's going to execute the else part that we have the user is not active. And we can do the same for u false. So if you put here false now let's actually execute this without the logical not operator to see what happens. Now this will not execute. So it will execute the else part. So you see the user is not active. But if I put the logical not operator in front of it's active. Now it's going to change this value from false to true. And now this will evaluate to true and it it will execute the uh code inside the calibration of the if. So now you see that it says the user is active. So this is how you can use the logical not operator to flip a value from false to true or from true to false. And you can do this for for every boolean expression that you use. So you can put the logical not operator in front of this actually not in front of this. um you can put the logical not operator generally speaking in front of any boolean expression. Right? So this is our discussion about u operators. So in the next video we're going to look how we can combine multiple expression to create a bigger expression which itself returns true or a false value. See you in the next. All right. So I deleted the code that we had previously because in this video I want to introduce another operator called the logical end operator and the logical end operator is used to combine multiple expressions which themselves going to return true or or false and u I'll type here two variables val is active going to be equal to true and val score equals to 80. Now I'm going to go down here and I'll type if parenthesis. I'm going to type is active. You can type is active equals equals true, but it's going to underline and it's going to say that this is redundant because you can simply put is active. It will have the same effect. Now to use the logical end operator we put two emperson signs here and we put now our second expression score let's say equals equals to 100 and the logical end operator returns true if all the expressions are true. So in this case if the is active it's equal to true and if score it's equal to 100 which is going to return true or false only then the logical end operator is going to return true. So I'm going to type here s you are at the next level. Let's put an else. Let's type you are at the same level. So now if you run this as I said both expression has to be true in order for the logical end operator to return for the and for the whole expression that we have here to return true otherwise it will return false and it will execute the else part. So let's wait for the output and we get you are at the same level because this evaluation room only if both condition are true. So if I put now 100 here. Now if I run this now you get you are at the next level because now both expressions respectively is active and score equals equals to 100 both are true and the entire expression returns true. And in fact, if you press shift Ctrl P here and you select this whole expression, as you can see, this whole expression, it's a boolean expression. So this entire thing that we have here, highlighted in blue, it's a an expression which is going to return either true or false. If both because we're using the logical operator, logical end operator if both expressions are true respectively, it's active is true and the score is equal to 100. So you can use the logical end operator when you want to combine multiple expressions and the logical operator works in this way and the way is that it returns true for the whole expression if all the expressions are true. So if you add another variable here like val let's say internet I don't know let's say speed let's put 4,000 here and if I put another logical end operator here internet speed equals equals to let's say 5,000. Now if you run this because now if I press shift Ctrl P on this and if I select the entire expression so we have its active equals to and uh then we have score equals to 100 and 10 speed equals to 500. I choose a random number there is not does not mean anything. And if I select the entire expression which is highlighted now. So if you press shift control P and you select the entire expression highlighted in blue. As you can see the entire expression is going to return true or false. And uh with the logical end operator all expressions this expression this expression and this expression have to be true in order for the um for the whole expression to be true. So for the whole expression to be true both all the expressions have to be true. So it's active has to be true. It's actually and score has to be true and intern speed has to be true. So all the expression have to be true. So now if you run this because we have the 4,000 value and here we're checking for 5,000 get you are the at the same level because this is false. So the entire expression returns false. Now if you change this to 5,000. Now if you run this we get you are at the next level because now all expressions so all expressions is active equals true score equals true and internet speed equals true and the entire expression now evaluates to true. So you get the true back and this is why get your the next level and you can put another end operator here. It depends on what you trying to build what program or what up. All right. So I'm going to end this video here and see you in the next video. All right. So now it's time to continue our discussion about operators and going to look at the next operator which can be used to combine uh multiple expression and that is the logical or operator and the logical or operator evates an expression or multiple expression in this case to true if one of the condition is true or if both condition are true. So let's see an example of this. But first let's comment the code that we have here because it will make things confusing. So go to code and select comment with light comment with line comment or you can press the the shortcut control slash. Now let's declare two variables down here. The first there the first is going to be a val. I'm going to call it num one. I'm going to assign a value of five to it. Second is going to be also val. It's going to be called num two. And I'm going to send a value of three to it or four. Now I'm going to type the if then else statement. I'm going to type the if keyword inside the parenthesis. I'm going to put the condition. First is if num one is greater than zero. And now to use the logical or operator. We type two vertical bars here. Now we put our next condition or our next expression. And that if num two is also greater than zero. And we put a curly brace. Press enter to to add the right curly brace. And we put also the else part. And inside the curly braces of the if. Let's print some text. Let's say the condition is true. All right. Now, if you run this code, you're going to see the output the condition is true because now both condition are true and uh the logical operator returns true if both condition are true or if one of the condition is true. So let's see if we change this to minus three. And now our second condition num to greater than zero is false. So now this is also going to evaluate to true because the logical or operator returns true if one if one of the condition is true or if both condition are true. So now get condition is true. But if you change this to also to minus five. Now our both both condition are false. Now you're going to see that it's going to be executed the else part because this condition now is going to be false because both conditions both expressions num one greater than zero and num two greater than zero are false. All right. So let's change it back to five. Now if you run this code you're going to see the condition is true because at least one of the condition is true which is this one. N greater than zero even though the num two condition is false. All right. And this is how we can use the logical or operator. All right. Now it's time to see how we can use the if then else statement as an expression. And that means that uh the if statement can return a value which can be stored in a variable. So let's see how can you can do that. But first uh actually let's skip this code that we have here. And uh to use the if then else statement as an expression, we declare a um a variable first. So I'm going to declare a val. And let's give it a name. I'm going to call it let's say text. And to use the if then else statement as an expression, we put equals. And after the equals, we put our if then else statement. Now we need to assign a value to our variable text to use it as as an expression. And the value is going to be the last expression in the in the curly braces. So here if I type the text let's say uh this uh is text one and down here if I type in quotation marks this is text two. Now if I add a print ln here at the end and if I print the value of the variable text that we declare above. Let's put a dollar sign. And if I type text here, now you can see our variable. It says it's of type string. So if I run this code now, you're going to see first this print ln inside the curly braces which says the condition is true. And uh after that you're going to see this text being assigned to our variable text and u it will be output out outputed down here by the print ln statement. So you see this is text uh two actually I was wrong that condition is false because uh here we have minus 5 and - 3 and this condition is to false and u the last the last expression in the in the curly braces is assigned as a value to our variable. Now um what you can also do is to remove the curly braces but uh it's recommended to use to not use the curly braces if you have one only one statement uh in your if then else statement. So let's delete the print ln here. Let's delete the curly brace here. Let's also delete the curly brace here. Let's delete the print statement here. And let's also delete the curly brace here. So now this will work the same uh it will work like previously but uh so now you'll see the same output without the condition without this state the condition is false because now we don't have the print statement so now we get this text too because this condition evaluates to false and it assigns this value to our variable text then down here it prints so also you see that this uh they are not aligned here this our variable and to format your code you Just go to code and click reform reformat code or press Ctrl Alt L on Windows or you can um use another shot. You can use the corresponding shortcut on Linux or Mac. So click on code and now you're going to see that our code is formatted. If you run this now see this is text two but let's change this to five. Now this condition level to true and this expression is going to be is going to be assigned to our variable text. So if we run this going to see this now our variable text is assigned the value of this is text one because this condition is true and assign this value to make things more clear you can um use uh you can you can uh type your code something like this. Now this does the exact same the exact same thing as previously. So if you run this code now, you'll see that now this it says the same thing. This is text one. Now you may be wondering what type this variable is. So if you hover over it, it says that it is of type string. But look uh what can I do? I can put here a number. So if this condition evalates to false, this is going to be assigned to our variable test. So if I run this now, now I get this text one. And let's uh put here minus five for the condition to be false. And if you run this code now, now you're going to see five. And you may be wondering how this uh makes any sense because um previously our variable was of type string. But if you hover hover over here now it says val text and the type is uh this thing comparable anyway. Um but what is uh simply doing here is looking at the condition. It's looking first at the if statement and u if this condition is true then it assigns our text to our variable and then it infers the type to be a string. Right? If this condition is false then this part is skipped and then it assigns this value of five to our variable text and not and then uh our variable is going to be of type int. So there is no case in which both uh both u values going to be assigned to our variable. So this is why you can use here text and here an integer even though but if you declare explicitly here that this variable is going to store only strings only sequence of characters or text. Now he'll get an earning because it says the integer does not convert to the expected type stream. So in other words we cannot put here a number because we said that explicit here that is going to store only strings. But if you omit the type that is going to work. So you can decide what uh depending on the context what you choose. So this way of uh writing the if then else statement in one single line like we have here it's okay as long as we use the if then statement as an expression or in other words if you're assigning um a value to a variable like in our case. But if you have multiple instruction in your um also if you have a single single instruction you can also omit the curly braces but um most of the time it's recommended to use the curly braces because that allows those allows us to execute more than on u instruction or statement in your code and uh it also makes the code more clear. So let's change this code uh from an expression to just an if then else statement to see why. So I'm going to change this. I'm going to add the curly braces. Let's format the code. Press control uh alt L. All right. And also let's change this. Let's delete this uh text. Let's add two print ln statements here. All right. Now if you run this code, you're going to see this part execute else part because this condition is false. both uh of our condition respectively num one is not greater than zero and also num two is also not greater than zero. So this is going to return false and it's going to execute the else part. All right. But now if you change this to five and now one of the condition is going to be true and uh as you know this entire expression is going to is going to evaluate to true. now because at least one of the the expression is true. So if you run this code now I see this condition is true and also we see our our second print align statement this condition is true and some text but um look what happens uh if I uh let's say if I u delete the else part, right? And if I delete the curly braces here. All right. And now if I run this code, you see the now both statements are executed. But as I said, if you don't use curly braces, just only one of the uh statements in is going to be executed. So why you have both of these? because first is exe executed as part of the if uh statement because this is true and the next one is executed because it's part of the code because it's read from top to bottom but if uh I uh change this to false and now I want both condition both uh statements uh respectively this one and this one to not be executed look what happens I get this condition some So the first uh instruction in our if then statement it's omitted because it's part of the if then statement but the second one it's uh executed because it's part of the code so it's going to be read anyway. So this is why you need to always use curly braces. In this way the entire code is al is going to be either executed or omitted. So this is our discussion about the if then else statement and uh you should always use the curly braces and for the else part also you should use the curly braces and um only omit the curly braces if if you have only single one single uh statement or one single expression if you want to use the if statement as an expression to assign a value to a variable. But if you have just one single uh statement, let's say you have something like this, you can omit the curly braces like this. So now you can see that everything works uh fine. So you can use it like this. If you have one single statement like we have here, you just print some text to our console or if you use it as an expression. So if you store a value in a variable. So see you in the next video. So while I was recording this course, Jet Brains have released a new version of IntelligJ. And you'll see in uh the course when when I create a new project, you'll see a different window. It's not that different. It's slightly different than this window than the window that you will see when you download IntelligJ ID which is this version now which has this window and when you see this window here you just need to specify a name here you specify the location for the language select cut here you select for the build system intelligj and you make sure to have the JDK here and you also check this add sample code and that is going to generate the main function for you and you click on create and the project is going to be created for you. So I thought that it's good to tell you about this and uh if you you'll see this window and if this window changes in the future I'm going to release a new video in which I'm going to show you how to if they change this specifically the window of creating a new project how to create a new project with that window but for now this is the window of the latest version now it's time to start a discussion about nullability in Katlin but first I'm going to create a new project I'm going to call it null Make sure to have the language cutlin selected and for the build system intelligj. Also make sure to have the JDK selected here. And I'm going to check this little box to generate the main function for us. And that code that comes with the main function. So I'm going to click on create to create a new project. Now our project is going to be created in a short moment. So, we got our project created and we get a we got our main function uh autogenerated here for us because we check that box. So, I'm going to delete this because I don't need this code and I'm going to hide the project pane. So, what is a null value? A null value is basically when you assign to a variable nothing. Synthetically speaking, you assign null to it. But what that means is that you have a variable which has no value which has no memory reference and if you try to do some arithmetic operation with that variable you'll get in an impossible scenario where you have nothing try to do some work and that will throw a null pointer exception and this is particularly bad because n the null pointer exceptions are only thrown at runtime and not at compile time. So you could have uh your program working well and having no problems and at some point you try to I don't know press a button and somehow somehow that pressing of the button is u using that variable which has a null value and then it's going to throw a null pointer exception and your app will crash and cotlin aims to avoid and to eliminate null pointer exceptions and null values that is not to say that we and have a null value in Kotlin but it's very hard to have one. So let's see why. So what Kotlin has done to make null values and implicitly null pointer exceptions very hard to get it first made all its types by default non-nullable and that means that you cannot assign null to it. So if I declare here a variable called text I'm going to declare the type explicitly. So it's going to be a string. And if I assign null. So to assign null to over we'll just type the null and we have the null keyword. We can't because this variable by default and all the variables by default are non-nullable. And that means that you cannot assign a value. So if I hover over this underline, it says null cannot be a value of a non-null type string. But what if I want to assign null to it? What if I want to have null for some reason? In that case you go at the end of the type and you put question mark and that means that now you have a nullable type that means that you can assign null to this uh variable. So if I put question mark here now I can assign null to this variable. So this is u how we can assign null to a variable by putting the question mark at the end of the type because that is saying to the compiler hey I'm going to let I'm gonna let me to assign null to this variable. So to avoid that underline red. Now if I try to print this. So if I put here print line and I put here text what do you think we'll get in the output and output get null you know do you don't get null pointer exception. So now if I hide this and u let's say that I want to get the length of uh let's say of the text. So let's say that uh I assign here uh some text actually let's put a name here name and let's and uh if I try to get the length here so if I put here dot length I have an underline which says only safe and we have this question mark that on non assert or asserted and we have this uh this operator calls are allowed on a nullable receiver of type string. So it's saying that we we can um we can only get because this this variable can have a null value but we know that it doesn't but the compiler is saying to us because we said that this can have null is saying to check first that this variable is not equal to null and then try to get its length and let's do that in the long way first. So let's delete this. So let's actually just copy this and we type if we put parenthesis if text not equal to null. Then we're going to output it. Then we're going to get its length. So we're going to type here text.length. So now the compiler is happy because it's it's uh it's ensured that it's not going to have null because we checked here especially that this variable uh should have you should not have null in order to get the length. So now if I run this I get four. So this is the length. So we have four letters here and I I can also add the else part. which is which is going to say the very well is null. So if the variable is null, the else part is going to be called. So now if you run this, we'll get also four. But I'm going to assign I'm going to reassign this variable. So I'm going to change it to var first. So we put here v and put here text equals null. So now our variable is null and this uh check is going to fail and it's going to execute the else part. So if I run this code so get the variable is null because now on the third on the third line we assign null to it. So this if fails and is executed else part but as you can see it's a lot of code just to check to to check and imagine if you have multiple variables to check to to always check for this that the variable is not no then print it length and there is a shorter way in cotlin to do that and uh we saw as a suggestion how to do that previously. So if I press Ctrl Z to um undo our code. So if I press still pressing now if I hover over this error that we had previously which says only safe call. So and we have this uh operator and this is called the safe call operator. And if I put here just a question mark. This is equivalent to having a if which is checking to see if the length is null and if the length is null then it's going to say null or and if it's not null it's going to just output uh four. So now if I run this so this is equivalent to the if that if the the if statement that we had previously. So we have four. So but if I and if I assign null so if I type here uh let's change this to v first. So if I assign here text equals null, we'll simply get null. We get null. But what if I want to get that null pointer exception for whatever reason? What can I do? Al the compiler it helping is helping us also there. So because if I delete that safe call operator which is just a question mark that and that is basically saying hey if this text is not null then output in the console u then get then get the length and then output that in the console. If it's null just say null. So this is how the safe call operator works. But if I delete this as you can see there is another operator and is and is this operator which is um two exclamation marks and this operator is basically saying hey if this variable has null then throw that exception but if uh it's not null just output just output the length. So if I put two exclamation marks here, but for whatever reason it's not uh our text length, it's not getting the length. And I think this this is because we assign to our text variable here null. It um it can't it has a it has a pro a problem with inferring what type this variable is even though here is saying that it's a string. So we should uh should we should be able to call that length and to avoid this you just put null at uh when we declare the variable. So we assign here null. So we put here null. Now if you run this because you have the two exclamation mark operator now this will throw that null pointer exception that I talked about. So we get exception in thread main. So it doesn't matter what trademar is. Now open you get java lang the null pointer exception. So this is the the null pointer exception. But if uh we assign down here this variable text equals uh some text. And now if you run this code you get nine because this is the length of this. So this is how you can use this operator. The next operator that you can use is the Elvis operator. And to show you how uh that works, I'm going to delete this code. Now I'm going to declare another variable up here. So I'm going to call it text two. We put equals and we type text. Now to use the Elvis operator, we put a question mark and a colon. And now we need to uh type something on the right of the alphace operator. So we put here some text. I'm going to explain immediately how this works. So I'm going to type here u some text. Now what the Elvis operator is doing is saying if this variable text is null then assign what is on the right to this variable text two. If this uh variable text is not null then assign what is to this variable to our variable text uh to our variable text two. And now let's uh let's type here text two and uh let's delete the length. So now if you run this code, let's actually put something more discrete like uh the variable is null. So if you run this code now, we get a variable is null because it uh check to see to see if this variable on the left is null and if it is null, it's going to it assign this to to a variable takes two and then output that in the console. But if this is not null, then it's going to assign what is on this what whatever value this variable has to our variable takes two. So if now I put here text equals this variable is not null. Now if I run this now this will this will evaluate to true. So it's going to assign whatever value is on the left to our variable text to our variable text two and we're going to see in the output uh the variable is not null. So when a variable to x2 has now the value assigned from for this variable. So this is the shorter way of writing. Let me show you how we will write this and the longer way. So in the longer way you will have something like this. So let's delete this quad actually. Yeah. Let's delete this code. We'll have a variable called text. two and u it's going to have an empty string. So what what we would have to do without the lvis operator we have to first check if text uh not equal to it's not equal to null then um going to call our variable text two and we're going to assign text to it. else we're going to assign to our text to variable the text this variable is null. So this is the longer way we doing of what we did previously. So if I press control to format the code. Now if you run this you will see the the same output the variable is not null. So we get the variable is not null and this is the longer way of doing the same thing. So if I assign here uh if I assign actually if I delete this and the variable will have null. Let's press Ctrl Z. If I run this now the else part is executed. So our variable text two it will be it will have the this is variable. this variable is null. The text assigned to it and then it's going to output that in the console. So this is the longer way of doing what of what we had previously. So if I press Ctrl Z to have the Elvis operator back. So that was the longer way of doing what of what we're doing here in one simple line of code. So this our discussion about nullable types and I hope that you got a lot from this uh video and see you in the next video. Right now it's time to start our discussion about functions. But first, let's create a new project. And I'm going to speed up the process now a little bit because we already did this a few times. So I'm going to call it functions. Should be in Pascal case. Select intelligj. Click on next and click finish. All right. Let's delete the code inside the curly braces. Let's hide the project pane. And uh let's start by saying that until now we have written our code only inside the main function and uh occasionally we have called the print line function to output some code to the console. But u as I said at the beginning of our videos you can uh create your own functions in kine. So let's see how we can do that. So go down here at the end of our main function at the end of our enclosing curly brace and to declare a function we type the keyword fun. We put space next we need to give a name to our function and the name of the function should say what the function is doing and uh generally it's a verb. I'm going to call this function say hello and it should be in camel case. Next you put parenthesis because uh functions can take input to work with it. And next we put curly braces because this is where we're going to put our instructions for our statements. This going to put our logic. Now let's add the print len statement here. Let's type the text hello. And now let's run this code. Now as the input we don't see nothing here because if you hover over this say hello function it says function say hello is never used because in order for that this in order for this function to be used we need to call it from main function and to call it from main function we type here the name of the function you can type it uh entirely you or you can type start typing in intellig is going to be is going to give you some suggestions. So press enter to fill in fill in that for you and also press Ctrl Alt L to format the code. Now let's run our code. Now you're going to see that it says in the output hello because the main function executes what is inside the curly braces and uh on the line two it says that we have a function that we're calling here a function executes it executes the the say hello function the code inside the say hello function in this case is a simple print ln statement and then uh it outputs that to the console and uh it exits the the code it finishes the process because we don't have anything else below for say hello. But what we can do is put another say hello here to call our function again. And now we're going to see the output hello two times because now we're calling the say hello function first on the line two and second on the line three. So it will be executed two times. So you can see that in this way functions are reusable and are separate chunks of code can which can be can be reused and you'll see that they can be used in specific cases. Now as I said functions can take input and to make a function to receive some input we need to define what is called a parameter and to define a parameter we type the name of the variable first. I'm going to call it name. And we need to explicitly provide a type here. So for the type I'm going to choose string and we need to explicitly provide a type here because is there there is no way for the type to be inferred here because u the value that you're going to pass to this say hello function is going to be here. So it's impossible to know to what kind of value we're going to pass to it. So this is why you need to explicitly say what data this parameter or this variable is going to store. So let's delete this second say hello. Now let's uh change the text that we have here in the print ln. Let's uh let's refer the parameter name. So put a dollar sign and start typing name. And you can see that has this p for parameter and press enter. Now what is going to happen now is that this say hello function is is going to be called is going to be executed from our main function and uh the code inside the say hello function is also going to be executed and specifically it's going to execute this uh this uh text that we type here to be outputed. It's going to execute the hello and then it's it's going to execute what's what value if we have passed as an argument for this parameter. And uh if you don't know what an argument is, the argument is the value that you pass to the um to the function. So in this case up here we need to pass u some text. So I'm going to put the name here, not here. It should be in double quotation marks and you see that we have a hint here which says which which says the name of the v the name of the parameter which is name. Now what is going to happen now is this this say hello function is going to be called uh and this value that we passed here is going to be used in our print ln statement where uh where where we referring now which is uh here we're using the name and it's going to use that value and that value is going to be outputed down here and let's run the code to Okay, now we get hello Alex because now it's using the value that we pass here as the as an argument to the parameter name and it's using that uh value down here where we type the print ln statement or function. Now functions can take multiple arguments, multiple arguments and uh to do that we need to define u I should say that you can define more parameters for the function and a function can take multiple arguments and to define another parameter we put comma here and uh we type the name of our parameter is going to be age And you need to provide the type. And I'm going going to provide the type as an integer as an integer because it's going to store whole numbers. And uh here we're going to put an exclamation mark. And I'm going to type your age is now we need to refer the parameter age and also you need to provide the value for that parameter. Now if you go up here now it says that you need to provide a value for the parameter has age. It says no value passed for the for the parameter age. So here you need to type on a value for our parameter that we define. So let's say 22. All right. Now if you run this code now those parameters that we define here are going to use the values the arguments that we type here inside the our curly braces or how is often called the body of the function or the cold block. So it's going to use the values here and here. Now another important thing to know is that you cannot change the value of a parameter. So if you type here age equals let's say 30. Now you're going to if you hover over here because you have an underline it says val cannot be reassigned because those parameters are are declared as val as variable false. So that means that they are immutable. You cannot change their values. The only way to use variable which can uh have this value change is to declare on. So if you declare here a a variable I'm going to declare as a v to change this value I'm going to call it number and uh if I uh assign here a value of 50 now I as you know you can change now it value to let's say 70 and you can also assign the parameter here to our variable number and now we can change this value also can put number and you can put another value here. All right. Another important thing to know is that this variable that we define here also the parameters are scoped inside the function say hello. In other words, they cannot be referred or accessed outside of the the calibrations of the say hello function. So if I try to access that uh number variable here, it's not working. If I try to access access here on the main function, let's say I type number, I can't because it's scoped inside the say hello function. Uh it exists only inside the say hello function. The same thing with the parameters age or name. So in this way you can see that functions can uh be very useful because they are uh chunks of code which are reusable and uh flexible and um to see an example uh with this let's declare another function down here let's call it u let's type the fun key or let's call it get data and uh this function is going to mimic the a real function in a real app which can get some data from the internet. Of course that we're not going to get on the internet but we're going to u mimic that behavior. So here I'm going to define a parameter. Let's call it data. And for the type let's choose string. Right? Let's uh put a curly braces. And here let's add the print len statements. And uh here we're going to print your data is and going to refer the parameter data. All right. Now what I can do up here is to define a boolean uh variable. It's going to be a val. I'm going to call it has internet connection. I'm going to assign a value of true to it. Now I can put an if statement here. Now I can check to see if that uh expression is true. So I can put t= equals true or use the shortcut because it's better here. I'm going to put the curly braces. And now I'm going to call the get data function only if this condition is true. So in our case is true that but in a real lab that can be can be false. So in that case you can do something else. As you can see we're going to let's also add the else part here. And here we're going to call another function which can which is going to be called in the case that uh there is no internet connection. All right. So above here you can put some logic to get the data from the internet. But because this is a simple example we're going to type here directly um some text. Let's put some data. And uh for the else part in the case that there is no internet connection that variable is false. We can declare another function down here to and I'm going to call it fun show message and uh this is not going to have any parameters. It's going to have only a simple print ln statement and it's going to say that uh there is no internet connection. All right. So now let's go up here now. And now we're going to call the show message uh function in the else part. Now if you run this code, you're going to see hello Alex, which is our uh first function up here on the line two. your age is 22 and next you see your data is some data because it uh executes the say hello function then it declares this variable and uh it checks it checks to see if this condition is true and this condition is true and uh it executes this function get data so uh this is why get your data is some data but if for whatever reason the user has no internet connection let's say you assign a value false to it to this. Now this function is going to be called show message. So this function show message now is called and the code inside the show message function is called which is print ln is this print statement which says there is no internet connection. In a real app, you can have some logic to show a dialogue that there is no internet connection. And uh you can see that in this way with functions, you can divide your uh code in different chunks of uh different in different chunks of code which can be called in very specific cases. So in this case uh we only call our functions only in in the case that we have internet connection or in the case that we don't have internet connection. So in this way uh we have divided our code in different uh chunks of code which can be called in different uh uh specific scenarios. And in this way uh we can write a program because uh if you just put your code all of your code in the main function and you execute that code from top to bottom you you cannot create a program like that. So um this was an additional uh discussion with the get data and show message and um I hope that I not confused you a lot. So see you in the next video. All right. So in the last video we seen how we can uh create functions, how we can call our functions, how we can define parameters uh to our functions, how we can uh send data to our function as arguments. Now it's time to see how we can return some data from a function. But let's first read the code that we have here to make things more clear and uh the code inside the function the main function. All right. Now let's define another function. I'm going to type the fun keyword space and it's going to be called get max. And this function is going to return the maximum of uh two integers. So I'm going to define here a parameter. I'm going to call it a in. And real app should more descriptive names. It's going to be of type int. And the next one is going to be called b. And it's going to be also of type int. I'm going to put the curly braces and I'm going to press enter. Now to get the maximum value between these two numbers, we type here. We type here val max and here we type an if expression. So we type if a greater than b then we're going to assign a to our max variable. Else we're going to assign b. And in the case that the two numbers are equal, b is going to be assigned to our max uh max variable. Now in order to return this uh to in order to return this u value to our function we need first to def to to say to the function what kind of data is going to return. And to do that, you go here, add the enclosing parentheses. And if you don't have spaces here or you wondering about uh how to have the proper formatting, you just press uh Ctrl Alt L and the code is going to be formatted. All right. And here we put a colon. And here we we define what uh what type of data this function is going to return. And in this case going to be an integer. So you put int here and this is similar to the way we declare a type for a variable. Now if you if you go down here now you see that it says a return expression is required in a function to block body. So now we need to return this uh now we need to return some data that we define here. In this case we need to return an integer. And to return uh the maximum value to our function, we type here return the keyword return and our value which is our variable max and press enter. Now if you call this function in our main function get max. And now let's also press Ctrl Alt L to format the code. And here we let's say that we type uh two arguments for our parameters. Let's say that put five and nine. Now if you run this code now you're going to see in the output nothing because uh in order to in order to get this uh value we need to retain this value to capture this value in a variable and to do that we define a variable in front of our get max function also be a file. I'm going to call it max. And we're going to assign uh the get max uh uh function to our variable and that is going to return or maximum number. Now uh you now you may see that we have two names. We have the same name here and the same name and that is not a problem because uh this variable max that we have inside the the get max function exists only inside the the get max function is scoped only here. This is why we can use uh the same name here. So if I try to type here also max to declare the variable of which is going to be called max. I can't because uh it says completing declaration because if you because the compiler doesn't know if you assign a value let's say to this it will not know what variable to to to call because have the same name but u up here is not a problem because u this is in the block of the main function. So it exists only here and this exist only here. Now we have called our get max function and this function is going to return the maximum of these two values which is nine. Now we need to print this value in our console and to print this value just type here print ln and uh we're referring our max variable. Now if you run this code you're going to get nine because uh our get max function is called the two the two arguments are passed to to our function. Then the code inside our get max function it's executed. It compares these two two numbers and if a is greater than b then it assigns a to our max a max variable or it's going to if that is not true it's going to assign the value of b and then it returns that max value to the to the function which is called. So this function returns that value here and and here we retain we capture that value which which is returned from the get max function in our max variable and then we print that max variable with our print ln function or statement and then we see the output in our console 9. Right? Another important thing to know is that after we type the return uh call or the return keyword the the function is left the function is stopped there. So if we add other text here let's say we add here a print ln and we type here some text. We see that this is highlighted and it says that it's this is a reachable code because after we typed our return max our function is uh stopped is leaved and uh it's not executing any any code below of uh of that return uh call or or of that return keyword. Right? Another important thing that you can do with return keyword is that you can uh return from a function without returning any kind of value. And in that case it will have the same effect and uh that means that it will uh leave the function right there even though we don't return any kind of value. So I can type here return but let's uh delete here the type because now it's expecting a value to be returned. And now this will have the same effect. it will leave the function right there and it will not execute any any kind any line of code below of our return keyword. So if I type here a print ln statement and I put some text here as you can see this has the same effect. If you type the return keyword in your function, it will stop the execution of the function and every line of code below of the return keyword is not going to be executed. So it's very good uh to know this. Also another important thing to know is that we can we can only return a single value with the return keyword. So we cannot return here two two integers let's say and um also we cannot have two returns in the same uh function. Let's say if I put here also return max I can't because this is also a reachable code but I I can have two returns two returns calls if they are reaching different branches. So if I change this u if uh expression to an if statement. So if I type something like this if uh let's say a greater than b. Let's add a curly braces. Now we type here return a else return b. So now we have two returns but they are never reached together. They they they are they are always reached separately. So this is why you can use here two returns because I said they they are reach always separately. So if you run this code, you're going to see the same output. So you see nine. So this is another way to to write the same thing that we had previously that but it's recommended to use u to use u what we had previously because with specifically is better to have an if expression. another way to to write the the same thing but in a more concise way and let's undo the code that we have that we had previously. So let's press Ctrl Z. So now I have the code that I had previously. Now if you hover over over this max it says variable used only in formatting return can be in line. So if you click here in line variable now this thing does the same thing that uh it it was um doing previously but this is a shorter way to write the same thing. So now it returns a if this condition is true otherwise else part the else part is called and it returns b. So we have the same thing but u uh written in a shorter way. All right. Another way to write the same thing that we have here is by using what is called the single expression function. And uh to do that we can uh remove the curly braces. We can put our code up here. We can uh remove the first curly brace. And we can also remove the return keyword. And here we put equals. And this uh now does the same thing that uh the code uh the previous code was doing but this is a more concise way and it's recommended to use uh this only if you know that you you have in your function a single expression or in other words if you know that you have a single line of code and you can also remove the type here because the type can be be inferred and uh again only use this if you know that you have in your function a single line of code and uh in that case you can uh you can uh skip the return keyword but if you if you have multiple lines of code you should always use the curly braces and the return call or the return keyword. All right so this our discussion about how we can return a value from a function. All right. So now let's continue our discussion about functions. And now it's time to look at what is called function overloading. And let's say that I want here to pass not two integer types but two doubles to be compared. So let's say that I type here 6.5 and 9.6. Now this will not work because we define here explicitly that the parameters are of type int. So we cannot pass here double. Uh and uh a solution to this it may be to create another function with a different name and to define uh the parameters as doubles. But that uh does not make a lot of sense because we we are doing the exact same thing. So it does not make sense to create a new to create a function with a different name. So what we can do instead is to let's change this back to integers is to duplicate this get max function and to do that you can press control D or command line on Mac. So I'm press I'm going to press control D here. Now what I can do is change the types of the variables. So I can put here instead of u integer I put double and here I can put also double. Let's add a space to make things more clear. Now if I pass here let's say uh 5.6 and uh 9.7. Now you see that we have no error. So if you run this code all right so we get in the output 9.7 and this is correct this uh is bigger than 5.6 But now it's using the second function which is the function which has the parameters as of type double. And we can see that this function now which is using the parameters as integers is gray out because the compiler can figure out what function to use by the argument types we pass here. So in this case we pass here doubles and it knows that uh it has to use this function which has the types which has the parameters defined as type of double right another way to overload a function is to let's also press Ctrl D is by changing the number of parameters. So I can um put here let's say uh also integer also integer. So let's pass the third parameter now and uh this also going to be an integer. It's going to be called c. It's going to be of type int. All right. Now let's change this logic to an if statement. So, let's delete the if expression. Let's put the curly braces. All right. So, let's change this now. So you can type it here. Now if a greater than equal to b and a is greater than equal to to c then we're going to return a. So now we're using the return keyword. else if now uh we putting here an else if B is greater than equal to A and B is greater than equal to C then return B else C is going to be the bigger value so put here return See? All right. Let's press control alt uh return b here. Right. So I need to put here the type. This way I get that error. Right now everything is okay. Oh, so from here is the problem. All right. So now if you run this code, let's change this to let's say five 7 and 10. Now if you run this code, you see that the two functions above are gray out. And now it's going to use the third one which has third parameters. All right. So we get we get in the output 10 which is correct. So if you change this to test let's say we put here 100 let's put here three and if you run this code now we get 100 and this is correct. All right. So this our discussion about function overloading and you can use function overloading every time you know that you'll do the same task but with different type of parameters or with a different number of parameters. Also you may have noticed that we have u over if over the if statement. So if you hover over here it says that the return can be lifted out of if. So if you click this you can uh write it in this way or we can keep it in the previous way but uh because this is u underline that means that cotlin recommends to use it in this way in this way. So see you in the next video we're going to talk about u default parameters. All right. So now it's time to look at another features uh that you can use with functions and that is called default parameters. And uh let's first declare a function. So I'm going to declare down here at the end of our enclosing curly brace for main fun. I'm going to call it send message. Let's put the parenthesis. And inside the parenthesis I'm going to define two parameters. The first is going to be called name. It's going to be of type string. And this second one is going to be called message and it's going to be also of type string. Now let's uh you declare the curly braces. Press enter and let's print those u parameters in our console. So let's put a print line here and inside the also uh while I'm typing uh while I'm typing this print line you may see that this print function is a function which is overload referring to our previous video because you can see that it can it has the same name but it can take different types of parameters here. So it has int this thing which is any byte short char long. So the print f function that uh we've used uh throughout our videos is overloaded. This why you can pass different uh different types of uh of data to it. Now let's type here print ln quotation mark and inside the quotation mark we're going to type name equals dollar sign name. So now it's going to print the argument values that we're going to pass to this function as parameters. Now let's call this function here. So let's type send message. Let's press Ctrl L to format our code. And here I'm going to pass a name. Let's say Alexa and the message. Let's say hello. Now if you run this code, you're going to see the output um uh the name here is going to be this argument that you type here which is Alexa and the message going to be hello. So we have this but let's say that for whatever reason the user doesn't want to to send uh any kind of message but we still uh need to use this function. What you can do then u one way is to remove this parameter all the way but u that's not uh good because you still need to use this function with those two parameter the name and and the message. So in let's say that if the user doesn't pass any message, we can define here what is called a default par a default uh parameter. So we can assign here a default value for our parameter. So I'm going to put here uh just uh double quotation marks or u or you can put some text here but let's put just the double quotation marks which is just an empty text. So if now I don't pass a message here now it's still going to work because as a value for this parameter is going to use the default value that we defined here. All right. So let's run this code to see now how it works. So now I get name Alexa and message equals empty because double quotation marks without any text is uh an empty text. Now if I want to pass here now the message now I can pass here the message and now uh our default value is going to be replaced with the argument value that we typed here. So now I'm going to see name Alexa message hello. All right. You can do the same thing for u for the name. So here you can define let's say that the user doesn't want to to type a name. And we can define default name. Let's say user. Yes. Now let's say that I delete both uh arguments here. And I just have an empty function. And if I run this now, it's going to use the default values that you type here respectively user and this empty string. So now I get name, user and message. Now let's put back uh what we have. So let's put here Alexa and let's uh put our message here again. Now let's run this code again. Now what we can also do is uh use what is called named parameters and uh let's say that I want to to pass a parameter only for the second uh second uh let's let's say that I want to pass a argument type for only for the second parameter that we define here respectively for our message. So if I delete this now, let's say let's say that I delete the argument uh for our name and if I want to pass a value only for the message, let's put in the quotation mark because it's a string. Let's say I put the message hello here. Now you can see that the hint is name. So it's using the first parameter that we defined here, not the second. So what we can do now to pass a value to only to our second parameter because as you can see it's using the it's assigning this value that we type here for this parameter and to solve that you can use what is called name parameters. So with name parameters we can um type an argument for our parameter irrespective of the order in which they are defined. So in our case we have the name for define and then the message. So to use that we can u type the name of the parameter. Let's say message. Now you see that we have this p message and it's equals to a string. So that means that we need to pass some text there. And now we can pass here hello. And now this is going to work because uh now we can pass a value to our parameter irrespect of the order in which they are defined here. Right? So now get name is this default user and the message hello. And we can do the same for um for the second one which is name. So I can put now here name and I can type here uh let's say Alexa and if you run this now you get Alexa is the name and the message is hello. So with the named parameters this how they are called named arguments we can uh pass a value to our parameters irrespective of the order in which they are defined in the function. So in our case name is first defined and you saw there that when I try to use uh the message to assign a value only for the message it's was it was using the first parameter because this was the word in which they were defined. But with this named parameters you can change you can um we can um type an argument irrespective of the word in which they are defined and you can put this in uh multiple lines to make it more clear like this. Let's say so this is our discussion about um named parameters and default parameters. But before we end our video I should say that uh to the default parameters we cannot only type liters values like we typed here some values directly we can use variables or you can use uh a function which can return a value. So I can define here a function. Let's say fun send text. It's not going to have any parameters. It's going to return a string. So I'm going to define here a string as the type of return. And here we're going to type return and let's type some text. Now we can call this function here send text. And now this value which is returned for this function send text is going to be assigned to our parameter message. So now if you run this code let's uh increase this a little bit. So now if you run this code you're going to see that the now the default value for the parameter message is the return value from our send text function. So you can see now that we have okay so let's change it here to no value for our message. So now if you run this code because uh there we have an argument passed. So let's not pass no argument. So now it's using the value that we what's was passed as the default value for our parameter which is this return value from this function. Now because here we have one single line of code. We have one single expression. We just return some text or function. We don't have multiple statements in our function. We can write this as a single expression function. And you can remove the curly braces and you can write this as a single expression body like we did in previous videos. You can put it just equals and you can remove the type here also. So if you run this code now you're going to see it to the output uh for the parameter message some text which is the value which is returned from this uh function from the single body expression function. Right? So see you in the next video. All right, now it's time to look at the v ar keyword and how we can use the var keyword with functions. But first, let's declare a function below of our enclosing curly brace of our main function. So I'm going to type here the fun keyword to declare a function. I'm going to call it sum because it's going to return the sum of all the parameter that we're going to define. So I'm going to define here some parameters. First going to be called a. It's going to be of type int. Second, second is going to be called B also int. Third is going to be called C also an int. And the final one is going to be called D also an int. And this this function is going to return the sum of all of those parameters. So we need to define here uh the return type which is going to be an int whole number. And now we can put the curly braces. And here we type return a + b plus c plus d. And this is going to return the sum of all the parameters that we define here or uh the values that is going to be passed as arguments to this function. And you can write this as a single expression function if you want. So you can write this uh like this if you want. this will have the same effect but u I will I will write it with the u curly braces because uh it will make things more clear. So let's uh undo this. All right now let's call this function here sum. Let's press ctrl alt l to format the code and u let's pass some values here. So I'm going to type here five 6 7 and u 10. All right. If you press shift control P on this, you're going to see that the type of this function let's press again. The type of this function is an int. So this function return returns an integer. So it's an is it's an expression. That means that we can put this instead of putting uh storing um um the return value in a variable we can put this direct in a print ln uh function. So we can type here print ln and we can put the function inside here because that function is of type int. So it's going to return the sum of all the arguments that we type there. So now if you run this code we get we get 28 because 5 + 6 + 7 + 10 is 28. Now if I want to pass another parameter here let's say that I want to pass another number here. I can't because I am limited by the number of parameters that we define here. Of course that I can define another parameter u here. But what if I want to pass uh 10 numbers or 15 numbers or 100 numbers? In that case, it will be very tedious to write all of those parameters. And and to solve that, you need to use what is called a var var keyword. And a var keyword allows us to pass more than one parameter actually more than one argument to our uh parameter. So to declare a var keyword we type here u we type here var arg. So this is the keyword var stands for variable arguments. Now we need to define a name for this uh var. I'm going to call it uh numbers and we also need to specify a type what kind of uh values this var is going to store. Now this return keyword uh is this variables here are red because they don't exist. Now if I want to but first let's say that I want to type here another uh parameter now now you see that we have no problem. So I can type here whatever number of uh numbers I want and there is no problem as you can see. Now um to to sum the values which are stored in this var numbers we need to use something which we didn't cover yet but we have a separate section which I show you how to in which I explain uh this thing that I'm going to use now and that is loops because uh now we need to loop through our numbers and to add our numbers to a variable so we need to use what is called a for loop. So to do that first we declare a variable. I'm going to call it um let's say uh result. I'm going to assign a value of zero. Now to use a for loop we type four. And again, I'm going to go into more detail about this in the separate section, but now we need to use this because there's not another way to to sum up the values inside the var numbers. And here we need to define a name for our variable. So it's going to be called number. And now we use the keyword in. And now we're referring the parameter v the var parameter which is defined up called numbers. All right. Now we here you can put a curly braces and inside the curly braces we're going to type here should be numbers result. And now we're going to use that abbreviation which is plus and equals plus equals number n. Now here going to return the result. Return result. All right. So let's uh explain what we have here. So first we declare a variable. We assign a value of zero to it. So it's a type int. Next what this uh for loop is doing. So this for loop is doing what is doing is executing um a line of code multiple times. So it executing it's executing uh uh what's inside the curly braces until uh the it finishes to it finishes the to loop through all these numbers which are stored in the var numbers. So it uh first assigns uh the first uh the first number to our number here and it loops and it adds that it adds that number to our result. Then it it does that again and again and again until it finishes it reaches uh the final number five. And in that case also going to assign from our numbers uh the number five to our number. And then that uh number is going to be added to our result variable. And when it finishes uh that uh looping when it finishes that uh thing it's going to return the result. It's going to return all the values which are which were been uh added to our result variable. So now if you print this now you get 85 and probably this is the result of this uh calculation. So this is how you can use var numbers var keyword to store more than one single more than limited number of parameters defined by you. And uh another short shortcut to to loop through this is using a for each loop. So we can do something like this. But this time let's say that I want I don't want to return uh something. So let's say that I delete this and I I can uh type here actually numbers for dot here you put dot for each and here you type print ln. Again, this is a more complex sub subject which is going to be covered in the next videos. So, here we type print ln and we type it. All right. Now, let's uh let's call our sum. Let's define some numbers here. Let's say that I type here 1 4. All right. So if you run this code now you can see that every number in that uh that we defined here is printed one by one. So this number this for each which is also a loop it goes through all of these numbers. it and it prints them one by one. And again, we're going to look at for the for loop and the for each in the next videos, but I had no other way to show you this example without using them. But if you feel confused by for the for each or the for loop, don't worry. We have a separate uh section which in which we talk about in detail about this. For now we need to only the only thing you need to know is that you can um pass an indefinite number of parameters of actually you can pass a diff an indefinite number of arguments to our var numbers because this allows us to to par to to pass uh an indefinite number of values. So here you see that we have 1 4 5 6 7 8 9 5 7 and six numbers that we have here. So this for each is going through all of them and it prints them one by one. So see you in the next video. All right now it's time to start our discussion about loops. But first let's create a new project. I'm going to call it loops. Select IntellJ. Click on next and finish. Right, a project is created. Let's delete the code inside the curly braces. Let's hide the project pane. And um let's start by explaining what loops are and why you need to use them. So loops allows us to execute um a piece of code multiple times without uh you writing down every uh line of code uh manually. So let's say that I want to print uh 10 numbers in the console or 20 or 50. Instead of printing uh instead of typing that code manually line by line which will be very tedious, you can use a loop and you can put that code in a loop and that code in the loop it will be executed repeatedly uh until uh a certain uh number is uh reached. So let's see how we can define a loop. To define a loop we type here four. This is the f first loop that we're going to look at because there are other loops. So this is the for loop. We press enter. Inside the parenthesis we type I. This is convention. You can put whatever name you want there. Now we put here in this is the keyword in. And now let's say that I want to loop from one from 1 to 10. And to do that we type one dot dot. This is a range one to 10. Now I put a curly braces and inside the the curly braces I'm I'm going to put the code that I want to be executed 10 times. So I'm going to type here print ln and I'm going to type the value of i at each iteration. And uh we can put it like this. You can type here uh you can put in the quotation marks and you can do the placeholder can put I. So now if you run this. So now you get I is 1 2 until 10. So it what it's doing is is uh looping through this range until it reaches 10 then it stops. Now uh you can write this I like I did it here using a placeholder or you can write it like this. Now if you run this code it will have the same effect but we will not have the taste before here. So now we get one until 10. Now there are other other var variations to use uh this uh range and one of them is to use until. So let's uh comment this code. So let's press control slash. Right now let's type four again. And now I'm going to I'm going to call it also I. Here you can put whatever name you want. And uh instead of now I'm going to put also again the yin keyword. And instead of putting the dot dot to loop to arrange I'm going to put one until 10. All right. Now I'm going to add the print len statement and here I'm going to type I and uh this will have the same effect with the sing with a single difference that one until 10 is is going to exclude 10 and one uh using the range do 10 it will include 10. So if you run this code, you're going to see that we will not have uh 10 here because 10 is excluded when uh you're using the until. So now as you can see we have one until 9 and 10 it's excluded because now we're using the until. So if you if you're using the range which is dot the last number is going to be included. But if you use until the N number is going to be excluded. So it's important to note that this uh two respectively the until and the range can only count [Music] upwards. So we cannot put here 10 until one because the first uh number the number of on the left needs to be smaller than the number on the right. So we cannot put 10 until one. We can only count upwards. You can only put one until 10 or 1 10. So to loop backwards, we need to use another uh link. So let's comment this code also. Now to loop backwards, we type four. Again in the paradise you put I here you can put uh whatever if you want in. So here you can put uh whatever name but uh convention is I is used. Now I'm going to put in and now I'm going to put down first. Let's put uh 10 down to one. Let's add the curly braces the print a line and let's put I here. Now this is going to this is going to count downwards. Now it's going to it's going to count from 10 to one. So if you run this code now I get 10 9. So it's counting it's counting backwards. Right? Another thing that you can do is that you can introduce an arbitrary step. So let's uh let's comment this code also. Let's add below another code another for loop. So here I'm going to type four parenthesis uh let's put I and let's use uh the until. So in one until 10. So now I can put a uh an arbitrary step. So at uh at at this arbitrary step that number which is that is that is at that arbitrary step is going to be skipped. So I can put here step two let's say. So now I'm going to put the code places to have the some code executed. I'm going to put print ln and let's put here I. So now if you run this code now we get 1 3 5 7 and 9. So you can see that uh uh at uh the second uh iteration the number is skipped. So we have one two is skipped. Now we have then we have three four three and four is skipped and so on. So you can put this arbitrary step if you want. So let's comment this code also. And let's add the first code that we had here to to talk a little bit more. So let's put I in one. Let's put the range 10 and curly braces here. I'm going to type print ln. I now if run this code we'll get the same output that as at the beginning of of the video. So get one until 10 inclusive. Now again what this uh loop is doing is going through this is going through this to this range from 1 to 10 and it's executing the code inside the curly braces 10 times and when it reaches 10 it stops and it executes the next line of code below here that because here we don't have any line of code um our program exits exit and is finished it. So this is our discussion about for loop. There are other loop loops which can loop on which can loop as long as a certain condition is true not as long as uh a c a certain number is reached. So we're going to look at the next loops in the next video. See you next. All right. Now it's time to look at the next two loops that you can use in cotlin and those are the while and the dow loop. But first let's um open our previous project called loops because I'm going to type my code there. So open your previous project and here I already have deleted the code that we had previously here. But uh if you have the code delete it and um let's start our discussion about the while loop. So first I'm going to type uh while this is this how you declare the while loop. And here you can uh press enter to fill in the the while key and the parenthesis for you or you can type manually the while and the parenthesis. So I'm going to press enter to fill that for you. Fill in that for me. And next you put a space and you put curly brace here. So I'm going to put the left curly brace and press enter. And the right curly brace is going to be added automatically as you can see on the line five. Now a while loop loops as long as a certain condition is true. In contrast to the for loop which loops for a specific number of time. A while loop loops as long as a condition or an expression is true. So this gives us more flexibility in um circumstances where we don't know for what specific number of times we want our code to be repeated. So we put our condition inside the parenthesis here and as long as this condition is true the code inside the curly base is going to be executed repeatedly. So let's see how we can do that. So first let's declare a variable here. I'm going to call it number and I'm going to assign a value of zero to it. And here I'm going to type while number is less than 10. Inside the curly brace I'm going to type the print alen function. And we're going to output the value of our number. And then we're going to increment the value of our number. So I'm going to put number. I'm going to use the increment operator plus plus. Let's put a space here to have things uh more clear. Now, if you run this code, you're going to see the numbers from 0 to 9. So, what is happening? How this uh works? First, we've decarble, we assign a value of zero to it. Next, we'll loop using the while loop. And first, the Y loop is checking to see if this condition inside the parenthesis is true. So it checks to see if the value of our variable number is less than 10 and it's true because we have zero. Then it executes the code inside the the curly braces and it outputs the value of our number in the console using the print function. Then it increments the value of our number and then it loops again but now the value of our number is one. So now it will compare again if the value of our number is less than 10 which is true. It will loop again. It will output this value. It will increment this value. So now we have two and so on until uh it reaches nine. When it reaches nine, it will increment the value. It will it will print it will output the value in the console. It it will increment this value. Then it will check again to see if this condition is true. But now the value is going to be 10 because it was incremented when it was nine. And this condition is going to be is going to evalate to false. And the code inside the cable basis is not going to be executed. This why we don't have 10 here. And u it executes the code that comes below our y loop. But because you don't have any code, the program is terminated. And you can uh write this thing in one single line. But it's recommended to use uh this form only if you know that you have one single statement in your uh loop. So if I can delete this this two and I can put here directly number plus+ and because this returns the value it increments the value and it returns the value is an expression I can uh I can run this code and it will have the same output but if I delete the increment operator uh at after the variable and If I put it before now going to see a different output here because this increments the value of u of our number then it decrements the value of our number and this is called prefix increment increment. So this is called prefix incrementing and uh the the previous one let's press ctrl g this is called postfix increment. So this is postfix because it's at the end of our variable and uh if you put the incremental operator at the the beginning of of our variable is called prefix or increment. Anyway, let's press Ctrl Z to have the code that we had previously and the curly braces. Now look what happens if I delete the increment uh if I don't increment our variable. Now, now uh let's talk what is happening because we don't increment the variable and the variable always has the value of zero. This will always be true and it will execute the code inside the curly braces forever. So we'll have what is called an infinite loop. So if you run this code now you can see that um it prints zero forever because this condition never has to to to false. So let's stop this by pressing on this uh red uh square. So now the process uh the program is terminated. So this is why you need to increment the value of our number because we want that condition to be false at some point in the in the in the in the in the future. Next let's say that I assign here 10. So if I run this code now this condition is going to be false because 10 is not less than 10 and the code inside the curly brac is not going to be executed. So now we have no output here because this condition is evaluates to false and it's not executing the code inside the curly bracing. But there are certain situation where you'll want to have your code executed at least once even though the initial the initial uh condition or the initial expression is false. So if I delete the curly braces and the code inside the curly braces here now to to do what I said to execute the the code even though the condition is initially set to false to execute the code at least once you need to use a dowh loop and to use a dowh loop we put here a do press enter to add the curly braces and we put our while at the end of of our enclosing curly brace of our do and here inside the calibration. I'm going to put our print function and I'm going to output um the value of our variable number. So now if you run this now I get 10 because this executes the code inside the curly braces at at least once and after that it uh comes down here and checks to see if this condition is true or false. And if this condition is uh true it will repeat the code. But if the condition is false it uh it it will not repeat the code. But the important aspect here is that it will um execute the code inside the curly braces at least once irrespective of our condition if the condition is true of or false. In our case is false. So these are the three loops that you can use in cotlin. The for loop the y loop and the dy loop. Now it's time to see um how you can use the continue and the break keywords with those loops. So let's press Ctrl Z to undo the code to have the Y loop back because I'm going to start the Y loop and to see how we can use the break and continue keyword with the Y loop. Let's bring this up a little bit. So let's first look at the continue keyword. And the continue keyword can be used to bypass a section of code. So let's say that I want to skip the number seven in our uh loop. So I can type here if our variable number equals 2. So 2 equals 7. Let's put curly braces. Press enter to add the right curly brace. Continue. So now what this is going to do is when it's going to reach the number seven and this condition is going to be true. It's going to call continue and it's going to bypass the code that comes below of our if statement. So if you run this code look uh what happens it happens nothing because uh here our number has a value of 10. So this condition is false. So the code is not executed. So let's put here zero. Now if you run this code so we get 0 1 2 3 4 5 and six. But uh and you see that at seven it uh it skips the code. But why you don't have the next numbers? This is because uh and we created here an infinite loop. So let's stop this and uh to explain. So what happens here is that because um we said we say here explicit to check here explicit. We check here to see if our number is equal to seven. Then we're going to continue. In other words, you're going to skip the code which comes below of our uh of our if then statement. Then this code is never called. So this code is never called the print and the more importantly the increment operator is not never called on our number variable and because of that because our variable is not incremented here when it's going to loop again it it will have again the value seven. So this condition the in the if is going to evaluate again to true and it's going to call continue again and because this code again is never reached this code and the value is not not not incremented it it will still have the value seven. So this is going to evaluate again to true. So it does this uh forever and to change that we need to put our u number and which is incremented before our if and that is going to solve the problem. So now if I let's uh now if I run the code let's increase this Now get 1 2 3 4 5 and seven is skipped then 8 9 and 10 because now when this uh condition eval to true it will continue it's not going to print uh the code is not going to be executed below of our if and u it will come back to loop and now it's going to increment the value and the value is going to eight instead of seven. This is going to evalate to false and the continue is not going to be called and it will print the number eight and it will do the same for it. It will increment the value again and then we'll have nine and so on. So this is how you can use the continue keyword and you have to be careful where you put your in your where where you increment your variable because as you saw you can easily get into an infinite loop. Now you can have a more complex expression here. So you can put here if number is greater than let's say two and number is less than u 8. Now if you run this look what happens. Now we get 1 2 and then we get 8 9 and 10. Because what this condition now checks is is uh if our number is greater than 10 and less than 8 then continue then skip that number. So this why the numbers between uh uh 2 and 8 are skipped because uh we call continue on uh on them and u if you hover over here you see that it says the two comparison should be converted to a range check. So we can uh write the same condition that we have here using a range but u this is different from a range that you you've uh used in a for loop in the sense that this is now checking to see if our number is in the range three from 3 to 7. So if you run this quad now I have the exact same output but now we are using the range to to check to see if our number is in that range and if it is that range we we call continue to skip that. Um the next thing that you can do is you can uh break the loop. You can stop the loop using uh the break here. So I can say here again if number equals equals let's say 7 call break. So I'm going to type here break. Now when this condition is going to vary to true it's going to call break and the loop is going to stop. It's not going to execute any line of code after that. It's going to stop. It's going to leave and it's going to execute the code which comes below of here. But in our case we don't don't have any code. So you see that we have 1 2 3 4 5 and six. And when it reaches u seven it breaks and it not it's stopping the loop. It's not printing the value and it executes the loop. So it terminates. So um and we can do the same thing with the for loop. So we can use the for we can use the continue and uh and break with a for loop. So you can type here four let's say I in let's create uh let's say 0 to 10. Let's create this range. Let's add a curly braces. Now I can say here if I in let's say 3 to 8 then continue. Let's print the value. Let's type here print ln. Let's print the value of I here. So now if you run this code. So we have our first loop which loops uh to six and then it breaks because you call break there. Then it using the for loop, it loops. It loops u from zero to 10. But uh because here we're checking to see if our number is in the range 3 to 8 then skip it. We get 0 1 2 and then the numbers uh the three and uh the numbers between the three and the numbers between three and eight inclusive are skipped here. So we have uh 0 1 2 and then the numbers between two and nine are skipped and we have 9 and 10 and we can use also the break keyword with the four. I can put here if I equals 7 break. So now if you run this code now you get zero 1 2 3 4 5 6 and when it reaches seven it breaks similar to our while loop. So this how you can use the break and continue cured with the while and for loop and you can use uh the break and continue cure also with the dy loop. Next, we're going to look at how we can nest loops within each others. And we're going to look at an example using the while loop. So, I'm going to delete the code of the for loop and the print statement there. I'm going to delete the if then statement, the break keyword. And I'm going to bring the print ln function up there. Down here, I'm going to declare a variable. It's going to be a var. I'm going to call it i. And I'm going to assign a value of zero to it. Now, inside the y loop, I'm going to type another while loop. Here I'm going to put a condition while I is less than five. We're going to increment i and we're going to print going to output the value of i in the console. But let's put some text here to make uh clear what loops what loop is looping. So I'm going to put uh three asteric signs here. I'm going to type the dollar sign and I. So now if you run this code, let's also change this condition to less than five. So if you run this code now we get first we get one then we get our code inside of inner while loop which is the three asterics and we get 1 2 3 4 5 then it loops again but let's take this code line by line to explain what is happening here. So first we declare this variable called number. We assign a value of zero to it. Then we loop using the while loop the outer loop and it checks to see if this condition is true and it's true. Z is less than five. So it executes the code inside the curly braces and it increments the value of a number by one. Then it outputs the value of our number in the console. So we get here one. Then it declared this variable called I and then it start looping inside our outer loop while using the inner loop while. So it loops here five times. So when it finishes it goes back and now the value was incremented by one. It will check again if the condition is true. It will uh execute the code inside the calibrates. It will increment the value now. So the it will have two. It will output that value. So now we have two here. And then it will u loop again five time five five times using the inner loop. And it will uh do the same thing for the next round and the next round until you get to five. When uh we'll get to five, five will not be less than five and this condition will evalate to to false and the code inside the calibrate is not going to be executed. All right. And uh you can use the break keyword inside the inner loop. So I can say here if uh I can put it above here. So I can say if I equals equals zero then break. And uh you can put the break and continue keys in one single line. If you want you can avoid the curly braces. So now if you run this code now get 1 2 3 4 5 because now uh when it reaches the inner loop it breaks immediately at every at every iteration in the outer loop. So when it comes down here it will check to see if this condition is true. It's going to be true. But when it gets to the if statement it will break. it will stop the loop. So um it will execute only the outer loop and with a break uh keyword you can not not target only the loop. So with with this break keyword we stop we stop only this while loop that uh that we are currently that is currently the nearest loop. But there is something which which is called labels and with labels you can target an outer loop. So we can break the outer loop here. So we can break the while loop which is here. And to do that we we give to our while loop a label. So we type here a name. I'm going to call it outer. And we put at. Now here you can put break. And now I'm going to type at and put outer. And this this should be a single word. Now if you run this look what happens. Now we get one because it breaks the outer loop. So it first uh evaluates this condition. It increments the value of our number. It prints the value of a number and uh then it goes in the inner loop. It checks to see if this condition is true. Is true. Then it checks to see if I is equal to zero which is uh true. Then it breaks. But now it's not breaking this loop, this inner loop. It's breaking the outer loop. So it's breaking this loop. So this is why you get only one. And the code is not executed because it breaks the outer loop. And the code uh our program or code is terminated because down here we don't have uh any code. So this is how you can use labels with the break keyword to target an outer loop and the labels can be used also with the continue keyword and uh in other loops that we've looked at. So this is our discussion about loops. What we can do I should say is that you cannot target with the labels an inner loop from an outer loop. So we cannot uh we can give here a name to a label to our inner loop but we cannot target our inner loop from our outer loop. So this our discussion about loops and in the next video we're going to do a challenge. All right. So now it's time to do a challenge using all the knowledge that we have accumulated so far. And I'm going to use the previous project to put my code there. But if you don't have the previous project, you can create a new project and give it whatever name you want and type your code there. So I'm going to open my previous project. I'm going to delete the code inside the main function. Now what is the challenge? The challenge is to create an arbitrary range of numbers. Then we need to find a way to go to that arbitrary range of numbers and to determine if uh a number is an even number or if a number is an odd number and based on that if the number is an odd number skip that number but if the number is an even number then output that number in the console. Additionally, you should create a way to count all the even numbers that you've found in your range and display the total numbers of even numbers um at the end of your program. So try to do this solution and uh after that you can watch my uh solution to this. So first I'm going to declare I'm going to declare a variable called number. I'm going to assign a value of one to it. Next, I'm going to declare another variable, but this is going to be a val. It's going to be called last number, and it's going to have a value of 20. Now, I'm going to loop using the while loop. And in the parenthesis, I'm going to type while number is less than equal to last number. Then I'm going to put curly braces. Now, I'm going to increment our number. Now here we need to add the logic to determine if a number is an even number or not. And to do that I'm going to add the function at the end of our enclosing curly brace of our main function. So I'm going to type here the fun keyword. Now you need to give a name to our function. It's going to be called is [Music] even number. We're going to put parenthesis and it's going to take a parameter called number of type int this function and it's going to return a boolean value true or false this function. So you put colon and we type boolean then you put curly braces and now we add the logic to determine if a a number is even or not. So we type here if and now inside the parenthesis we put another set of parentheses and here we refer our parameter number that we have defined above and here we put the modulus operator because the modulus operator gives us the remainder of a division. So we put the modulus operator two. Now outside our inner parenthesis we put equals equals zero. Then we put curly braces and we return true else we're going to return false. Now how this logic works. So our u num our number uh parameter which is defined here is going to take an argument. Let's say that it's going to take an arbitrary number. Then it's going to check to see if that number divided by two has no remainder. If it and if it has no remainder, then that number is an even number because the because even numbers when they are divided by two, they have no remainder. And if that is the case, if the remainder equals equals to zero, our number is an even number and it's going to return true and the else part is not going to be executed. Now let's add the logic here. Let's use our function here because as you can see now it's uh gray out. It's not used. So let's type here if and in the parenthesis we're going to type is even number our function. We're going to call our function here and we're going to pass a parameter to it called. Now I'm going to pass the parameter. We're going to pass an argument and the argument is going to be the variable that we define above. As you can see you have the V there. So press enter to fill in that for you. Let's press Ctrl Alt L to format the code. Now if you press shift Ctrl P on this, you're going to see that this is a boolean expression because this returns true or false. So we can put the logical not operator in front of it to change his value to switch it value from true to false and from false to true. So let's put curly braces and here we're going to call our continue keyword. down here I'm going to add a print ln which is going to output uh the the number in our console. Next we need to add the logic to to count the total the total even numbers which are found. So I'm going to declare v is going to be even numbers counter and I'm going to assign a value zero to it. And here we're going to increment our even numbers counter. So now if you run this code we get 2 4 6 8 10 12 14 16 18 and 20. So these are the even numbers that they that they f that the our program found and is correct. But let's add a print n at the end of our loop to print the total number of numbers. of even numbers found. So let's put print ln here and let's put let's refer our even numbers count here even numbers counter. So if you run this let's put some text actually let's put this in curly braces. So let's type here total number of even numbers found equals dollar sign. Now if you run this now you get total numbers of even numbers found is 10 and this is correct and it outputed correctly our even numbers and it show that the total number of new numbers found is 10. So let's now think about how this logic works here. So let's look at the if statement that we have here. So what is doing this thing here? So let's take an um an um specific example, a specific number to to to see how this work. So let's say that here we pass four and it's going to use the logic to determine if this number is an even number and if that number is an even number, it's going to return true. So this this function which is a boolean expression is going to return true. And then we're going to negate this value. We're going to change this value from true to false and continue is not going to be called and uh the code which comes below which is uh our variable which uh counts how many even numbers uh we have then it uh it increments then it outputs that number in the console. So we have our even number four output outputed in the console. But if you pass here three that the number is not going to be an even number because has a remainder and this is going to return u false and because here you put the logical not operator that is going to be switched to true and the continue key is going to continue keyword is going to be called and our code which w

Original Description

This massive Kotlin and Android course takes you from the fundamentals to advanced concepts with hands-on practice. Throughout this course for beginners, @programmingwithalex.585 will teach you to build multiple real-world applications, including an impressive Uber clone that demonstrates professional-grade development techniques. The course covers Firestore, SQLite, MVVM, Retrofit, Navigation Component, Jetpack Compose and more. Alex's Python course: https://www.udemy.com/course/learn-python-from-zero-to-expert/?referralCode=42090420F4BB05718386 Alex's Java course: https://www.udemy.com/course/the-complete-java-course/?referralCode=022EBD61D8A97DB18E22 YouTube Channel: https://youtube.com/@programmingwithalex.585?si=nF0-5uS1XAyq9Xg0 - 00:00:00 Introduction to the Course - 00:20:07 Downloading and Installing IntelliJ IDEA - 00:27:47 Creating Your First Project - 00:44:47 Working with Variables and Data Types - 01:36:58 Operators and Operations - 01:56:17 Control Flow - 02:08:11 Comparison and Logical Operators - 02:38:11 Nullability in Kotlin - 02:53:57 Functions - 03:49:16 Loops and Control Keywords - 04:30:54 Challenges - 04:37:11 Arrays - 05:51:50 Object-Oriented Programming - 06:33:22 Inner Classes - 06:38:19 Object-Oriented Programming Challenge - 06:54:16 Inheritance and Interfaces - 07:28:01 Sealed and Abstract Classes - 07:49:02 Data Classes - 08:58:11 Collections - 09:15:01 Collection Operations - 09:34:47 Ordering Collections - 10:16:30 Generics - 11:17:16 Access Modifiers, Packages, and Imports - 11:45:29 Exception Handling - 11:54:55 Lambda Expressions and Higher-Order Functions - 12:17:23 Scope Functions - 12:41:21 Threads and Coroutines - 13:37:31 SQLite Database - 14:50:11 Android Studio - 15:18:32 Setting up UI with XML - 16:00:16 UI Elements - 16:52:43 Activities and Fragments - 17:20:20 Layouts - 18:00:00 Collapsing Toolbar - 18:16:16 Animations - 19:07:30 Navigation Drawer, Themes, Styles, and Options Menu - 20:33:03 Jetpack Compose - 22:22:57
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

This course teaches Android and Kotlin development from fundamentals to advanced concepts, covering various tools and technologies, and provides hands-on practice with building real-world applications. It is suitable for beginners and covers a wide range of topics, including database management, user interface design, and software development. By the end of the course, students will be able to design and build their own Android apps using Kotlin.

Key Takeaways
  1. Download and install IntelliJ IDEA
  2. Create a new project in Android Studio
  3. Learn Kotlin fundamentals, including variables, data types, operators, and control flow
  4. Implement UI elements, activities, and fragments
  5. Use Firestore and SQLite for database management
  6. Apply MVVM, Retrofit, and Navigation Component for app development
  7. Use Jetpack Compose for building user interfaces
💡 The course provides a comprehensive introduction to Android and Kotlin development, covering both theoretical and practical aspects, and provides hands-on practice with building real-world applications.

Related Reads

📰
7 Apache Kafka Design Patterns Every Backend Engineer Should Know
Learn 7 essential Apache Kafka design patterns for building scalable and efficient real-time data streaming systems
Medium · Programming
📰
Symfony Validator: Where Framework Validation Ends, Domain Rules Begin
Learn to separate framework validation from domain rules in Symfony to avoid duplicating logic and improve code maintainability
Dev.to · Gabriel Anhaia
📰
Seu status de pedido não devia ser uma string
Learn how to replace status strings with Enums in PHP 8.1 to improve code quality and prevent bugs
Dev.to · Denis Augusto
📰
From a Go CLI to a full developer ecosystem: Gopher Glide for IDEs
Learn how to transition from a Go CLI to a full developer ecosystem using Gopher Glide for IDEs, enhancing your backend development workflow
Dev.to · Shiyam
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →