If statements allows you to execute code if a specific condition is met. In Kotlin, they are also referred to as expressions which means that they return a value.
If statements allows you to execute code if a specific condition is met. In Kotlin, they are also referred to as expressions which means that they return a value.
Syntax
if ([condition]) {
Examples
In the above example, there is an If statement that checks if the temperature is greater than 70. It if is, it will print "It is super hot today," else, it will print "It feels nice."
Since If statements are expressions, they can return a value. In the example, the same If statement is used, except, if the temperature is greater than 70, it will return "It is super hot today" and pass it into the message variable. If it is not, it will return "It feels nice" and that will be the new value of message.
Also note that if an If statement is used in conjunction with a variable, you must include the else block.
Comparison and Boolean Operators
Comparison and Boolean operators are used to add comparable logic to your code to perform specific tasks if it is met.
The comparison operators available are:
< : Less than
The boolean operators available are:
&& - And
Examples
In the example, it used the If statement to print out a specific message when a certain condition is met. in the second else if block, it combined 2 Boolean statements with the && (AND) operator.
populationInUSA > 250000000 && populationInUSA < 350000000