From the course: Programming for Non-Programmers: Android & Kotlin
Conditional statements
From the course: Programming for Non-Programmers: Android & Kotlin
Conditional statements
- [Instructor] Conditionals, or conditional statements, are the next building block of programming that we're going to talk about. Conditional statements are if/then statements. In an if/then statement, first a condition is evaluated as true or false, and then, a block of code similar to a method is executed depending on the result. So if the result is true, one block of code can execute. And if the result is false, a different block of code can execute. When you're building a conditional statement, you start with the if keyword, and then a true or false condition is placed within parentheses. Note that when you use conditional statements, they're almost always placed within methods. So they're not written like methods, they're inside of methods. After the if keyword and the parentheses, curly braces then contain code that runs if the condition is true. If the condition is false, a separate block of code, called an else statement, can optionally run. Here's what a conditional statement looks like in code. So we have the if keyword. And then we have the condition in parentheses. Now in this code, num1 and numb2 would both be numeric variables. So it's checking to see if the value of num1 is greater than the value of num2. So if num1 were equal to three, and num2 were equal to one, this condition would be evaluated as true, and then the code would run that's inside of the if body. If num1 were equal to zero, and num2 were equal to five, that statement would not be true. And so the if body would not run, but rather the else body would run. Finally, in working with conditional statements, there are three different conditional operators that I want you to understand. When I say operator, what I'm referring to is code symbols that are used within conditional statements. One of the ones we're going to be using in this course is the greater than symbol, which works just like it does in math. If the number on the left is greater than the number on the right, then the condition is true. So if we look at the example, that if statement would be evaluated as true because three is greater than one. And there is, of course also the less than symbol. Again, in the example here, the condition would also be evaluated as true, because one is less than three. And finally, the other kind of operator we're going to be using in the course is the is equal to operator. That's two equal signs. Notice that's different than one equal sign. One equal sign is used for setting the values of variables. Two equal signs compare two values to see if they are equal to each other. So in our example here, for is equal to, this condition would be evaluated as true because one is equal to one.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Conditional statements3m 20s
-
(Locked)
Using conditional statements5m 11s
-
(Locked)
Boolean values6m 24s
-
(Locked)
Arrays1m 45s
-
(Locked)
Using arrays3m 4s
-
(Locked)
Loops3m 42s
-
(Locked)
Using loops4m 25s
-
(Locked)
When statements2m 29s
-
(Locked)
Using when statements8m 6s
-
(Locked)
Practical examples of conditional statements2m 35s
-
(Locked)
Challenge: Build a mini calculator app2m 19s
-
(Locked)
Solution: Build a mini calculator app3m 3s
-
-
-
-